Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView DeleteCommand Asp.net

Status
Not open for further replies.

eilob

Programmer
Mar 28, 2007
54
IE
Hi all, I am using Visual Studio 2005, I have created a web application that uses a Grid View with a Delete, Update, insert commmand field. But I am getting the following error when I try to delete a record:

Must declare the variable '@FAQ1ID'.
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: Must declare the variable '@FAQ1ID'.

This is my code:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Edit1.ascx.cs" Inherits="CMSWebParts_MyWebParts_Edit1" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
AutoGenerateSelectButton="True" DataSourceID="SqlDataSource1" DataKeyNames="FAQ1ID">
<Columns>
<asp:BoundField DataField="FAQ1ID" HeaderText="FAQ1ID" InsertVisible="False" ReadOnly="True"
SortExpression="FAQ1ID" />
<asp:BoundField DataField="FormInserted" HeaderText="FormInserted" SortExpression="FormInserted" />
<asp:BoundField DataField="Area" HeaderText="Area" SortExpression="Area" />
<asp:BoundField DataField="Topic" HeaderText="Topic" SortExpression="Topic" />
<asp:BoundField DataField="Issue" HeaderText="Issue" SortExpression="Issue" />
<asp:BoundField DataField="Update" HeaderText="Update" SortExpression="Update" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="OverwriteChanges"
ConnectionString="<%$ ConnectionStrings:pSCEMEAWEB_BConnectionString4 %>"
DeleteCommand="DELETE FROM [TBL_FAQ1] WHERE [FAQ1ID] = @FAQ1ID AND [FormInserted] = @original_FormInserted AND [Area] = @original_Area AND [Topic] = @original_Topic AND [Issue] = @original_Issue AND [Update] = @original_Update"
InsertCommand="INSERT INTO [TBL_FAQ1] ([FormInserted], [Area], [Topic], [Issue], [Update]) VALUES (@FormInserted, @Area, @Topic, @Issue, @Update)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [FAQ1ID], [FormInserted], [Area], [Topic], [Issue], [Update] FROM [TBL_FAQ1] WHERE ([Area] = @Area)"
UpdateCommand="UPDATE [TBL_FAQ1] SET [FormInserted] = @FormInserted, [Area] = @Area, [Topic] = @Topic, [Issue] = @Issue, [Update] = @Update WHERE [FAQ1ID] = @original_FAQ1ID AND [FormInserted] = @original_FormInserted AND [Area] = @original_Area AND [Topic] = @original_Topic AND [Issue] = @original_Issue AND [Update] = @original_Update">
<SelectParameters>
<asp:parameter DefaultValue="CXP" Name="Area" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:parameter Name="original_FAQ1ID" Type="Int32" />
<asp:parameter Name="original_FormInserted" Type="DateTime" />
<asp:parameter Name="original_Area" Type="String" />
<asp:parameter Name="original_Topic" Type="String" />
<asp:parameter Name="original_Issue" Type="String" />
<asp:parameter Name="original_Update" Type="String" />

</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="FormInserted" Type="DateTime" />
<asp:parameter Name="Area" Type="String" />
<asp:parameter Name="Topic" Type="String" />
<asp:parameter Name="Issue" Type="String" />
<asp:parameter Name="Update" Type="String" />
<asp:parameter Name="original_FAQ1ID" Type="Int32" />
<asp:parameter Name="original_FormInserted" Type="DateTime" />
<asp:parameter Name="original_Area" Type="String" />
<asp:parameter Name="original_Topic" Type="String" />
<asp:parameter Name="original_Issue" Type="String" />
<asp:parameter Name="original_Update" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="FormInserted" Type="DateTime" />
<asp:parameter Name="Area" Type="String" />
<asp:parameter Name="Topic" Type="String" />
<asp:parameter Name="Issue" Type="String" />
<asp:parameter Name="Update" Type="String" />
</InsertParameters>
</asp:SqlDataSource>



Thanks in advance!
 
Your parameter in your Delete query should be called @original_FAQ1ID instead of @FAQ1ID.

However, you really should look at using a data layer instead of the built in SqlDataSource as it will only cause you problems (like in this instance where you couldn't debug to find the issue).

Mark,

[URL unfurl="true"]http://lessthandot.com[/url] - Experts, Information, Ideas & Knowledge
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Website Design
[URL unfurl="true"]http://aspnetlibrary.com[/url] - An online resource for professional ASP.NET developers
 
Thanks Mark, that works now

Now my next problem is the UpdateCommand, nothing happens when I update. Retrieves the same data without any change.
Any ideas?


UpdateCommand="UPDATE [TBL_FAQ1] SET [FormInserted] = @FormInserted, [Area] = @Area, [Topic] = @Topic, [Issue] = @Issue, [Update] = @Update WHERE [FAQ1ID] = @original_FAQ1ID AND [FormInserted] = @original_FormInserted AND [Area] = @original_Area AND [Topic] = @original_Topic AND [Issue] = @original_Issue AND [Update] = @original_Update">
 
Not sure how to do this, do you have any examples?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top