skip to Main Content

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


  1. Chosen as BEST ANSWER

    I have Created a class and passing OBJECT & VALUE using List argument.

     public DataTable GetRecordsByQuery(string inputQuery, List<string> objectInput, List<string> valInput)
    

    Here in inputQuery String, I am passing name of stored procedure and in objectInput passing object and in valInput passing its value.


  2. You can send your parameter string with values combined with ‘,’ in C# and then separate theme in the stored procedure :

    DECLARE @tags NVARCHAR(400) = 'clothing,road,,touring,bike'  
    
    SELECT value  
    FROM STRING_SPLIT(@tags, ',') 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search