Solved

C# - Re-using a DataAdapter

Posted on 2007-04-11
4
238 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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