Solved

C# - Re-using a DataAdapter

Posted on 2007-04-11
4
237 Views
Last Modified: 2010-04-16
Hi guys 'n gals,

I wanted to know if there is a way so consolidate my code, by maybe re-using my DataAdapter variable, rather than creating a new one for each query to the database? Here is how my code looks at the moment:

        void FillDataTables()
        {
            MySqlConnection conn = new MySqlConnection(connString);
            DataSet ds = new DataSet();
            conn.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM tblConnections", conn);
            adapter.Fill(ds);
            adapter = null;
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM tblInspector", conn);
            adapter.Fill(ds);
            conn.Close();

            MessageBox.Show(ds.Tables.Count.ToString());
        }


It throws the following error:

Error      1      A local variable named 'adapter' is already defined in this scope



Any ideas on getting past this?


Cheers guys!
0
Comment
Question by:Cyber-Drugs
4 Comments
 
LVL 23

Assisted Solution

by:Jens Fiederer
Jens Fiederer earned 150 total points
ID: 18892980
For the second use, do
    adapter = new MySqlDataAdapter("SELECT * FROM tblInspector", conn);

You don't need (and are not allowed!) to declare it twice!
0
 
LVL 4

Assisted Solution

by:aponcealbuerne
aponcealbuerne earned 150 total points
ID: 18892997
hi:
Well the error is because you have the same declaration two times
MySqlDataAdapter adapter --> this is twice in the code so you can use something like:

void FillDataTables()
        {
            MySqlConnection conn = new MySqlConnection(connString);
            DataSet ds = new DataSet();
            conn.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM tblConnections", conn);
            adapter.Fill(ds);
            adapter = null;
            adapter = new MySqlDataAdapter("SELECT * FROM tblInspector", conn);
            adapter.Fill(ds);
            conn.Close();
            MessageBox.Show(ds.Tables.Count.ToString());
        }
you can also just change the CommandText for the DataAdapter instead of recreate the object.


something like:

void FillDataTables()
        {
            MySqlConnection conn = new MySqlConnection(connString);
            DataSet ds = new DataSet();
            conn.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM tblConnections", conn);
            adapter.Fill(ds);
            adapter = null;
            adapter.SelectComand =      "SELECT * FROM tblInspector";
            adapter.Fill(ds);
            conn.Close();
            MessageBox.Show(ds.Tables.Count.ToString());
        }


Hope helps
0
 
LVL 3

Accepted Solution

by:
zajda82 earned 200 total points
ID: 18893043
you cant declare two variables of same name in one scope, you set your adapter to null, but variable still exists, try something like this:

using (MySqlConnection conn = new MySqlConnection("connstring"))
using (MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM tblConnections", conn))
{
       conn.Open();
       adapter.Fill(ds);
       adapter.SelectCommand.CommandText = "SELECT * FROM tblInspector";
       adapter.Fill(ds);
       conn.Close();
}
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18893109
Thanks guys, all the solutions seem to work, although I think I will be going with zajda82's, as it allows me to just change the select statement for the adapter, rather than re-calling the while lot again.


Cheers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

23 Experts available now in Live!

Get 1:1 Help Now