Solved

Adding data to datatable

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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