Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# - Re-using a DataAdapter

Posted on 2007-04-11
4
Medium Priority
?
258 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
[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
4 Comments
 
LVL 23

Assisted Solution

by:Jens Fiederer
Jens Fiederer earned 600 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 600 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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.…

610 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