[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2007-11-21
2
Medium Priority
?
230 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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