Solved

C# - Re-using a DataAdapter

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Windows app updating JSON file error. 9 48
pressing download button in c# 7 47
Coding for the first time 9 99
Trigger C# code inside the SQL Server 6 57
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 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