You must Sign In to post a response.
  • Category: .NET

    Loop throgh SP result in C#

    Hi ,
    I have a stored procedure to get list of employeeIDs. I want to loop though the result of this SP and execute another SP which gets me all the details for each employees (based on EmployeeID).
    Kindly let me how to loop though the result of stored procedure in C#.

  • #764180
    You can use the data table for this.
    Following are some tips to do your requirement.

    SqlConnection MySqlConnection = new SqlConnection("ConnectionString");
    SqlCommand MyCommand = new SqlCommand("Your SP", MySqlConnection );

    MyCommand .CommandType = CommandType.StoredProcedure;
    MySqlConnection .Open();
    SqlDataAdapter MyDataAdapter = new SqlDataAdapter(MyCommand);
    DataSet MyDataSet = new DataSet();
    MyDataAdapter.Fill(MyDataSet, "MyDT");

    DataTable aDataTableL;
    aDataTableL = aDataSetP.Tables["MyDT"];
    foreach (DataRow drow in TheDataTableP.Rows)
    string EMPId = drow["EmployeeID"]
    // Call the another SP

    By Nathan
    Direction is important than speed

  • #764182
    Thanks but further to this , I want to know how can I loop if there are more than 1 field in select statement in SP.
    For example if SP is retrieving EmpID and EmpName, how to loop with these 2 values and execute underlying SP?

  • #764188
    Hai Pramode,
    I think you can pass 2 parameters for the next SP. Other ways could be just do it in the Stored Procedure itself if you are not doing anything with the first SP values.
    Just called the second SP inside first and pass the values. Get the final result to your code.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

Sign In to post your comments