Object reference not set to an instance of an object (asp.net, c#)

K

krunom

I write some application with asp.net (c#) and here is my problem.
This code (with "commented" string line) works well: when i open this page
in Internet Explorer i get in DataGrid2 some data frome my database...


....
....

OleDbConnection OleDbConn = new
OleDbConnection(ConfigurationSettings.AppSettings["connstr"]);

string selectString="SELECT * FROM myTable"

OleDbCommand myOleDbCommand=OleDbConn.CreateCommand();
myOleDbCommand.CommandText=selectString;

OleDbDataAdapter myOleDbDataAdapter = new OleDbDataAdapter();
myOleDbDataAdapter.SelectCommand=myOleDbCommand;

DataSet myDataSet=new DataSet();
myOleDbDataAdapter.Fill(myDataSet);

//string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];

DataGrid2.DataSource = myDataSet;
DataGrid2.DataBind();

......
......


The problem: if I "uncomment" string line, then i get error message for this
line ("
Object reference not set to an instance of an object") when i try to open
this page in Internet Explorer.

I have in my database table myTable and in this table i have column
myColumn, and in this
column i have surely first row (indexed with "0"), but it doesnt work...why?

please help...Thanks...
 
M

Miha Markic [MVP C#]

Hi,

//string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];

I suggest you to check the sentence either in debugger or with something
like:
DataTable table = myDataSet.Tables["myTable"];
DataRow row = table.Rows[0];
object value = row["myColumn"];
And you'll see where the problem is.
 
H

Hank Hatch

kronum,

//string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];

will not be set to a reference of an object because there is no table named
"myTable" in the dataset.

You need to use the Fill overload that takes the table name as the second
parameter or access your table with the index of 0 as below.
//string s = (string) myDataSet.Tables[0].Rows[0]["myColumn"];

The dataadapter has no way of knowing what to name tables that are a result
of a query from the database.
A query can actually return multiple result sets and therefore the
dataadapters must use (table, table1, ...... tablen)

Good Luck

Hank



Miha Markic said:
Hi,

//string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];

I suggest you to check the sentence either in debugger or with something
like:
DataTable table = myDataSet.Tables["myTable"];
DataRow row = table.Rows[0];
object value = row["myColumn"];
And you'll see where the problem is.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
The problem: if I "uncomment" string line, then i get error message for
this
line ("
Object reference not set to an instance of an object") when i try to open
this page in Internet Explorer.

I have in my database table myTable and in this table i have column
myColumn, and in this
column i have surely first row (indexed with "0"), but it doesnt
work...why?

please help...Thanks...
 
K

krunom

Hi, im .net-beginner and because of that please be patient with me...and
thanks for replies...

I tried this:
access your table with the index of 0 as below.
//string s = (string) myDataSet.Tables[0].Rows[0]["myColumn"];

but i get error message: Specified cast is not valid.

And if I write instead of:

string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];

this lines:

DataTable table = myDataSet.Tables["myTable"];
DataRow row = table.Rows[0];
object value = row["myColumn"];

I get the same error message:

"Object reference not set to an instance of an object."

and this message appears for this line:

DataRow row = table.Rows[0];

This could mean that I can wrote "myTable" in

DataTable table = myDataSet.Tables["myTable"];

because the error message dont appear for this line.
 
B

Bob Grommes

krunom said:
"Object reference not set to an instance of an object."

and this message appears for this line:

DataRow row = table.Rows[0];

In that case, the RowCollection in the DataTable is empty. The query must
not be returning any records.

--Bob
 
J

Jon Skeet [C# MVP]

Bob Grommes said:
krunom said:
"Object reference not set to an instance of an object."

and this message appears for this line:

DataRow row = table.Rows[0];

In that case, the RowCollection in the DataTable is empty. The query must
not be returning any records.

No, that would give an IndexOutOfRangeException - if it's returning
NullReferenceException, that means the table doesn't exist at all.
 
K

Kruno Milicevic

that means the table doesn't exist at all.

I wrote in my 1st post:

"This code (with "commented" string line) works well: when i open this page
in Internet Explorer i get in DataGrid2 some data frome my database...
....
....

OleDbConnection OleDbConn = new
OleDbConnection(ConfigurationSettings.AppSettings["connstr"]);

string selectString="SELECT * FROM myTable"

OleDbCommand myOleDbCommand=OleDbConn.CreateCommand();
myOleDbCommand.CommandText=selectString;

OleDbDataAdapter myOleDbDataAdapter = new OleDbDataAdapter();
myOleDbDataAdapter.SelectCommand=myOleDbCommand;

DataSet myDataSet=new DataSet();
myOleDbDataAdapter.Fill(myDataSet);

//string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];

DataGrid2.DataSource = myDataSet;
DataGrid2.DataBind();
......
......"

If i comment this line:
//string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];
everything works fine and this should be evidence that this table exists,
but if I "uncomment" this line i get this error message in Explorer! why? :
(
The same thing happens if i write/comment/uncomment this lines:
DataTable table = myDataSet.Tables["myTable"];

DataRow row = table.Rows[0];//for this line error message appears

object value = row["myColumn"];

instead of:

string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];



Please help...
 
J

Jon Skeet [C# MVP]

Kruno Milicevic said:
I wrote in my 1st post:

If i comment this line:
//string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];
everything works fine and this should be evidence that this table exists,

Not really. It shows that *a* table exists, but it doesn't show that
myDataSet.Tables["myTable"] exists.

Try calling myDataSet.Tables.Contains("myTable") - I think you'll find
the result is false.
 
K

Kruno Milicevic

Try calling myDataSet.Tables.Contains("myTable") - I think you'll find
the result is false.

yes...the result is FALSE, but why? myDataSet is NOT empty (i succesfully
fill DataGrid with data from my DataSet)!

how should i approach data (tables, columns, rows) in myDataSet?

i simply cant approach data...

please help me...

Thanks...
 
J

Jon Skeet [C# MVP]

Kruno Milicevic said:
yes...the result is FALSE, but why? myDataSet is NOT empty (i succesfully
fill DataGrid with data from my DataSet)!

As Hank said a few posts ago:

<quote>
You need to use the Fill overload that takes the table name as the
second parameter or access your table with the index of 0 as below.
</quote>
 
K

Kruno Milicevic

You need to use the Fill overload that takes the table name as the
second parameter or access your table with the index of 0 as
elow.

It works... Thanks....
 
N

Nick Malik

Hi Kruno,

Arguing with Jon Skeet is kinda pointless unless you check your facts before
responding...

Besides, I made this mistake myself. The resultset that comes back from a
query does not usually contain the name of the table that the data came
from. The table name in the dataset is usually something like "table1".

Run your code in the debugger and stop on the line in question. If you
query the dataset, you will find the actual name of the table.

Good luck. I hope this helps,

--- Nick Malik

Jon Skeet said:
Kruno Milicevic said:
I wrote in my 1st post:

If i comment this line:
//string s = (string) myDataSet.Tables["myTable"].Rows[0]["myColumn"];
everything works fine and this should be evidence that this table
exists,

Not really. It shows that *a* table exists, but it doesn't show that
myDataSet.Tables["myTable"] exists.

Try calling myDataSet.Tables.Contains("myTable") - I think you'll find
the result is false.
 

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