473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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


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...
Nov 16 '05 #1
10 10943
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...

Nov 16 '05 #2
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 [MVP C#]" <miha at rthand com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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...


Nov 16 '05 #3
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.
Nov 16 '05 #4
"krunom" <kr****@hotmail.com> wrote in message
news:cg**********@bagan.srce.hr...
"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
Nov 16 '05 #5
Bob Grommes <bo*@bobgrommes.com> wrote:
"krunom" <kr****@hotmail.com> wrote in message
news:cg**********@bagan.srce.hr...
"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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
> 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...


Nov 16 '05 #7
Kruno Milicevic <kr****@hotmail.com> wrote:
that means the table doesn't exist at all.
I wrote in my 1st post:


<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
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...
Nov 16 '05 #9
Kruno Milicevic <kr****@hotmail.com> wrote:
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)!


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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kruno Milicevic <kr****@hotmail.com> wrote:
that means the table doesn't exist at all.
I wrote in my 1st post:


<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob Cannistraci | last post by:
A three-tier user authentication system was running without a problem for almost a year and now is suddenly dysfunctional. We don't know of any changes to any of the servers. It's quite maddening....
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
1
by: Dave | last post by:
Hello I hope someone can give me some idea of what is causing this error. I can run though my asp app locally no problem but when i put it on the server i get this error, any help at all whould be...
6
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page...
8
by: Charlie J | last post by:
I have a real stumper. I added a server side table to a home page that has two other server side tables on it that have been working great. In Visual Studio everything works great. When I put...
2
by: danielle.m.manning | last post by:
I have a question about a problem I am having with attempting to port some old ASP code to ASP.NET. We have a dll out there which we downloaded which creates GUIDs for session management. In...
3
by: SAL | last post by:
I am getting the following ERROR in my WebApp on line 30: Server Error in '/TestWebApp' Application. -------------------------------------------------------------------------------- Object...
6
by: kalaivanan | last post by:
hi, i am a beginner in c#. i have theoretical knowledge about object, reference and instance. but i want to know clearly about what is an object, reference and instance. can any one help me? or...
3
by: Asprisa | last post by:
Hi Guys, We have a aspx page on a web server that was created by someone who has now left our company which for some reason just started throwing an error, i have been looking into reasons for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.