0

"Update" button is returning all nulls.

I click "Edit" and everything displays ok.

When I click "Update" it makes Columns: datetime, col1, col2 and col3 all nulls on the screen and in the "MDF".

The "Delete" works fine.

The "mdf" file is working correctly with the correct connection string.

It is probably just syntax.

Could someone take a look.

<asp:GridView ID="GridView1" 
    runat="server"
    DataSourceID="SqlDataSource1"
    AutoGenerateColumns="false"
    DataKeyNames="idt"
    AutoGenerateEditButton="true" 
    AutoGenerateDeleteButton="true"
    showfooter="true">
    <Columns>
        <asp:BoundField DataField="idt" HeaderText="idt" Readonly="true" SortExpression="idt" />
        <asp:BoundField DataField="datetime" HeaderText="datetime" SortExpression="datetime" />
        <asp:TemplateField SortExpression="col1" HeaderText="col1">
                <ItemTemplate>
                    <asp:TextBox ID="txt1" runat="server" Text='<%# Eval("col1") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:textbox id="col1TextBox" text='<%#Eval("col1")%>' runat="server"/>
                </EditItemTemplate>
       </asp:TemplateField>
        <asp:TemplateField SortExpression="col2" HeaderText="col2">
                <ItemTemplate>
                    <asp:TextBox ID="txt2" runat="server" Text='<%# Eval("col2") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:textbox id="col2TextBox" text='<%#Eval("col2")%>' runat="server"/>
                </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField SortExpression="col3" HeaderText="col3">
                <ItemTemplate>
                    <asp:TextBox ID="txt3" runat="server" Text='<%# Eval("col3") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:textbox id="col3TextBox" text='<%#Eval("col3")%>' runat="server"/>
                </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField SortExpression="rowtotal" HeaderText="Row Total">
                <ItemTemplate>
                    <asp:TextBox ID="txtrowtot" runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:textbox id="rowtotTextBox" runat="server"/>
                </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource
    id="SqlDataSource1"
    ConnectionString="<%$ ConnectionStrings:Total %>"
    SelectCommand="SELECT * FROM [test];"
    UpdateCommand="UPDATE [test] SET [datetime] = @datetime, [col1] = @col1, [col2] = @col2, [col3] = @col3 WHERE [idt] = @idt;"
    DeleteCommand="DELETE FROM [test] WHERE [idt] = @idt;"
    InsertCommand="INSERT INTO [test] [datetime], [col1], [col2], [col3] VALUES @datetime, @col1, @col2, @col3;"
    runat="server">
    <UpdateParameters>
        <asp:Parameter Name="idt" Type="Int32" />
        <asp:Parameter Name="datetime" Type="String" />
        <asp:Parameter Name="col1" Type="Double" />
        <asp:Parameter Name="col2" Type="Double" />
        <asp:Parameter Name="col3" Type="Double" />
    </UpdateParameters>
    <DeleteParameters>
        <asp:Parameter Name="idt" Type="Int32" />
    </DeleteParameters>
</asp:SqlDataSource>
noviscientia
  • 17
  • 1
  • 6

1 Answers1

0

The issue is with TemplateFields, they don't get passed to the SqlDataSource automatically. You must write code behind in RowUpdating event of the GridView to pass the values to each of the parameters referred by the update command. Refer to the answer given in this forum

Sam
  • 2,917
  • 1
  • 15
  • 28
  • Also see this thread in SO for another example. http://stackoverflow.com/questions/13938256/updating-gridview-with-sqldatasource-in-asp-net – Sam Aug 31 '14 at 23:57