Steven Combs
Steven Combs

Reputation: 1939

Why is my GridView not updating when I click Update?

I am writing an asp.net application with a GridView. I have the Delete command working properly; however, the update command is not working right. I would really love to stay away from creating Template Fields for this situation as I am going to have about 5 different GridView's on the same page.

Here is my Markup code.

<asp:GridView ID="GridView1" runat="server" 
      DataSourceID="DataSource" AutoGenerateColumns="False" 
      DataKeyNames="ID" >
    <Columns>
      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
      <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="true" />
      <asp:BoundField DataField="Name" HeaderText="Name" />
      <asp:BoundField DataField="DefaultValue" HeaderText="DefaultValue" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="DataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MyApp.Web_App.Properties.Settings.DB %>" 
      SelectCommand="SELECT [ID], [Name], [DefaultValue] FROM [MyTable] WHERE ([MyID] = @MyID)" 
      DeleteCommand="DELETE FROM dbo.MyTable WHERE (ID = @ID)"
      UpdateCommand="UPDATE dbo.MyTable SET Name = @Name, DefaultValue = @DefaultValue WHERE (ID = @ID)" >

    <SelectParameters>
        <asp:SessionParameter Name="MyID" SessionField="My_ID" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
      <asp:Parameter Name="ADRoleToAttributeDefaultID" Type="Int32" />
      <asp:Parameter Name="ADAttrName" Type="String" />
      <asp:Parameter Name="ADAttrDefaultValue" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

The table populates with data and I can perform delete's on the table. I am just not sure why the updating is not working.

EDIT 1

I added the markup suggested but that still didn't work. How am I suppose to set the Parameter?

Upvotes: 1

Views: 302

Answers (1)

Samiey Mehdi
Samiey Mehdi

Reputation: 9424

Add UpdateParameters after SelectParameters:

<SelectParameters>
    <asp:SessionParameter Name="MyID" SessionField="My_ID" Type="Int32" />
</SelectParameters>

<UpdateParameters>
        <asp:Parameter Name="ID" Type="Int32" />
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="DefaultValue" Type="String" />
 </UpdateParameters>

Upvotes: 2

Related Questions