I am at the moment trying to learn how to use basic grid view controls to display my data.
To start with I have tried to do everything through the aspx page to try and keep things simpler for myself.
Everything is working great so far, but I am having a problem with using an update query.
My desired result is just a simple 2 column table, which will display a users name, and then an edit button.
The problem is, that if I don’t include the user ID in the columns the query does not update anything.
This is my SqlDataSource code:
<asp:SqlDataSource
ID="sqlDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:conn %>"
SelectCommand="SELECT UserID, Name FROM tblUsers;"
UpdateCommand="UPDATE tblUsers SET Name = @Name WHERE UserID = @UserID">
<UpdateParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="UserID" />
</UpdateParameters>
</asp:SqlDataSource>
And this is my GridView:
<asp:GridView
ID="Clients"
DataSourceID="sqlDataSource"
runat="server"
AllowPaging="true"
AllowSorting="true"
PageSize="25"
AutoGenerateColumns="False"
DataKeyNames="UserID">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Users Name" SortExpression="Name" />
<asp:CommandField EditText="Edit" ShowEditButton="true" />
</Columns>
</asp:GridView>
When I go to the page and click edit, the Name field changes to a text box, and I can choose either update or cancel as expected.
But with the code above, when I click update it just displays the normal table again, with no changes to the data because SQL just isn’t getting any UserID.
However if I add <asp:BoundField DataField="UserID" HeaderText="Users ID" SortExpression="UserID" /> to the <Columns>, it works.
Is there any way for me to make this work without having to add the UserID column to the table?
You can use the OldValuesParameterFormatString: