Mo T
Mo T

Reputation: 450

Event handling in datalist

I am having a datalist showing posts and I added textbox and button for adding a comment, but when I test the website and add a comment I find that the comment is added for the post correctly but a blank comment added to the rest of the posts...?

Anyone know how i can handle this? Here is my code:

protected void Button9_Click1(object sender, EventArgs e)
{
    foreach (DataListItem item in DataList2.Items)
    {
        TextBox TextBox1 = (TextBox)item.FindControl("TextBox1");
        string text = TextBox1.Text;
        Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
        string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("comment", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        int post_ID = Convert.ToInt32(post_IDLabel.Text);
        string comment = text;
        string email = Session["email"].ToString();
        int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
        cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
        cmd.Parameters.Add(new SqlParameter("@comment", comment));
        cmd.Parameters.Add(new SqlParameter("@myemail", email));
        cmd.Parameters.Add(new SqlParameter("@postID", post_ID));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }       
} 

and this is the comment procedure

CREATE PROC comment
@comment VARCHAR (100),
 @myemail VARCHAR (30),
 @postID INT,
 @course_ID INT
 AS
 IF @myemail IN (SELECT student_email FROM Students_Subscribes_Course_pages WHERE subscribed = 1) 
 OR @myemail IN (SELECT added_email FROM Lecturers_Adds_Academics_Course_page WHERE course_ID = @course_ID) 
 AND @postID IN (SELECT post_ID FROM Posts WHERE @postID = @postID) OR @myemail = 
 (SELECT page_creator FROM Course_pages WHERE course_ID = @course_ID) AND @postID IN 
 (SELECT post_ID FROM Posts)
 INSERT INTO Members_Comments_Posts (commenter,post_ID, comment_content)
 VALUES (@myemail, @postID, @comment)
 ELSE 
PRINT 'sorry, you are not subscribed or post not found'

Upvotes: 0

Views: 264

Answers (1)

competent_tech
competent_tech

Reputation: 44921

The problem is that you execute the same code for every item in the list rather than those items that had comments left for them or the currently selected item.

One solution to this is to see if the text is set before executing the database code.

Here is how I would rewrite the loop:

foreach (DataListItem item in DataList2.Items)
{
    TextBox TextBox1 = (TextBox)item.FindControl("TextBox1");
    string text = TextBox1.Text;
    if (!string.IsNullOrEmpty(text)) {
      Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
      string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
      SqlConnection conn = new SqlConnection(connStr);
      SqlCommand cmd = new SqlCommand("comment", conn);
      cmd.CommandType = CommandType.StoredProcedure;
      int post_ID = Convert.ToInt32(post_IDLabel.Text);
      string comment = text;
      string email = Session["email"].ToString();
      int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
      cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
      cmd.Parameters.Add(new SqlParameter("@comment", comment));
      cmd.Parameters.Add(new SqlParameter("@myemail", email));
      cmd.Parameters.Add(new SqlParameter("@postID", post_ID));
      conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();
   }
}

This will ensure that any comments on any posts will be updated, but you may need to update the logic to ensure that only the selected post is updated.

Also, you should move your connection open/close and command creation out of the loop in order to be more efficient.

Upvotes: 2

Related Questions