Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to add datatable object to dataset. I can add at a time single object to dataset but now I want to select data from multiple tables in datatables. Finally, I want to add all datatables to dataset and return dataset.

I am unable to append datatable to dataset.

Following is the code I have where tablename is the list of table names:

C#
public DataSet MultipleSelectRecord(string databasename,List<string>tablename)
       {
           try
           {
              DataSet AppendDs =new DataSet ();
               for (int i = 0; i < tablename.Count; i++)
               {
                   string query = " USE " + databasename + " SELECT * FROM " + tablename[i] + " ";
                   da = new SqlDataAdapter(query , con);
                   DataTable dtobj = new DataTable();
                   da.Fill(dtobj);
                  
                   AppendDs.Tables.Add(dtobj);//it replace previous dataset record this is problem
               }
               return AppendDs;
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }

       }</string>


Thank you.

Edit: Added pre tag.
Posted
Updated 25-May-11 4:43am
v2

1 solution

This code will not compile. Maybe you missed why pasting it. Other than that it looks fine to me. What does the debugger say?

Unless DataSet declaration is inside the loop in actual code, it should add tables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900