Object reference not set to an instance of an object

  • Thread starter Thread starter Bill Cart
  • Start date Start date
B

Bill Cart

I am very new at this C# stuff. I keep trying to understand it but I don't
seen to have a clue. I am trying to fill a DropDownList from a data adapter.

I have a web application. In the Global.asx I have
private System.Data.SqlClient.SqlConnection sqlConSWT;
private System.Data.SqlClient.SqlCommand sqlCmdAddDemoReq;
private System.Data.SqlClient.SqlDataAdapter sqlDaStates;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private DemoReqSql.dsStates dsStates;


In the web form I have
// redeclare the database stuff in the Global unit
System.Data.SqlClient.SqlCommand sqlCmdAddDemoReq;
System.Data.SqlClient.SqlDataAdapter sqlDaStates;
protected DemoReqSql.dsStates dsTryAgain;
protected System.Web.UI.WebControls.DropDownList DropDownList1;


private void Page_Load(object sender, System.EventArgs e)
{
sqlDaStates.Fill(dsTryAgain);
DropDownList1.DataBind();

Something just like this works in another applications but I can't get this
to go at all. It look just like what is in the book. The error points to the
sqlDaStates.Fill(dsTryAgain); and says "Object reference not set to an
instance of an object". The data set is on the form, do I need to redeclare
it again? Or is there some sort of other error that I should be able to
infer?
 
this is bad bad bad. you are holding references to connection objects in
global don't do that
you also are not initializing and opening your connection objects.

i recommend programming ado.net by david scheppa. you need to understand the
basics thoroughly before going on.
 
Thanks.

The idea for the data stuff to be in the Global comes from the Microsoft
MCAD/MCSD Self Paced Training Kit - Developing Web Applications. The idea is
that if they are declared there they can be used in all of the other
modules. Because I am just starting out I don't understand all of it. I
don't see why they have to be redeclared in the form is they are present in
the Gobal. I have been working with Delphi which has a data module, you just
place things in there and include a reference to it and all of the forms can
access them.

I have tried it both ways (in the Gobal and in the form itself) and I still
can't get it to work. I am basicly copying what they did in the book's
example (which does work) but I must be leaving something out. I have some
C# database books on reserve at the libary, I hope that they can show me
what I should be doing.

Ignacio Machin said:
Hi Bill,

These variables needs to be declared in the aspx page, no in the global.aspx
, there are no use for then in the global.aspx
 
Back
Top