Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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