Solved

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

Posted on 2007-11-21
2
224 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
[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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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