0

I'm trying to use the selected value from a dropdown as a parameter in a update query to update the name for that selected item, but the update query doesn't work. The dropdown is bounded and extracts data from a sql table,the new name is captured from a textfield. Here is my code:

protected void UpdateName_Click(object sender, EventArgs e)
{
     using (SqlConnection con = new SqlConnection(cs))
     {
          SqlDataAdapter da = new SqlDataAdapter("update z_SignAssets set signAsset =@asset where signAssetID =@id",con);
          int dropdownValue = Convert.ToInt32(SignAssetDropDown.SelectedValue);

          da.SelectCommand.Parameters.AddWithValue("@asset", newNameTextField.Text); 
          da.SelectCommand.Parameters.AddWithValue("@id", dropdownValue);               
          UpdateName.Visible = false;
          newNameTextField.Visible = false;
          checkcost.Visible = true;
          EditName.Visible = true;
     }
 }
Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
  • Is it all of your code? In my knowledge this is not how SqlAdapter works. Did you read this https://msdn.microsoft.com/en-us/library/33y2221y(v=vs.110).aspx – wonderbell Apr 08 '15 at 16:59
  • This should definitely be outside of your sql using statements, they do not pertain to updating data but to UI elements: `UpdateName.Visible = false; newNameTextField.Visible = false; checkcost.Visible = true; EditName.Visible = true;` – Steven Mays Apr 08 '15 at 17:32
  • I got it solved, realized i was using selectCommand, the updateCommand with ExecuteNonQuery should be the one. Thanks, – user4731064 Apr 08 '15 at 17:34
  • And take a look at this: http://stackoverflow.com/a/1260961/855916 – Steven Mays Apr 08 '15 at 17:35

1 Answers1

0

You are using a SelectCommand instead of an UpdateCommand in your code. Also it could just be an SQLCommand. You do not need to use a SqlDataAdapter here. Also where are you executing non query or executing the command which is shown below with ExecuteNonQuery?

int dropdownValue = Convert.ToInt32(SignAssetDropDown.SelectedValue);

using (SqlConnection con = new SqlConnection(cs))
{        
    SqlCommand updateCommand = new SqlCommand("update z_SignAssets set signAsset =@asset where signAssetID =@id",con);

    updateCommand.Parameters.AddWithValue("@asset", newNameTextField.Text); 
    updateCommand.Parameters.AddWithValue("@id", dropdownValue);               
    updateCommand.ExecuteNonQuery();        
}
UpdateName.Visible = false;
newNameTextField.Visible = false;
checkcost.Visible = true;
EditName.Visible = true;
Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19