Click here to Skip to main content
16,022,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir,
I am getting this ERROR while updating or deleting data in gridview.
“Must declare scalar variable @id”.
Delete command for example: delete command=”DELETE FROM [table]” WHERE [id]=@id”
I saw the above delete command in source of the page.
Please tell me where to declare @id? Please solve the problem in details. I have a table with 3 columns id (int), name (varchar) & email (varchar).
This is a web based gridview where I true Autogenerate property of edit and delete.
Regards.
Hasan

What I have tried:

C#
Sir,
I am getting this ERROR while updating or deleting data in gridview.
"Must declare scalar variable @id".
Delete command for example: delete command="DELETE FROM [table]" WHERE [id]=@id" 
I saw the above delete command in source of the page.
Please tell me where to declare @id? Please solve the problem in details. I have a table with 3 columns id (int), name (varchar) & email (varchar).
This is a web based gridview where I true Autogenerate property of edit and delete.
Regards.
Hasan
Posted
Updated 15-Dec-16 18:20pm

1 solution

Hi,

Have you create update and Delete parameters. Have you pass dataKeyNames ? As you have ID in your table so DataKeyNames must be ID. Please refer following code.
In code, you can see parameters for update and delete like

ASP.NET
<UpdateParameters>
                <asp:Parameter Name="Mobile" />
                <asp:Parameter Name="Email" />
            </UpdateParameters>
            
            <DeleteParameters>
                <asp:Parameter Name="EmpID" /> 
            </DeleteParameters>

Full Code as below
HTML
<html>
<head>
    <title>GridView Edit and Delete Row</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView
            ID="GridView1" Font-Names="Arial" Font-Size="Smaller" 
            AutoGenerateColumns="False" 
            AutoGenerateEditButton="True" 
            AutoGenerateDeleteButton="False" 
            DataSourceID="SqlDataSource1" 
            DataKeyNames="EmpID"
            runat="server">
            
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%--ADD THE DELETE LINK BUTTON--%>
                        <asp:LinkButton Runat="server" 
                            OnClientClick ="return confirm('Are you sure you?');"
                            CommandName="Delete">Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            
                <%--DATA BOUND COLUMNS--%>
                <asp:BoundField DataField="EmpID" HeaderText="EmpID" 
                    SortExpression="EmpID" ReadOnly="true" />
                <asp:BoundField DataField="EmpName" HeaderText="EmpName" 
                    SortExpression="EmpName" ReadOnly="true" />
                <asp:BoundField DataField="Mobile" HeaderText="Mobile" 
                    SortExpression="Mobile" />
                <asp:BoundField DataField="Email" HeaderText="Email" 
                    SortExpression="Email" />
            </Columns>
        </asp:GridView>
        
        <%--THE SQL DATA SOURCE CONNECTED WITH THE GRIDVIEW--%>
        <asp:SqlDataSource
            ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:DNA_CLASSIFIEDConnectionString %>"
            SelectCommand="SELECT [EmpID], [EmpName], [Mobile], [Email] FROM [EmployeeDetails]"
            UpdateCommand="UPDATE EmployeeDetails SET Mobile = @Mobile, Email = @Email 
                WHERE EmpID = @EmpID"
            DeleteCommand="DELETE FROM EmployeeDetails WHERE EmpID = @EmpID">
           
            <UpdateParameters>
                <asp:Parameter Name="Mobile" />
                <asp:Parameter Name="Email" />
            </UpdateParameters>
            
            <DeleteParameters>
                <asp:Parameter Name="EmpID" /> 
            </DeleteParameters>
            
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Please refer link(GridView Edit, Update and Delete Example - Simple GridView Example with Row Edit and Delete Options - C# and Vb.Net[^]) for detail knowledge.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900