0

I have this code that works fine:

protected void ButtonImgTextToDB_Click(object sender, EventArgs e)
   {
       string ID = TextBoxStartPictID.Text;
       string Rubrik = TextBoxHeadLineStart.Text;
       string Text = TextBoxTextStart.Text;

       SqlConnection conn = new SqlConnection(config);
       conn.Open();
       string sql = "UPDATE StartImage SET Headline ='" + Rubrik +
"', Text ='" + Text + "' WHERE ID ='" + ID + "'";
       SqlCommand comm = new SqlCommand(sql, conn);
       SqlDataReader dr = comm.ExecuteReader();

       //messagebox
       //clear textboxes

       conn.Close();
}

And then I have this code that I'd rather use, with parameters, for the exact same function. This code does not work (no error message, it's just that it does nothing). How is that?:

protected void ButtonImgTextToDB_Click(object sender, EventArgs e)
   {
SqlConnection conn = new SqlConnection(config);
       conn.Open();
       string sql = "UPDATE StartImage SET Headline = @Headline,
Text= @Text WHERE ID ='" + ID + "'";
       SqlCommand comm = new SqlCommand(sql, conn);

       SqlParameter param1 = new SqlParameter("@Headline",
SqlDbType.VarChar, 50);
       param1.Value = TextBoxHeadLineStart.Text;
       comm.Parameters.Add(param1);

       SqlParameter param2 = new SqlParameter("@Text", SqlDbType.VarChar, 300);
       param2.Value = TextBoxTextStart.Text;
       comm.Parameters.Add(param2);

       //messagebox
       //clear textboxes

       comm.ExecuteReader();
       conn.Close();
   }
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by BonaDrag
0

try two things.
first: try to make the id parameter in the same manner as the others.
second: instead of using the ExecuteReader method use ExecuteNonQuery method. that returns the number of rows affected by the query.

regards.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.