Adding the NewComment.aspx Page
The next page we need to add is the NewComment page that we created a link to on the Post page. This page will allow you to add a comment to a given post, using the PostId passed via a query string. The NewComment page will be similar to the NewPost page in that it will contain a LoginView which will show a Login Control to anonymous users and show a simple form for users who are logged in. To begin:
  1. Right click the project in your Solution Explorer.
  2. Select Add New Item…
  3. Select a Web Form and name it NewComment.aspx.
  4. Ensure that the Select master page checkbox is checked and click Add.
  5. Select the MasterPage.master we added earlier and click OK.
  6. Open the NewComment.aspx page in Design mode.
  7. Drag and drop a LoginView Control into the ContentPlaceHolder1 on the Web Form.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!
The next thing we need to do here is add some content to the AnonymousTemplate of our LoginView Control. To do this:
  1. Expand the LoginView Tasks menu.
  2. Select AnonymousTemplate in the Views DropDownList.
  3. Drag and drop a Login Control into the editable area of the LoginView Control.
Next, we need to add in our simple form for the user, if they are logged in, to fill out to add a new comment. To do this:
  1. Expand the LoginView Tasks menu.
  2. Select LoggedInTemplate in the Views DropDownList.
  3. Select the editable area of the LoginView Control.
  4. Insert a new table that has 2 rows and 2 columns, and choose not to specify a width.
  5. In the top left cell, type in Message:.
  6. In the top right cell, drag and drop a TextBox Control.
    1. Change the ID property of the TextBox to txtMessage.
    2. Set the TextMode property to MultiLine.
    3. Set the Rows property to 6.
    4. Set the Columns property to 40.
  7. Drag and drop a RequiredFieldValidator to the right of the txtMessage TextBox.
    1. Change the ID property to rfvMessage.
    2. Set the ErrorMessage property to Message is required.
    3. Set the Text property to *.
    4. Set the ControlToValidate property to txtMessage.
  8. Select the two cells in the bottom row and merge them.
  9. Set the alignment of the bottom row to center.
  10. Drag and drop a Button Control into the bottom row.
    1. Change the ID property of the Button to btnSubmit.
    2. Set the Text property of the Button to Submit.
  11. Drag and drop a ValidationSummary Control underneath the btnSubmit Button.
You should now have a simple form for adding a new comment that looks something like this:
Next, quickly load up the website and navigate to the new comment page. Test the validation by clicking the submit button when the message TextBox is empty and ensuring that you receive an error message. Then, do the same with some text in the box to make sure the error message doesn’t show up. This will ensure that we won’t add any invalid data into our database like blank comments. Next, we need to add the code in the event that the Submit button is clicked. To do this, double click the Submit button to generate the button click event method.
Then, in the btnSubmit_Click event method we need to add the following code:
Code Block
NewComment.aspx.cs
The btnSubmit_Click event method
//setup data to insert into the comment table
string commentUserName = Page.User.Identity.Name;
string commentMessage = ((TextBox)LoginView1.FindControl("txtMessage")).Text;
DateTime commentDateTime = DateTime.Now;
int postId = int.Parse(Request.QueryString["id"]);
 
//Add appropriate data to the post table
//setup our connection to the db
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ForumDBConnectionString"].ConnectionString);
//create our sql command object
SqlCommand cmd = new SqlCommand("Insert INTO Comments(PostId, CommentUserName, CommentMessage, CommentDateTime) VALUES (@PostId, @CommentUserName, @CommentMessage, @CommentDateTime)", conn);
 
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@PostId", postId);
cmd.Parameters.AddWithValue("@CommentUserName", commentUserName);
cmd.Parameters.AddWithValue("@CommentMessage", commentMessage);
cmd.Parameters.AddWithValue("@CommentDateTime", commentDateTime);
 
using (conn)
{
    conn.Open();
    cmd.ExecuteNonQuery();
}
 
Response.Redirect("Post.aspx?id=" + postId.ToString());
Let’s review what this code is actually doing. First, we setup all of our data to make a new comment, commentUserName, commentMessage from the txtMessage box, commentDateTime using DateTime.Now, and the postId we got via a query string.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
Next, we setup our connection to the database and create a SQL command to insert a new entry into our Comments table inserting PostId, CommentUserName, CommentMessage, and CommentDateTime respectively. Then, we configure the parameters with the data that we setup earlier and send the query.
Finally, we redirect them back to the Post page, with the corresponding postId so that they can view their new post on that page.