Shalu S
Shalu S

Reputation: 11

GridView control fail to find the id when update

I'm writing the following code to update SQL Server. I received the following error message:

Incorrect syntax near '-'. Must declare the scalar variable "@id". Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '-'. Must declare the scalar variable "@id".

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SqlException (0x80131904): Incorrect syntax near '-'. Must declare the scalar variable "@id".]

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None" AllowSorting="True">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="E-mail" HeaderText="E-mail" SortExpression="E-mail" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#2461BF" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:demoConnectionString4 %>" 
        SelectCommand="SELECT * FROM [de]" 
        UpdateCommand="UPDATE [de] SET [Name]=@Name, [E-mail]=@column1 WHERE [id]=@id">
        <UpdateParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="Name" Type="String"/>
            <asp:Parameter Name="column1" Type="String"/>
        </UpdateParameters>
    </asp:SqlDataSource>

Upvotes: 0

Views: 198

Answers (1)

Aristos
Aristos

Reputation: 66641

The error is on the ID TemplateField, you use a Label on EditItemTemplate to show the ID, and this label is not post back that ID that is needed for the sql update. The asp:label is not render an input field that is needed here.

Either remove the TemplateField, and just use as the rest fields an asp:BoundField that automatically creates input fields when you update each line, either you make it a read only text box (on EditImteTemplate) so can post the id.

This lines are what you need to focus and change:

<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id">
    <EditItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 1

Related Questions