Simple database class error


L

Lasse Edsvik

Hello

Im trying to create a simple testclass that connects to a db on localhost
and a method that returns a dataset. I get these errors:

Unhandled Exception: System.InvalidOperationException: Fill:
SelectCommand.Conne
ction property has not been initialized.
at System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter adapter,
IDb
Command command, String method)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset,
DataTable[]
datatables, Int32 startRecord, Int32 maxRecords, String srcTable,
IDbCommand co
mmand, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32
startRecord,
Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior
behavior)

at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at Procent.DataJox.Northwind.GetAllCustomers() in C:\Documents and
Settings\A
dministrator\My Documents\Visual Studio\Projects\DataJox\DataJox\Class1.cs:
line 21
at rock.Program.Main(String[] args) in C:\Documents and
Settings\Administrato
r\My Documents\Visual Studio\Projects\DataJox\rock\Program.cs:line 14



using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;

namespace Procent.DataJox

{

public class Northwind

{

private SqlConnection dbconn;


public DataSet GetAllCustomers()

{

Open();

DataSet ds = new DataSet();


SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = new SqlCommand("CustListAll", dbconn);

da.Fill(ds);

return ds;

}



private void Open()

{

string connstr = "server=localhost;database=northwind;UID=sa;PWD=kk";

try

{

SqlConnection dbconn = new SqlConnection(connstr);

dbconn.Open();

}

catch

{

throw;

}

}


}


}
 
Ad

Advertisements

C

Chris Dunaway

Lasse said:
Im trying to create a simple testclass that connects to a db on localhost
and a method that returns a dataset. I get these errors:

Unhandled Exception: System.InvalidOperationException: Fill:
SelectCommand.Conne
ction property has not been initialized.

That's because you never open the connection.

The connection you open in the Open() method is a /different/
connection that is declared inside the Open method and is only visible
there. It is not the same as the one you declared at class level.
SqlConnection dbconn = new SqlConnection(connstr);

Change this line to:

dbconn = New SqlConnection(connstr)
 
S

Spidey

The line
SqlConnection dbconn = new SqlConnection(connstr);
in the Open method is the problem.
Replace this line with
dbconn = new SqlConnection(connstr);


What's happenning here is that a connection object is being created,
assigned to and opened properly in the Open method. However, this is
being assigned to the local variable dbconn (because of the
SqlConnection dbconn declaration inside the Open method). This local
dbconn variable is valid only within the Open method. The dbconn
variable referred to in the GetAllCustomers method is the private
variable dbconn which has never been assigned to.

Regards,
Sarin.
 
L

Lasse Edsvik

Spidey,

thx, one step further til next error :)

I still get NullReferenceException, but on this line:

da.SelectCommand.Connection = dbconn;

not sure what's wrong here



using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;

namespace Procent.DataJox

{

public class Northwind

{

private SqlConnection dbconn;


public DataSet GetAllCustomers()

{

Open();

DataSet ds = new DataSet();


SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand.Connection = dbconn;

da.SelectCommand = new SqlCommand("CustListAll");

da.Fill(ds);

return ds;

}



private void Open()

{

string connstr = "server=localhost;database=northwind;UID=sa;PWD=kk";


try

{

dbconn = new SqlConnection(connstr);

dbconn.Open();

}

catch

{

throw;

}

}


}


}




Spidey said:
The line
SqlConnection dbconn = new SqlConnection(connstr);
in the Open method is the problem.
Replace this line with
dbconn = new SqlConnection(connstr);


What's happenning here is that a connection object is being created,
assigned to and opened properly in the Open method. However, this is
being assigned to the local variable dbconn (because of the
SqlConnection dbconn declaration inside the Open method). This local
dbconn variable is valid only within the Open method. The dbconn
variable referred to in the GetAllCustomers method is the private
variable dbconn which has never been assigned to.

Regards,
Sarin.

Lasse said:
Hello

Im trying to create a simple testclass that connects to a db on localhost
and a method that returns a dataset. I get these errors:

Unhandled Exception: System.InvalidOperationException: Fill:
SelectCommand.Conne
ction property has not been initialized.
at System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter adapter,
IDb
Command command, String method)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset,
DataTable[]
datatables, Int32 startRecord, Int32 maxRecords, String srcTable,
IDbCommand co
mmand, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32
startRecord,
Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior
behavior)

at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at Procent.DataJox.Northwind.GetAllCustomers() in C:\Documents and
Settings\A
dministrator\My Documents\Visual Studio\Projects\DataJox\DataJox\Class1.cs:
line 21
at rock.Program.Main(String[] args) in C:\Documents and
Settings\Administrato
r\My Documents\Visual Studio\Projects\DataJox\rock\Program.cs:line 14



using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;

namespace Procent.DataJox

{

public class Northwind

{

private SqlConnection dbconn;


public DataSet GetAllCustomers()

{

Open();

DataSet ds = new DataSet();


SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = new SqlCommand("CustListAll", dbconn);

da.Fill(ds);

return ds;

}



private void Open()

{

string connstr = "server=localhost;database=northwind;UID=sa;PWD=kk";

try

{

SqlConnection dbconn = new SqlConnection(connstr);

dbconn.Open();

}

catch

{

throw;

}

}


}


}
 
B

Bjorn Abelli

...
thx, one step further til next error :)

I still get NullReferenceException, but on this line:

da.SelectCommand.Connection = dbconn;

not sure what's wrong here
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand.Connection = dbconn;
da.SelectCommand = new SqlCommand("CustListAll");

Maybe you should shift the order of statements...

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("CustListAll");
da.SelectCommand.Connection = dbconn;

// Bjorn A
 
Ad

Advertisements

L

Lasse Edsvik

Bjorn,

thx very much....... :)

class seem fine now though........ Just cant get it to display in a listbox
or whatever :)

cant get that DataBind to show up for the object im trying to populate.


what's more needed to do besides:

public Form1()

{

InitializeComponent();

Northwind n = new Northwind();

listBox1.DataSource = n.GetAllCustomers();


}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top