I don't really know how to check why it's not working.
I have a try/catch that tells me in the catch that it didn't succeed but not sure how to really know what's going on with it.
Here's my update attempt:
SqlCommand comm = new SqlCommand("UPDATE inventoryTbl SET categoryID = '@category' WHERE id = '@ID'", conn);
comm.Parameters.Add("@category", System.Data.SqlDbType.SmallInt);
comm.Parameters["@category"].Value = txtbx_itemCategory.Text;
try
{
// Execute the command
comm.ExecuteNonQuery();
// Reload page if the query executed successfully
//Response.Redirect("addrelatives.aspx");
}
catch
{
// Display error message
bttnApplyChanges.Text = "Error";
}
finally
{
// Close the connection
conn.Close();
}
Loading
foo fooliosPosted Oct 4, 2007, 11:22 PM
I was able to correct it as a result.
Thanks again!
Jan MontanoPosted Oct 4, 2007, 10:10 PM
SqlCommand comm = new SqlCommand("UPDATE inventoryTbl SET categoryID = '@category' WHERE id = '@ID'", conn);
should be instead
SqlCommand comm = new SqlCommand("UPDATE inventoryTbl SET categoryID = @category WHERE id = @ID", conn);
You don't have to enclose the @param in quotes. Enclosing it in quotes will be interpreted as comparing it to the '@category' or '@ID' strings literally.
if you think comm.ExecuteNonQuery(); will result in an error if no update was made, it won't. However, ExecuteNonQuery() for update queries returns an int value for the number of rows affected. A zero (0) return means no update was made. You could use this one to check if an update was made or not.
int rowsAffected = comm.ExecuteNonQuery();
You also have to add your @ID parameter.