0

Hi,

I have a simple grid view that i want to be able to delete rows from by clicking the delete button that's automatically created.

Every time I click on the delete button to delete the row i get the following error:

Incorrect syntax near 'int'.

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 'int'.

When I look at the stack trace the only part mentioning 'int is:

System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +714

I'm new to using asp so this could be a simple problem hopefully .

Thanks for any help

6
Contributors
7
Replies
11
Views
7 Years
Discussion Span
Last Post by crishjeny
0

I believe the error is occuring from your SQL Delete statement/query. Can you please put some line of your code ?

Hi,

I have a simple grid view that i want to be able to delete rows from by clicking the delete button that's automatically created.

Every time I click on the delete button to delete the row i get the following error:

Incorrect syntax near 'int'.

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 'int'.

When I look at the stack trace the only part mentioning 'int is:

System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +714

I'm new to using asp so this could be a simple problem hopefully .

Thanks for any help

0

there are to reasons behind this

if parent child relationship

or

passing an invalid parameter..

or an Syntax error.

your case is Syntax error ...show me your code

0

Sorry for late reply. Here is my code

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="Product ID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="Product ID" HeaderText="Product ID" ReadOnly="True" 
                    SortExpression="Product ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Format" HeaderText="Format" 
                    SortExpression="Format" />
                <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                <asp:BoundField DataField="Genre" HeaderText="Genre" SortExpression="Genre" />
                <asp:BoundField DataField="Publisher" HeaderText="Publisher" 
                    SortExpression="Publisher" />
                <asp:BoundField DataField="Stock" HeaderText="Stock" SortExpression="Stock" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:gameshopConnectionString %>" 
            DeleteCommand="DELETE FROM [games] WHERE [Product ID] = @Product_ID" 
            InsertCommand="INSERT INTO [games] ([Product ID], [Title], [Format], [Price], [Genre], [Publisher], [Stock]) VALUES (@Product_ID, @Title, @Format, @Price, @Genre, @Publisher, @Stock)" 
            SelectCommand="SELECT * FROM [games]" 
            UpdateCommand="UPDATE [games] SET [Title] = @Title, [Format] = @Format, [Price] = @Price, [Genre] = @Genre, [Publisher] = @Publisher, [Stock] = @Stock WHERE [Product ID] = @Product_ID">
            <DeleteParameters>
                <asp:Parameter Name="Product_ID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Format" Type="String" />
                <asp:Parameter Name="Price" Type="String" />
                <asp:Parameter Name="Genre" Type="String" />
                <asp:Parameter Name="Publisher" Type="String" />
                <asp:Parameter Name="Stock" Type="Int32" />
                <asp:Parameter Name="Product_ID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Product_ID" Type="Int32" />
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Format" Type="String" />
                <asp:Parameter Name="Price" Type="String" />
                <asp:Parameter Name="Genre" Type="String" />
                <asp:Parameter Name="Publisher" Type="String" />
                <asp:Parameter Name="Stock" Type="Int32" />
            </InsertParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Edited by naka1888: n/a

0

I may be completely wrong here but I think your entire problem boils down to this row of code:

<asp:BoundField DataField="Product ID" HeaderText="Product ID" ReadOnly="True" SortExpression="Product ID" />

More specifically to the ReadOnly="True" statement within that row.

This reference at msdn.microsoft.com is similar in nature to yours but different in the way they bound the data within the GridView however what it boiled down to on their end (for the same error and with the same underlying error code) was that they needed to use the equivalent of EVAL instead of BIND for their read-only GridView segments to avoid this issue occurring.

Not sure how to translate that into the <asp:BoundField...> method you're using but it might lead you in the right direction.

Hope this helps :) Please remember to mark threads solved once your issue is resolved.

0

Hi thanks everyone for their help. I just took out the space in product id and changed it from an int to varchar in the database and it seemed to work.

-1

Listen Dude u Cant Delete From grid view just by firing the delete event u
eg

GridView.DeleteRow(index)//This is wrong
U hv 2 delete it frtm back end dats frm the database
And then call the Gridview.Databind() to reflect the Change.


IF u still hv prob Revert Back 2 ME i will surely solve ur Query

Edited by Mr.Akira: n/a

Votes + Comments
lrn2spl wtb dictionary?
0

Hi

To get the solution of delete row from Gridview .you can refer below code.

In this code customer is one type of table.

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
customer.Delete(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
FillCustomerInGrid();
}

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.