Monday, 21 February 2011

Post Comments web page in ASP.Net using C#

Recently I have developed a web application automation application in which I have added a functionality for the user to post their comments, experience and questions for the admin.
Thought to share this approach with all of you.
So here is my approach what I have done.
1. An aspx page with name URComments.aspx.
2. we will use XML file to store all the comments posted by the users.
3. Used GridView to show all the comments posted by the users.
4. On page load and/or on post button click we will call a funtion to read all the comments
posted till now by the user and show that in to GridView.
5. Below controls we will on design view of URComments.aspx.
a. Text box to get the Emailid from the user.
b. Text box for the user to entered Comments and/or suggestion.
c. Button to submit comments.
d. GridView to show all the comments posted till that time.
The web page would look something like this:

Fig 1. Web Page with Post Comments Feature

6. The XML file format would be something as below. This XML file we will use to store all the comments provided by the user. B d way you can customize this as per your requirement.


fig 2. XMl File to store Comments posted by User
Page Load :
public static string xmlPath =ConfigurationSettings.AppSettings["xmlFilePath"].ToString();
//on page load redirect the user to the specific url and then show all the comments
//posted //till //now
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
showThePostOnWebPage();
}

}

Showing Post in Web Page:
//Display the Post on webPage
public bool showThePostOnWebPage()
{
bool result = false;
try
{
DataSet dsForWebPage = readXMFile();
GridViewPost.DataSource = dsForWebPage.Tables[0];
GridViewPost.DataBind();
result = true;
}
catch (Exception ex)
{

}
return result ;
}


Reading the XML file for the comments posted till now:
//Read the xml File
public DataSet readXMFile()
{
DataSet ds = new DataSet();
System.IO.FileStream fsReadXml = null;
try
{
//Read an XML file to generate serial number
fsReadXml = new System.IO.FileStream(xmlPath, System.IO.FileMode.Open);
//Declare a dataset to hold all the XML value
//Read the value from XML reader to the dataset
ds.ReadXml(fsReadXml);
}
catch (Exception ex)
{
ds = null;
}
finally
{
fsReadXml.Close();
}
return ds;
}

On Button click (when user tried to submit the post):
protected void ButtonPost_Click(object sender, EventArgs e)
{
FileStream fsxml = null;
FileStream fs = null;
try
{
//check that the comments textbox is not empty
if (TextBoxComments.Text != null && TextBoxComments.Text != string.Empty )
{
//Read an xml file to get all the post commented till now
DataSet dsSet = readXMFile();
if (dsSet != null)
{
//make sure that table exist
int totalRowCount = dsSet.Tables[0].Rows.Count;
string strSerialNumber = null;
string getTheNumber = null;
if (totalRowCount <= 0) { getTheNumber = "1"; } else
{
//get the number
strSerialNumber = dsSet.Tables[0].Rows[totalRowCount - 1][4].ToString();
getTheNumber = strSerialNumber.Remove(0, 3);
}

Int32 currentSerialNumber = Convert.ToInt32(getTheNumber);
Int32 nextSerilaNumber = currentSerialNumber + 1;
string strSerialNumberToadd = "CFG" + nextSerilaNumber.ToString();
fs = new FileStream(xmlPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(fs);
// New XML Element Created
XmlElement newcatalogentry = xmldoc.CreateElement("CFGPost");
// New Attribute Created
XmlAttribute newcatalogattr = xmldoc.CreateAttribute("ID");
// Value given for the new attribute
newcatalogattr.Value = strSerialNumberToadd;
// Attach the attribute to the XML element
newcatalogentry.SetAttributeNode(newcatalogattr);
// First Element - Comments
XmlElement firstelement = xmldoc.CreateElement("Comments");
// Value given for the first element
firstelement.InnerText = TextBoxComments.Text.ToString();
// Append the newly created element as a child element
newcatalogentry.AppendChild(firstelement);
// Second Element - Date
XmlElement secondelement = xmldoc.CreateElement("Date");
// Value given for the second element
secondelement.InnerText = DateTime.Now.ToString();
// Append the newly created element as a child element
newcatalogentry.AppendChild(secondelement);
// Third Element - EmailID
XmlElement thridelement = xmldoc.CreateElement("EmailId");
// Value given for the third element
thridelement.InnerText = TextBoxEmailId.Text;
// Append the newly created element as a child element
newcatalogentry.AppendChild(thridelement);
// Fourth Element - UserName
XmlElement fourthelement = xmldoc.CreateElement("UserName");
// Value given for the fourth element
fourthelement.InnerText = hostName.ToString();
// Append the newly created element as a child element
newcatalogentry.AppendChild(fourthelement);
// New XML element inserted into the document
xmldoc.DocumentElement.InsertAfter(newcatalogentry,xmldoc.DocumentElement.LastChild);
fs.Close();
// An instance of FileStream class created
// The first parameter is the path to the XML file
fsxml = new FileStream(xmlPath, FileMode.Truncate,
FileAccess.Write,
FileShare.ReadWrite);
// XML Document Saved
xmldoc.Save(fsxml);
fsxml.Close();
//Clean the text box area
TextBoxEmailId.Text = string.Empty;
TextBoxComments.Text = string.Empty;
bool flag = showThePostOnWebPage();
RegisterClientScriptBlock("js", "
");
}
else
{
//show the message to the user that currently we are facing some issue
RegisterClientScriptBlock("js", "
"
);
}
}
//if it is empty then show the message to user
else
{
RegisterClientScriptBlock("js", "
"
);

}
}
catch (Exception ex)
{

}
}

Method whenever user clicked on Pagination of Grid View
//fire this even whenever user clicked on pagination at gridView
protected void GridViewPost_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GridViewPost.PageIndex = e.NewPageIndex;
showThePostOnWebPage();

}
catch (Exception ex)
{

}
}

Now you are ready with the Post Comments project.
You can integrate this with your web application to provide the user an option to share their suggestion/comments and issues with others.
Hope you would get benefited form this.

Feel free to provide your comments.
*************************
Thank You,
Md. Jawed

4 comments:

Muhammad Azeem said...

This is a nice article..
Its very easy to understand ..
And this article is using to learn something about it..

c#, dot.net, php tutorial

Thanks a lot..!

kranthi said...

good work...

Anonymous said...

good work..

Anonymous said...

good one