So I am having an issue with doing an update in a Gridview during an OnRowUpdating event.
What I am trying to do is set the UpdateCommand in an SqlDataSource then update using that command. The event is firing okay, but when the event is done it appears that the row never updates.
C#:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlDataSource1.UpdateCommand = "UPDATE A_Table SET Something = @AValue WHERE ID = " + e.RowIndex;
SqlDataSource1.Update();
}
Edit: Re-wrote my example update command…it is really an update command, not a select command haha.
The other answers here are VERY correct in pointing out the issue with your
UpdateCommand.As I mentioned in the comment earlier, I’ll just point out that this is a bit easier than what you have there. When using a
GridViewwith aSQLDataSource, alot of the work is done for you as long as you set up correctly.First off, define your
UpdateCommandin the markup for theSQLDataSource, like this:(
ColPKwould be the primary key of the table you’re updating)Then, you can set “AutoGenerateEditButton” to true in your GridView markup and, poof! You can update the
GridView(without having to do anything in code behind).Now, you can still handle the
OnRowUpdatingevent in your code if you need to do extra processing, or cancel theUpdatebased on some logic, etc. But the basic Update functionality is pretty much for free!