View Full Version : A problem:the failure to object reference to the targets set an example.

05-03-2007, 01:43 PM
hi,all.I am met a problem.can't fix it.
And I have no idea how to transalted ,but use google language tool.so if it makes any mistake ,forgive me.

Error : the failure to object reference to the targets set an example.
Note : implementation of the current Web request, there untreated abnormal. Please check stack tracking information to understand the code and the error led to the wrong source of detailed information.
Extremely detailed information : System.NullReferenceException : failure to object reference to the targets set an example.
Source mistakes :
line 42 :
line 43 : // DataGrid with data shows.
line 44 : if (myDataSet.Tables[ "bbs_infomation" ].Ro ws.Count = 0)
line 45 : {
line 46 : myDataSet.Tables[ DataGrid1.DataSource = "bbs_information "].DefaultView;

it shows that line 44 has problem,but I can't find it.
so this is the code:

//Use Connection to connect the database。
string connStr = System.Configuration.ConfigurationSettings.AppSettings["connString"];
SqlConnection myConnection = new SqlConnection(connStr);
//Use DataAdapter to visit the table 。
string queryStr = "select * from bbs_information where parentid=0 order by datetime desc";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(queryStr, myConnection);

//Use DataSet to store the table。
DataSet myDataSet = new DataSet();

//Use DataGrid to display data。
if (myDataSet.Tables["bbs_infomation"].Rows.Count != 0)
DataGrid1.DataSource = myDataSet.Tables["bbs_information"].DefaultView;
Label1.Text += "(No words leave to you!)";
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
DataGrid1.CurrentPageIndex = e.NewPageIndex;

I am sorry for my poor enlish,but I have no choice.
Thank you,

05-04-2007, 01:00 PM
as far as I can interpret this, it says reference to an object not set to an instance of an object

so try this: run the query directly on the database, if it returns nothing, that's where your problem is.

If I am not mistaken, if the query returns nothing, the dataset will be set to null, so you wont have a row count. If there are any chances of having empty results, try changing your line 44

if (myDataSet.Tables["bbs_infomation"].Rows.Count != 0)


if (myDataSet.Tables["bbs_infomation"] != null)

hope that helped =)