尝试关键字导致问题 [英] try keyword causing problems

查看:75
本文介绍了尝试关键字导致问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,我试图将其包含在try / catch块中。这个

代码在.aspx页面的代码隐藏的Page_Load()方法中。


在尝试的行上关键字是,我收到错误对象

引用未设置为对象的实例。为什么我会在

上得到尝试关键词。我已经复制了下面的代码,你可以看到我已经用b $ b注释了额外的代码,我所拥有的只是

Page_Load( )事件和try / catch块。可能导致这种情况的原因是什么?

public void Page_Load(object sender,System.EventArgs e)


{


// string sSQL;


试试


{


/ *


OleDbConnection2.Open();


sSQL =" SELECT Statement_Id,Statement_In_Brief FROM Statements" ;;


daStatements = new OleDbDataAdapter(sSQL,OleDbConnection1);


daStatements.Fill(dsStatements);


Response.Write(" Record count = " +


dsStatements.Tables [0] .Rows.Count.ToString());

string sSQL =" SELECT Statement_Id,Statement_In_Brief FROM Statements" ;;


daStatements = new OleDbDataAdapter(sSQL,OleDbConnection1);


daStatements.Fill(dsStatements," Statements");


DropDownList1.DataSource = dsStatements.Tables [" Statements"];


DropDownList1.DataTextField =" State ment_In_Brief" ;;


DropDownList1.DataValueField =" Statement_ID";


DropDownList1.DataBind();


Response.Write(" test");

OleDbConnection1.Close();


* /


}


catch(例外e)


{


//回复.Write(e.ToString);


}


} //结束Page_Load()事件

I''ve got some code that I''m trying to enclose in a try/catch block. This
code is in the Page_Load() method of the code-behind for an .aspx page.

On the line where the "try" keyword is, I''m getting the error "Object
reference is not set to an instance of an object". Why would I get that on
the "try" keyword. I''ve copied the code below, and you can see that I''ve
commented out the extra code to the point where all I have is the
Page_Load() event and the try/catch block. What could be causing this?
public void Page_Load(object sender, System.EventArgs e)

{

//string sSQL;

try

{

/*

OleDbConnection2.Open();

sSQL = "SELECT Statement_Id,Statement_In_Brief FROM Statements";

daStatements = new OleDbDataAdapter(sSQL,OleDbConnection1);

daStatements.Fill(dsStatements);

Response.Write( "Record count = " +

dsStatements.Tables[0].Rows.Count.ToString());
string sSQL = "SELECT Statement_Id,Statement_In_Brief FROM Statements";

daStatements = new OleDbDataAdapter(sSQL,OleDbConnection1);

daStatements.Fill(dsStatements,"Statements");

DropDownList1.DataSource = dsStatements.Tables["Statements"];

DropDownList1.DataTextField = "Statement_In_Brief";

DropDownList1.DataValueField = "Statement_ID";

DropDownList1.DataBind();

Response.Write("test");
OleDbConnection1.Close();

*/

}

catch(Exception e)

{

//Response.Write(e.ToString);

}

} // end Page_Load() event

推荐答案

你好,


你确定在调用之前实例化了OleDbConnection2

Open()方法?

如果是,那么检查你正在使用的UI元素是否相同 - 我怀疑

问题与他们有关,但是 - 他们应该由InitializeComponent()方法中的

设计器初始化。


所以 - 检查是否正在初始化OleDbConnection2 var。或者只是在它上面添加一个

额外的行 - 类似于:

OleDbConnection2 = new OleDbConnection([connectionstring goes here]);


干杯,

Branimir


-

Branimir Giurov

MCSD.NET,MCDBA,MCT

eAgility LLC


" Curt Emich" < CE **** @ comcast.net>在消息中写道

news:3f ******************** @ comcast.com ...
Hi there,

are you sure that the OleDbConnection2 is being instantiated before calling
the Open() method?
If "Yes", then check the same for the UI elements you''re using - i doubt
that the problem is with them , though - they should be initialized by the
designer inside the InitializeComponent() method.

So - check if the OleDbConnection2 var is being initialized. Or just put an
extra line on top of it - something like:

OleDbConnection2 = new OleDbConnection(["connectionstring goes here"]);

Cheers,
Branimir

--
Branimir Giurov
MCSD.NET, MCDBA, MCT
eAgility LLC

"Curt Emich" <ce****@comcast.net> wrote in message
news:3f********************@comcast.com...
I我有一些代码,我试图将其包含在try / catch块中。这个代码位于.aspx页面的代码隐藏的Page_Load()方法中。

