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 GridView
with a SQLDataSource
, alot of the work is done for you as long as you set up correctly.
First off, define your UpdateCommand
in the markup for the SQLDataSource
, like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Your Connection String Here"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT ColPK, ColA, ColB, ColC FROM Table_Name"
UpdateCommand="UPDATE Table_Name
SET ColA=@ColA, ColB=@ColB, ColC=@ColC,
WHERE ColPK=@ColPK">
</asp:SqlDataSource>
(ColPK
would 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).
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateEditButton="True" DataKeyNames="ColPK"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ColPK" HeaderText="Column PK"
SortExpression="ColA" ReadOnly="True" />
<asp:BoundField DataField="ColA" HeaderText="Column A"
SortExpression="ColA" />
<asp:BoundField DataField="ColB" HeaderText="Column B"
SortExpression="ColB" />
<asp:BoundField DataField="ColC" HeaderText="Column C"
SortExpression="ColC" />
</Columns>
</asp:GridView>
Now, you can still handle the OnRowUpdating
event in your code if you need to do extra processing, or cancel the Update
based on some logic, etc. But the basic Update functionality is pretty much for free!