对象引用未设置为对象的实例(asp.net,c#) [英] Object reference not set to an instance of an object (asp.net, c#)

查看:50
本文介绍了对象引用未设置为对象的实例(asp.net,c#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我用asp.net(c#)编写了一些应用程序,这是我的问题。

此代码(带有注释字符串行)效果很好:当我打开这个页面

在Internet Explorer中我在DataGrid2中获取了一些数据来自我的数据库...

....

.. ..


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);


//字符串s =(字符串)myDataSet.Tables [" myTable"]。行[0] [ " myColumn"];


DataGrid2.Da taSource = myDataSet;

DataGrid2.DataBind();


......

......

问题:如果我取消注释字符串行,然后我收到此

行的错误消息(

对象引用未设置为对象的实例)当我尝试打开

这个页面在Internet Explorer中。


我在我的数据库表myTable中,在这个表中我有列

myColumn,并在此

列我肯定是第一行(索引为0),但它不起作用......为什么?


请帮助...谢谢...

解决方案



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


我建议你在调试器中或用某些东西检查句子

如:

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

DataRow row = table.Rows [0];

object value = row [" myColumn"];

你会看到问题所在。


-

Miha Markic [MVP C#] - RightHand .NET咨询&开发

miha at rthand com
www.rthand.com

问题:如果我取消注释字符串行,然后当我尝试打开
这个页面时,我收到错误消息
这个
行(
对象引用未设置为对象的实例) Internet Explorer。

我在我的数据库表myTable中,在这个表中我有列
myColumn,在这个
列中我肯定是第一行(索引为0 ;),但它没有工作......为什么?

请帮助......谢谢......



kronum,

//字符串s =(字符串)myDataSet.Tables [" myTable"]。行[0] [" myColumn"];


不会被设置为对象的引用,因为没有名为

" myTable"的表。在数据集中。


您需要使用将表名作为第二个

参数的Fill重载或访问索引为0的表作为下面。

//字符串s =(字符串)myDataSet.Tables [0] .Rows [0] [" myColumn"];


dataadapter无法知道如何命名表格的结果

来自数据库的查询。

查询实际上可以返回多个结果集,因此

dataadapters必须使用(table,table1,...... tablen)


祝你好运


Hank


Miha Markic [MVP C#]" < miha at rthand com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...



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



我建议你在调试器中或用类似的东西检查句子:
DataTable table = myDataSet.Tables [" myTable"];
DataRow row = table.Rows [0];
对象值=行[" myColumn"];
你会看到问题出在哪里。

-
Miha Markic [MVP C#] - RightHand .NET咨询&开发
miha at rthand com
www.rthand.com


问题:如果我取消注释字符串行,然后当我尝试
打开此页面时,我收到错误消息
这个
行(
对象引用未设置为对象的实例) Internet Explorer。

我在我的数据库表myTable中,在这个表中我有列
myColumn,在这个
列中我肯定是第一行(索引为0 ;),但它没有工作......为什么?

请帮助......谢谢......




即时网络初学者,因此请耐心等待我......并且

感谢您的回复...


我试过这个:

使用索引0访问你的表,如下所示。
// string s =(string)myDataSet .Tables [0] .Rows [0] [" myColumn"];




但我收到错误消息:指定的演员表无效。


如果我写而不是:


string s =(string)myDataSet.Tables [& myTable]。行[0] [" myColumn"];


这一行:


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

DataRow row = table.Rows [0];

object value = row [" myColumn"];


我收到相同的错误消息:


"对象引用未设置为对象的实例。


和此消息显示此行:


DataRow row = table.Rows [0];


这可能意味着我可以写了myTable在


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


因为此行没有显示错误消息。 />



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

解决方案

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



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




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.


这篇关于对象引用未设置为对象的实例(asp.net,c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