在尝试的行上关键字是,我得到错误对象
引用未设置为对象的实例。为什么我会在尝试中获得
关键词。我已经复制了下面的代码,你可以看到我已经将额外的代码注释掉了,我所拥有的只是
Page_Load()事件和try / catch块。可能导致这种情况的原因是什么?

public void Page_Load(object sender,System.EventArgs e)


//字符串sSQL;

尝试



/ *

OleDbConnection2.Open();

sSQL = SELECT Statement_Id,Statement_In_Brief FROM Statements;

daStatements = new OleDbDataAdapter(sSQL,OleDbConnection1);

daStatements.Fill(dsStatements);

Response.Write(" Record count =" +

dsStatements.Tables [0] .Rows.Count.ToString());

string sSQL =" SELECT Statement_Id,Statement_In_Brief FROM Statements" ;;

daStatements = new OleDbDataAdapter(sSQL,OleDbConnection1);

daStatements.Fill(dsStatements," Statements");

DropDownList1.DataSource = dsStatements.Tables [" Statements"];

DropDownList1.DataTextField =" Statement_In_Brief";

DropDownList1.DataValueField =" Statement_ID" ; ;

DropDownList1.DataBind();

Response.Write(" test");

OleDbConnection1.Close();

* /

}抓住(例外e)

//响应。写(e.ToString);

}
} //结束Page_Load()事件
I''ve got some code that I''m trying to enclose in a try/catch block. This
code is in the Page_Load() method of the code-behind for an .aspx page.

On the line where the "try" keyword is, I''m getting the error "Object
reference is not set to an instance of an object". Why would I get that on the "try" keyword. I''ve copied the code below, and you can see that I''ve
commented out the extra code to the point where all I have is the
Page_Load() event and the try/catch block. What could be causing this?
public void Page_Load(object sender, System.EventArgs e)

{

//string sSQL;

try

{

/*

OleDbConnection2.Open();

sSQL = "SELECT Statement_Id,Statement_In_Brief FROM Statements";

daStatements = new OleDbDataAdapter(sSQL,OleDbConnection1);

daStatements.Fill(dsStatements);

Response.Write( "Record count = " +

dsStatements.Tables[0].Rows.Count.ToString());
string sSQL = "SELECT Statement_Id,Statement_In_Brief FROM Statements";

daStatements = new OleDbDataAdapter(sSQL,OleDbConnection1);

daStatements.Fill(dsStatements,"Statements");

DropDownList1.DataSource = dsStatements.Tables["Statements"];

DropDownList1.DataTextField = "Statement_In_Brief";

DropDownList1.DataValueField = "Statement_ID";

DropDownList1.DataBind();

Response.Write("test");
OleDbConnection1.Close();

*/

}

catch(Exception e)

{

//Response.Write(e.ToString);

}

} // end Page_Load() event



" Curt Emich" < CE **** @ comcast.net>写在

新闻:3f ******************** @ comcast.com:
"Curt Emich" <ce****@comcast.net> wrote in
news:3f********************@comcast.com:
我有一些代码,我试图将其包含在try / catch
块中。此代码位于.aspx页面的代码隐藏的Page_Load()方法中。

在尝试的行上关键字是,我收到错误
对象引用未设置为对象的实例。为什么
我会在尝试中得到它关键词。我已经复制了下面的代码
,你可以看到我已经注释了额外的代码,以便我所拥有的是Page_Load()事件和
try / catch块。可能导致这种情况的原因是什么?
I''ve got some code that I''m trying to enclose in a try/catch
block. This code is in the Page_Load() method of the
code-behind for an .aspx page.

On the line where the "try" keyword is, I''m getting the error
"Object reference is not set to an instance of an object". Why
would I get that on the "try" keyword. I''ve copied the code
below, and you can see that I''ve commented out the extra code to
the point where all I have is the Page_Load() event and the
try/catch block. What could be causing this?




Curt,


只是为了澄清:尝试的例外情况正在发生关键字甚至

所有其他代码被注释掉了吗?


如果是这种情况,如果你使用的是Visual Studio 2002,那么' VS2002中的
a bug,当发生异常时,调试器会突出显示错误的行

。 MSDN上有补丁。


如果您没有使用VS2002,请提供更多信息:


- 。网络版

- 你正在使用哪个IDE(VS2003,SharpDevelop等)

- Windows版本

- 一个小的完整代码示例演示这个问题,或者如果不可能的话,那就是
,然后展开你发布的代码片段

,这样它就包含了之前和之后的更多代码

发生异常的地方。


希望这会有所帮助。


克里斯。

---- ---------

CR Timmons Consulting,Inc。
http://www.crtimmonsinc.com/


这篇关于尝试关键字导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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