Link to home
Start Free TrialLog in
Avatar of Cyber-Drugs
Cyber-DrugsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C# - Re-using a DataAdapter

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!
SOLUTION
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of aponcealbuerne
aponcealbuerne

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cyber-Drugs

ASKER

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!