简体   繁体   中英

SQL update is not working in asp.net with C#

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

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! ;)

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...))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM