user1955449
user1955449

Reputation:

update query not updating anything in the DB

I am having a problem that the update query is not working. It does not update anything in my database. Please tell why is it not working

private void button1_Click(object sender, EventArgs e)
{
    string title = this.textBox1.Text;
    string descri = this.richTextBox1.Text;
    string connstring = 
        @"Data Source=(LocalDB)\v11.0;AttachDbFilename=<path>\Database1.mdf;Integrated Security=True";
    string query = "update diaryDB set Title=@title, Description=@descri where Description=@descri";
    SqlConnection con = new SqlConnection(connstring);
    SqlCommand com = new SqlCommand(query, con);
    SqlParameter p1 = new SqlParameter("@title" , title);
    SqlParameter p2 = new SqlParameter("@descri", descri);
    com.Parameters.Add(p1);
    com.Parameters.Add(p2);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
}

Upvotes: 1

Views: 923

Answers (3)

StarPilot
StarPilot

Reputation: 2272

This is most likely your problem:

string query = "update diaryDB set Title=@title, Description=@descri where Description=@descri";

You are instructing the database to find all rows whose field Description equals the passed in parameter @descri. However, you are trying to also update the field Description to the passed in parameter @descri. This suggests you are trying to change the description value in the row to a new value. I suspect that this new value is not currently in the table you are operating on, and therefore no updates are being made.

The other trouble you can have is that you are setting the field Title to the value it is already holding.

If you are trying to update the description field in a particular row which you find by the looking for the original value, then you should probably be using something like this:

string query = "update diaryDB set Title=@title, Description=@descri where Description=@OriginalDescri";

And then add a third parameter that holds the original description value (the current value for the field in the database).

So check that the value you are setting Title to is different than the existing value in the database, and make sure if you are using Description to find/identify the target row(s) in the database, that you are searching for the original Description value and not the new value you want to set Description to.

Upvotes: 0

Ntellect13
Ntellect13

Reputation: 331

In place of:

SqlParameter p2 = new SqlParameter("@descri", descri);

I put something that matches what is in your table. Ex:

SqlParameter p2 = new SqlParameter("@descri", "Something I know is in the table");

Then check to see if your title is updated in that row. This will tell you if your text in richTextBox1 is incorrect. Also keep in mind that a RichTextBox has different formatting than a regular TextBox which could be effecting it.

Upvotes: 0

Derek
Derek

Reputation: 8753

Title=@title , Description=@descri where Description=@descri"

You are setting the Description = @descri when it already = @descri

Upvotes: 1

Related Questions