In an ASP.NET Application, you are using ListView, you are trying to delete a record. Update, Insert everything works fine but delete does not work. The SQL syntax is correct
DeleteCommand="delete from SalesRep where ID = @ID"
<asp:Parameter Name="ID" Type="Int32" />
You are using Eval or Bind with your parameter like this
Your ListView is declared like this
<asp:ListView ID="ListView2" runat="server" DataSourceID="sdsGetSalesRepListView" >
The error is coming from SQL not ASP.NET
Must declare the scalar variable “@ID”.
You will find that although the error is coming from SQL but the cause is ASP.NET ListView.
Why is this error?
First of all it is interesting to knote that this error is not comming from ASP.NET or C# code. This is comming from SQL. you can verify this by reading the actual exception. It will say SQL Exception. The problem here is that someone the @ID is not properly updated and blinded into the delete query, that causes this exception.
Solution in this case is really simple. Include DataKeyNames=”ID” parameter in your ListView declaration. That should address the problem. It is funny at the end of the day, the error seems to be in ASP.NET not SQL while we know the exception is certainly from SQL.
<asp:ListView ID="ListView2" runat="server"
DataSourceID="sdsGetManagersGridView" DataKeyNames="ID" >
Also note that you do not need to bind the ID field in your ASP.NET Code or even use Eval(“ID”). ID must simply exist in the select query. There is not need to display it in the ListView to make it work.
If it still does not solve your problem please let me know.