Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

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

19 Experts available now in Live!

Get 1:1 Help Now