I have created a class, in which I am trying to pass query and return the data in a DataTable
, but I am unable to pass a parameter to the SqlCommand
.
My attempt:
OpenSqlConnection();
SqlCommand sqlCommand = new SqlCommand(inputQuery, sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("", "");
sqlCommand.ExecuteNonQuery();
DataTable dtResult = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
da.Fill(dtResult);
CloseSqlConnection();
return dtResult;
In the above code, I am passing SqlCommand
as an input query.
How I am calling the above function.
stdFetchDt = new DBManager().GetRecordsByQuery("storedprocedurename");
stdDrop.DataSource = stdFetchDt;
stdDrop.DataBind();
Here DBManager
is the class name and GetRecordsByQuery
is the method name.
But I’m not able to pass value for sqlCommand.Parameters.AddWithValue("", "");
.
My problem: I am unable to pass value in sqlCommand.Parameters.AddWithValue("", "");
, because number of value may be multiple.
Please help me with this.
2
Answers
I have Created a class and passing OBJECT & VALUE using List argument.
Here in inputQuery String, I am passing name of stored procedure and in objectInput passing object and in valInput passing its value.
You can send your parameter string with values combined with ‘,’ in C# and then separate theme in the stored procedure :