How do I store multiple results from a stored procedure into a dataset?

How do I combine to result sets from a StoredProcedure into one dataset in ASP.NET?

Below is my code in

SqlDataAdapter adap = new System.Data.SqlClient.SqlDataAdapter("sp_Home_MainBanner_TopStory",con);
adap.SelectCommand.CommandType = CommandType.StoredProcedure;
adap.SelectCommand.Parameters.AddWithValue("@rows", 9);

DataSet DS = new DataSet();

adap.Fill(DS, "Table1");
adap.Fill(DS, "Table2");

GridView1.DataSource = DS.Tables["Table2"];

Even if there were two adapters, how could I combine the results into one dataset?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In MS SQL we create a procedure like:

[ create proc procedureName
        select * from student
        select * from test
        select * from admin
        select * from result

In C#, we write following code to retrieve these values in a DataSet
    SqlConnection sqlConn = new SqlConnection("data source=(local);initial catalog=bj001;user id=SA;password=bj");
    SqlCommand sqlCmd = new SqlCommand("procedureName", sqlConn);
    sqlCmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter sda = new SqlDataAdapter(sqlCmd);
    DataSet ds = new DataSet();

    // Retrieving total stored tables from a common DataSet.              
    DataTable dt1 = ds.Tables[0];
    DataTable dt2 = ds.Tables[1];  
    DataTable dt3 = ds.Tables[2];
    DataTable dt4 = ds.Tables[3];  

    // To display all rows of a table, we use foreach loop for each DataTable.
    foreach (DataRow dr in dt1.Rows)
        Console.WriteLine("Student Name: "+dr[sName]);

Method 2

A DataSet contains Tables. For your above example, if you had two SqlDataAdapters, each calling a stored procedure and stored them like you did above.

adapter1.Fill(DS, "Table1");
adapter2.Fill(DS, "Table2");

This will take the table results from your first query and store it in the DataSet DS as Table1. It will then store another Table (Table2) in the same DataSet. To access these tables you use the following code:
DS.Tables["Table1"]  //Or Table2, or whatever you name it during your Fill.

You already have the right process, you just need to look up how a DataSet works and decide how you want to call your information.

IF you want to combine your results into one DataTable however, you will need to iterate through the tables and combine information.

DataTable combinedTable = new DataTable();
//Create columns

foreach (DataRow row in DS.Tables["Table1"].Rows)
    //Create rows?  Copy information over?  Whatever you want to do.

Method 3

try using this:

adapter1.Fill(DS, "Table1, Table2");

this works here so…

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x