EDN Admin
Well-known member
<br/>
Hi list,<br/>
I am trying to send multiple parameters into a SP in c#, but cant make<br/>
the code working. I know how to do this for a known number of parameters,<br/>
but when I want to make the code dynamic, it wont work.<br/>
Lets say I got an array of string which holds my parameters names<br/>
as defined in stored procedure, myNames. Also lets say I got all the <br/>
parameters I want to pass to SP in another array, myValues.<br/>
Now here is what I have done, but it gives me error, could you help me on this please?
//k is the length of arrays<br/>
//stpname stored procedure name
<br/>
SqlConnection objCon = new SqlConnection(this.serverstring(server, database));<br/>
SqlDataAdapter objDataAdaper = new SqlDataAdapter();<br/>
objDataAdaper.SelectCommand = new SqlCommand();<br/>
DataTable dt = new DataTable();
try<br/>
{<br/>
objDataAdaper.SelectCommand.Connection = objCon;<br/>
objDataAdaper.SelectCommand.CommandType = CommandType.StoredProcedure;<br/>
objDataAdaper.SelectCommand.CommandText = stpname;<br/>
for (int k = 1; k <= NoParameters; k++)<br/>
{<br/>
double num;<br/>
if (double.TryParse(myValues[k - 1].ToString(), out num))<br/>
{<br/>
objDataAdaper.SelectCommand.Parameters.Add(myNames[k - 1], SqlDbType.Float).Value = myValues[k - 1];<br/>
}<br/>
else<br/>
{<br/>
objDataAdaper.SelectCommand.Parameters.Add(myNames[k - 1], SqlDbType.VarChar).Value = myValues[k - 1];<br/>
}<br/>
}<br/>
objCon.Open();<br/>
//fill the DTATA TABLE WITH DATA COMING BACK FROM SP<br/>
objDataAdaper.Fill(dt);<br/>
}
View the full article
Hi list,<br/>
I am trying to send multiple parameters into a SP in c#, but cant make<br/>
the code working. I know how to do this for a known number of parameters,<br/>
but when I want to make the code dynamic, it wont work.<br/>
Lets say I got an array of string which holds my parameters names<br/>
as defined in stored procedure, myNames. Also lets say I got all the <br/>
parameters I want to pass to SP in another array, myValues.<br/>
Now here is what I have done, but it gives me error, could you help me on this please?
//k is the length of arrays<br/>
//stpname stored procedure name
<br/>
SqlConnection objCon = new SqlConnection(this.serverstring(server, database));<br/>
SqlDataAdapter objDataAdaper = new SqlDataAdapter();<br/>
objDataAdaper.SelectCommand = new SqlCommand();<br/>
DataTable dt = new DataTable();
try<br/>
{<br/>
objDataAdaper.SelectCommand.Connection = objCon;<br/>
objDataAdaper.SelectCommand.CommandType = CommandType.StoredProcedure;<br/>
objDataAdaper.SelectCommand.CommandText = stpname;<br/>
for (int k = 1; k <= NoParameters; k++)<br/>
{<br/>
double num;<br/>
if (double.TryParse(myValues[k - 1].ToString(), out num))<br/>
{<br/>
objDataAdaper.SelectCommand.Parameters.Add(myNames[k - 1], SqlDbType.Float).Value = myValues[k - 1];<br/>
}<br/>
else<br/>
{<br/>
objDataAdaper.SelectCommand.Parameters.Add(myNames[k - 1], SqlDbType.VarChar).Value = myValues[k - 1];<br/>
}<br/>
}<br/>
objCon.Open();<br/>
//fill the DTATA TABLE WITH DATA COMING BACK FROM SP<br/>
objDataAdaper.Fill(dt);<br/>
}
View the full article