Completing an ASP.NET AJAX Client-Centric Wiki Application - The Server Side
(Page 2 of 4 )
As is mentioned above, the SendMsgServer.aspx page serves as a CGI's functionality. Now let's look into the source code:
protected void Page_Load(object sender, EventArgs e){
//get the name of current user
string username = HttpContext.Current.User.Identity.Name;
string title = Request.QueryString["title"];
string content = Request.QueryString["content"];
int categoryid = int.Parse(Request.QueryString["categoryid"]);
ArticleManager myarticle = new ArticleManager();
myarticle.AddArticle(title, username, categoryid);
//add the details into a XML file
myarticle.AddXML(Server.MapPath(".") + @"content.xml", title,
content, username);
Response.Write("Congratulation! You've succeeded in sending the
post.");
}
There are four tasks here in total.
- Accept the passed parameters.
- Add the general article information into the database table.
- Put the detailed article content into the corresponding '.xml' file using an XML file template (i.e. file content.xml located at the root folder, which specifies the format of a newly-generated XML file).
- Send back to the client side the message about the asynchronous invocation.
Obviously, steps 2 and 3 are of major interest to us. Let's continue to study them.
First, let's check out the code for the AddArticle method:
public void AddArticle(string title, string user, int
categoryid)
{
StringBuilder strSQL = new StringBuilder();
SqlParameter[] newsParms = GetParameters();
newsParms[0].Value = title;
newsParms[1].Value = getFilename().ToString();
xmlfilename = getFilename().ToString();
newsParms[2].Value = DateTime.Now;
newsParms[3].Value = 0;
newsParms[4].Value = DateTime.Now;
newsParms[5].Value = user;
newsParms[6].Value = categoryid;
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
strSQL.Append("INSERT INTO ArticleInfo VALUES " +
"(@title,@filename,@posttime,@replycount,@lastreplytime,
@postuser,@categoryid)");
SqlHelper.ExecuteNonQuery(conn, CommandType.Text,
strSQL.ToString(), newsParms);
}
}
Here, we first get the buffered parameters using the helper method GetParameters; if there are no buffered parameters this method will automatically create a list of parameters and buffer them. Next, we assign values to each parameter. At last, within the 'using' code block we accomplish the task of inserting the current article information into the ArticleInfo table with the help of a helper class named SqlHelper (you can study it in the downloaded source code).
Additionally, it's worth doing some more research into the above private method named getFilename:
private int getFilename(){
int cardrule = 0;
string strsql = "select top 1 InfoID from ArticleInfo order by
InfoID desc ";
try {
cardrule = (int)SqlHelper.ExecuteScalar(ConnectionString,
CommandType.Text, strsql, null);
}
catch { cardrule = 0; }
return cardrule + 1;
}
As you can see above, using the SqlHelper helper class gives us the maximum value for the InfoID field in the current ArticleInfo table, and then adds 1 to it. Finally, we return the integer value as the file name.
Next, let's take a look at the code of the AddXML method:
public void AddXML(string filename/*include path*/, string
title, string content, string user){
XmlDocument mydoc = new XmlDocument();
mydoc.Load(filename);
XmlElement ele = mydoc.CreateElement("title");
XmlText text = mydoc.CreateTextNode(title);
XmlElement ele1 = mydoc.CreateElement("posttime");
XmlText text1 = mydoc.CreateTextNode(DateTime.Now.ToString());
XmlElement ele2 = mydoc.CreateElement("content");
XmlText text2 = mydoc.CreateTextNode(content);
XmlElement ele3 = mydoc.CreateElement("postuser");
XmlText text3 = mydoc.CreateTextNode(user);
XmlNode newElem = mydoc.CreateNode("element", "xmlrecord", "");
newElem.AppendChild(ele);
newElem.LastChild.AppendChild(text);
newElem.AppendChild(ele1);
newElem.LastChild.AppendChild(text1);
newElem.AppendChild(ele2);
newElem.LastChild.AppendChild(text2);
newElem.AppendChild(ele3);
newElem.LastChild.AppendChild(text3);
XmlElement root = mydoc.DocumentElement;
root.AppendChild(newElem);
int index = filename.LastIndexOf(@"");
string path = filename.Substring(0, index);
path = path + @"" + xmlfilename + "file.xml";
FileStream mystream = File.Create(path);
mystream.Close();
XmlTextWriter mytw = new XmlTextWriter(path, Encoding.Default);
mydoc.Save(mytw);
mytw.Close();
}
Here, we are performing the typical ASP.NET 2.0 XML data operation-mainly using the XmlDocument class and the XmlTextWriter class together with a ready XML file template to construct an XML file which, due to the reason discussed before, will finally be stored at the root folder of the website. Since there is nothing more peculiar to be emphasized (you can find out much about XML operations in MSDN), we will leave the subject here, but it is highly recommended that you study it together with the ready template file named 'content.xml'-it's a pretty universal module that you can put to use elsewhere.
So much for the 'sending post' module. Next, let's continue to study the last part of this article-adding comments to the current article.