Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6979543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:54:02+00:00 2026-05-27T17:54:02+00:00

I am having a datalist showing posts and I added textbox and button for

  • 0

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'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:54:02+00:00Added an answer on May 27, 2026 at 5:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a primefaces dataList within a primefaces dataGrid but I'm having issue mapping
I have a file log that I would like to parse and am having
I am having trouble displaying my object in a datalist. My object loks like
I'm a newbie at ASP.NET and am having trouble styling a DataList. I've lately
I have a DataList that has a collection of People bound to it, with
I'm having problem getting all the values in datalist here is the problem: I
Here's the problem I'm having in my ASP.NET web-form project. I have Datalist which
Does anyone know how to add a simple search textbox on an ASP.NET Dynamic
Having a grails domain class, how to find the type of a property having
I'm having a hard time understanding how DataList select/unselect is supposed to work. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.