In this article I will explain with an example, how to resolve the following error:
The variable name '@param' has already been declared. Variable names must be unique within a query batch or stored procedure.
 
 
Error
The following error occurs when SQL Server database finds two variables or parameters with same Name.
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 wgreyew 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.

 
 
Solution
The solution to this problem is to change the Name of any one variable or parameter which has exact same Name as another variable.
You can get rid of this error in the following way:
1. If you are running a loop and using the same command object to insert data add this statement.
cmd.Parameters.Clear();
 
2. If you have not passed the same parameter twice. If you found remove them or change the Name.
cmd.Parameters.AddWithValue("@city", txtCity.Text.Trim());
cmd.Parameters.AddWithValue("@city", txtCity.Text.Trim());
 
3. If you are using Stored Procedure check if you have duplicate variables. If you found it, remove them.
For example:
DECLARE @CustID varchar(10)
SET @CustID= 'ALFKI'
SELECT *FROM Customers WHERE CustomerID = @CustID
DECLARE @CustID varchar(50) --Duplicate