ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Exception - Variable names must be unique within a query batch or stored procedure
Author Name: Mudassar Khan Published Date: May 22, 2009
Filed Under :
Issues and Exceptions
Views: 1980

Server Error in '/ADO.Net' Application.


The variable name '@param' has already been declared. Variable names must be unique within a query batch or stored procedure.

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: The variable name '@param' has already been declared. Variable names must be unique within a query batch or stored procedure.

 

Cause

This error occurs when SQL server finds two variables or parameters of same name and hence the ADO.Net class throws the above exception.

 

Solution

1. If you are running a loop and using the same command object to insert data add this statement

cmd.Parameters.Clear()

 

2.  Check if you have not passed the same parameter twice like

cmd.Parameters.AddWithValue("@city", txtCity.Text.Trim());
cmd.Parameters.AddWithValue("@city", txtCity.Text.Trim());

 

3. If using stored procedure check if you have duplicate variables if you found remove them.

For example

DECLARE @CustID varchar(10)

SET @CustID= 'ALFKI'

SELECT *FROM Customers WHERE CustomerID = @CustID

DECLARE @CustID varchar(50)


The above query will give the same error.

If anyone else some other solutions that helped them, please share.


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code