1

I have some textboxes that are populated with customer info from a database (name, address) on page load. I want users to be able to simply change this and click a button to update their personal information.

I have the following code that runs on button click:

con.Open();
string query = "UPDATE CustomerInfo SET FirstName=@FirstName, LastName=@LastName, Address=@Address, PostalCode=@PostalCode, PostalName=@PostalName WHERE UserName=@UserName";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
cmd.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;
cmd.Parameters.AddWithValue("@Address", SqlDbType.VarChar).Value = txtAddress.Text;
cmd.Parameters.AddWithValue("@PostalCode", SqlDbType.Int).Value = txtPostalCode.Text;
cmd.Parameters.AddWithValue("@PostalName", SqlDbType.VarChar).Value = txtPostalName.Text;
cmd.Parameters.AddWithValue("@UserName", SqlDbType.VarChar).Value = UserName;
cmd.ExecuteNonQuery();
con.Close();

This does not work. If I change the textbox to something different and click the button, nothing happens. The table is not updated with the new values, and I get no error messages. However, if I change the Parameter values from for example txtFirstName.Text to "Bob", it successfully updates the FirstName column in my table to Bob.

Not sure what I'm missing, but for some reason using textbox.Text as value doesn't work.

Boxiom
  • 2,245
  • 6
  • 40
  • 51
  • 1
    Are you resetting the textbox value some where in the page_load/init before a button click? – Murali Murugesan May 09 '14 at 14:07
  • 1
    As @Murali says this is almost certainly an issue with setting the text of those textboxs somewhere else in the page lifecycle. I highly recommend reading up on the [page lifecycle](http://stackoverflow.com/questions/8457297/asp-net-page-life-cycle-explanation) and where event handlers factor into it. –  May 09 '14 at 14:11
  • Yeah, I'm setting the text with some information from a database on page_load. I assumed textbox.Text would be automaticly updated when I changed the textbox content. I was hoping there would be an easy way to deal with this, but I guess I'll take a look at page lifecycle. Thanks. – Boxiom May 09 '14 at 14:24

1 Answers1

2

AddWithValue takes parameter value as a second parameter, not SqlDbType.

You are mixing it with Add method.

cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;

Also use using statement to dispose your SqlConnection and SqlCommand.

EDIT: Ok, that's not the point as you said (And I can't delete my answer because of 5 answer delete limit). I really doubt that your textbox values change in your page life-cycle. By the way, where did you defined these code? Be sure that your textbox values keep their values since these codes were execute.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • If you read OP specifically `However, if I change the Parameter values from for example txtFirstName.Text to "Bob", it successfully updates the FirstName column in my table to Bob`. Looks like current code has problem with the text box values – Murali Murugesan May 09 '14 at 14:08
  • @Murali You are right. But I can't delete this answer because I had 5 delete answer limit for today. You can downvote it. – Soner Gönül May 09 '14 at 14:10