Solved

How do I Fill a DataSet with different tables without recreating the adapter?

Posted on 2007-11-21
2
221 Views
Last Modified: 2013-11-07
I'm using ASP.NET 2.0 / C# and I have several SELECT statements that I will be using to fill up a DataSet with DataTables. But I'm stuck recreating the dataadapters. See here:

    OdbcConnection conn = new OdbcConnection(connString);    
    DataSet ds = new DataSet();

    sqlSelect = "SELECT ordid, date FROM orders";
    OdbcDataAdapter ad = new OdbcDataAdapter(sqlSelect, conn);
    ad.Fill(ds, "Orders");

    sqlSelect = "SELECT customerid, misc1 FROM customers";
    OdbcDataAdapter adx = new OdbcDataAdapter(sqlSelect, conn);
    adx.Fill(ds, "Customers");

Notice how I have OdbcDataAdapter adx = new OdbcDataAdapter. How do I declase the OdbcDataAdapter at the top and reuse it?
0
Comment
Question by:bemara57
  • 2
2 Comments
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 20329932
You can each time create new commands and set it as the adapter's select command.
or you can simply change the SelectCommand.CommandText of the adapter to your new sql...

Like this:

OdbcConnection conn = new OdbcConnection(connString);    
DataSet ds = new DataSet();
 
sqlSelect = "SELECT ordid, date FROM orders";
OdbcDataAdapter ad = new OdbcDataAdapter(sqlSelect, conn);
ad.Fill(ds, "Orders");
 
sqlSelect = "SELECT customerid, misc1 FROM customers";
ad.SelectCommand.CommandText=sqlSelect;
adx.Fill(ds, "Customers");

Open in new window

0
 
LVL 12

Accepted Solution

by:
Mortaza Doulaty earned 500 total points
ID: 20329945
Sorry, the last line should change to:
ad.Fill(ds, "Customers");

So your code looks like:
OdbcConnection conn = new OdbcConnection(connString);    
DataSet ds = new DataSet();
 
sqlSelect = "SELECT ordid, date FROM orders";
OdbcDataAdapter ad = new OdbcDataAdapter(sqlSelect, conn);
ad.Fill(ds, "Orders");
 
sqlSelect = "SELECT customerid, misc1 FROM customers";
ad.SelectCommand.CommandText=sqlSelect;
ad.Fill(ds, "Customers");

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 33
Printing 1 50
Need to learn more about SecurityProtocolType.Tls12 3 27
tableview is not updating 1 7
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

805 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