Solved

Adding data to datatable

Posted on 2012-03-22
3
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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