Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Adding data to datatable

Hi,

How can I add data from a dataset to datatable from the follwoing query?

           OleDbDataAdapter da = new OleDbDataAdapter("select  Field1, Field2, Field3 from myTable", cn);
            DataSet ds = new DataSet();
            cn.Open();

            da.Fill(ds);

I want to add the result data to a datatable

e.g

        dt = new DataTable("Sample");
            dt.Columns.Add("No", typeof(int));
            dt.Columns.Add("Name");
            dt.Rows.Add(1, "A1");
            dt.Rows.Add(2, "A2");
            dt.Rows.Add(3, "A3");
           dt.Rows.Add(4, "A4");
            dt.AcceptChanges();
           Tables.Add(dt);

Please help.

ayha
0
ayha1999
Asked:
ayha1999
  • 2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
ds.Tables["myTable"]  //pretty sure this works
  or
ds.Tables[0] //definitely works

is your datatable . . . it's already populated
0
 
ayha1999Author Commented:
Actually my goal is to populate a reportviewer from an object datasource.  when I create a report from  a the follwoing class it works means I can choose filed e.g No, Name.
\ public class DBO : DataSet
    {
            //dt = new DataTable("Sample");
            //dt.Columns.Add("No", typeof(int));
            //dt.Columns.Add("Name");

        private DataTable dt;

        public DBO()
        {

            //dt.Rows.Add(1, "A1");
            //dt.Rows.Add(2, "A2");
            //dt.Rows.Add(3, "A3");
            //dt.Rows.Add(4, "A4");
            //dt.AcceptChanges();
            //Tables.Add(dt);
        }
    }

but when Use the the following class

           OleDbDataAdapter da = new OleDbDataAdapter("select  Field1, Field2, Field3 from myTable", cn);
            DataSet ds = new DataSet();
            cn.Open();

            da.Fill(ds);
dt = ds.Tables[0];

I get the field name as CaseSensitive, DataSetName, EnforceConstraint, Locale etc. instead of Field1, Field2, Field2 as in the other class.

Thanks
0
 
ayha1999Author Commented:
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now