Home All Groups Group Topic Archive Search About

Pb with DataAdapter.Fill

Author
7 Feb 2006 8:40 AM
bp
Hi

I'm trying to make a component to manage database access. One task of this
component is to create a database if it does not exist. To minimize queries's
count, I want to do database creation when DataAdapter.Fill fails (and if the
database doesn't exist). So I wrote the following code :

try
{
    dataAdapter.Fill(ds);
}
catch
{
    // Use a connection on master database to create a database
    connectionMaster.Open();
    commandMaster.ExecuteNonQuery(); // Create database query
    connectionMaster.Close();

    dataAdapter.Fill(ds);
}

But it fails on the second DataAdapter.Fill : "Can't open connection"

When I wrote the following code, it works :

connectionMaster.Open();
commandMaster.ExecuteNonQuery();
connectionMaster.Close();

dataAdapter.Fill(ds);

It seems that some resources have been blocked by the first
DataAdapter.Fill. How can I make my first code work ?

Thanks for responses.

bp

Author
8 Feb 2006 3:46 AM
Val Mazur (MVP)
Hi,

I believe if database does not exist, then you suppose to get an exception
when you try to open database. You do not need to call Fill of the
DataAdapter to do this. Anyway, if you need to call Fill for some reason,
then it is better to call Dispose method of the DataAdapter after you got an
exception, set variable to null an then continue

--
Val Mazur
Microsoft MVP
http://xport.mvps.org


Show quote
"bp" wrote:

> Hi
>
> I'm trying to make a component to manage database access. One task of this
> component is to create a database if it does not exist. To minimize queries's
> count, I want to do database creation when DataAdapter.Fill fails (and if the
> database doesn't exist). So I wrote the following code :
>
> try
> {
>     dataAdapter.Fill(ds);
> }
> catch
> {
>     // Use a connection on master database to create a database
>     connectionMaster.Open();
>     commandMaster.ExecuteNonQuery(); // Create database query
>     connectionMaster.Close();
>
>     dataAdapter.Fill(ds);
> }
>
> But it fails on the second DataAdapter.Fill : "Can't open connection"
>
> When I wrote the following code, it works :
>
> connectionMaster.Open();
> commandMaster.ExecuteNonQuery();
> connectionMaster.Close();
>
> dataAdapter.Fill(ds);
>
> It seems that some resources have been blocked by the first
> DataAdapter.Fill. How can I make my first code work ?
>
> Thanks for responses.
>
> bp

AddThis Social Bookmark Button