1

I wanted to update the cell of the table, but when I did that by writing this code, the table does not get updated and there was no any exceptions or any errors:

protected void btn_Click(object sender, EventArgs e)
{
    using(SqlConnection con = new SqlConnection("My connection string"))
    {
        SqlCommand command = new SqlCommand("UPDATE [Tablename] SET [notes]=@notes WHERE [ID]=@ID", con);
        command.Parameters.AddWithValue("@ID", this.ID);
        command.Parameters.AddWithValue("@notes", TextBox1.Text);

        con.Open();
        command.ExecuteNonQuery();
        con.Close();
    }
}

When I pass the value of "@notes" by myself it works, but when pass the value of "@notes" TextBox1.Text it is not working. Can someone explain me why?

Note: TextBox1.TextMode is Multiline

7
  • What is the value of TextBox1.Text exactly? And using con.Close() is redundant. using statement will handle that. Commented Mar 2, 2014 at 14:59
  • the value of TextBox1.Text is any type of text like "hello world and etc etc" Commented Mar 2, 2014 at 15:00
  • Usual reason is there's something wrong with ID. PS con.close is unnecessary. Commented Mar 2, 2014 at 15:02
  • Or on occasion the db you think your are connected to isn't the one you are connected to. Commented Mar 2, 2014 at 15:03
  • 1
    No errors = id doesn't exist, or no change to notes. That's it. int rowsAffected = command.ExecuteNonQuery() might tell you something. Commented Mar 2, 2014 at 15:07

2 Answers 2

2

Thanks to all of you. That was my fault, I've tried to do what @Tony Hopkinson said in the comment and I get the value of 1(that meant everything is going good). The problem was in Page_Load function, I've changed it:

protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.Text = info;
}

To this and everything worked:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        TextBox1.Text = info;
    }  
}

Then I've checked the table the value was updated. One more time thanks to all of you...))

Sign up to request clarification or add additional context in comments.

Comments

1

I recommend to save the "notes" textBox1 in a string. If it´s multiline use something like String.Join, and then pass the string as the parameter ;) It will works!

BTW.. It´s not neccessary to close the connection with the using SqlConnection, because it will Dispose by itself! ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.