Solved

Adding data to datatable

Posted on 2012-03-22
3
245 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 39

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

22 Experts available now in Live!

Get 1:1 Help Now