Solved

Adding data to datatable

Posted on 2012-03-22
3
247 Views
Last Modified: 2012-03-23
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
Comment
Question by:ayha1999
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 37754759
ds.Tables["myTable"]  //pretty sure this works
  or
ds.Tables[0] //definitely works

is your datatable . . . it's already populated
0
 
LVL 7

Author Comment

by:ayha1999
ID: 37754801
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
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 37760065
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now