Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use 'where in (a,b,c)' clause using ado.net and stored procedure. Below is the code.


C#
SqlConnection sqlConnection1 = new SqlConnection("connectionstring");
                sqlConnection1.Open();

                var sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection1;
                sqlCommand.CommandType = CommandType.StoredProcedure;


                var idList = new List<int> { 17, 55};
                var idParameterList = new List<string>();
                var index = 0;
                foreach (var id in idList)
                {
                    var paramName = "@idParam" + index;
                    sqlCommand.Parameters.AddWithValue(paramName, id);
                    idParameterList.Add(paramName);
                    index++;
                }


                sqlCommand.CommandText = String.Format("displayInfo",string.Join(",", idParameterList));    //displayinfo is Stored Procedeure Name

                using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
                {
                    var resultTable = new DataTable();
                    resultTable.Load(sqlReader);

                    // Do something with the result table
                }

                sqlConnection1.Close();




Stored Procedure

SQL
alter PROCEDURE displayInfo
@idParam varchar(max)
AS
BEGIN
    SELECT * FROM Table WHERE idParam IN (@idParam);
END
GO


And get below error-:

Procedure or function displayInfo has too many arguments specified.

Please Advise what should be the correct statement
Posted
Updated 18-Feb-15 23:29pm
v5
Comments
Praveen Tiwari 19-Feb-15 3:07am    
What is the error you are getting?
Herman<T>.Instance 19-Feb-15 4:29am    
You better store these parameters in a seperate table in the database with one identifying GUID value for the group of parameters. In your stored procedure you can simply JOIN that table when the generated GUID was added a parameter in the SP.
John C Rayan 19-Feb-15 4:36am    
Can you show us your store procedure? Without the SP it is difficult to say where the problem is.

CSS
You should use the following syntax for calling store procedure

sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "displayInfo";

// Here you could use your list and loop
sqlCommand.Parameters["idParam0"].Value = "17";
sqlCommand.Parameters["idParam1"].Value = "55";
 
Share this answer
 
v2
You should only provide the Procedure Name in CommandText like...
C#
sqlCommand.CommandText = "displayInfo";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900