try keyword causing problems

C

Curt Emich

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
 
B

Branimir Giurov

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
 
C

Chris R. Timmons

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,

Just to clarify: the exception is occurring on the "try" keyword even
with all of the other code commented out?

If that's the case, and if you are using Visual Studio 2002, there's
a bug in VS2002 where the debugger would highlight the wrong line
when an exception occurred. There's a patch on MSDN.

If you're not using VS2002, could you please give more info:

- .Net version
- Which IDE you are using (VS2003, SharpDevelop, etc.)
- Version of Windows
- A small complete code sample demonstrating the problem, or
if that's not possible, then expand the code fragment
you posted so it includes more code from before and after
where the exception occurred.

Hope this helps.

Chris.
 
C

Curt Emich

I'm using the Microsoft Development Environment 2002. I couldn't see a
patch on the link you gave me, so I clicked "check for updates", but was
told I have the latest.

- .Net version - 1.0.3705

- Which IDE you are using (VS2003, SharpDevelop, etc.)

Version 7.0.9466


- Version of Windows

XP


- A small complete code sample demonstrating the problem, or
if that's not possible, then expand the code fragment
you posted so it includes more code from before and after
where the exception occurred.

Hope this helps.



I'll paste the code below. Everything is commented out, so I'm not sure
why I'd get this problem.





using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

namespace TrainOfThought
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class TrainOfThought : System.Web.UI.Page
{
protected System.Data.OleDb.OleDbConnection OleDbConnection1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Data.DataSet dsStatements;
protected System.Data.OleDb.OleDbConnection oleDbConnection2;
protected System.Data.OleDb.OleDbDataAdapter daStatements;


private void InitializeComponent()
{
/* this.oleDbConnection2 = new
System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Pas
sword="""";User ID=Admin;Data
Source=C:\Inetpub\wwwroot\TrainOfThought\data\TrainOfThought.mdb;Mode=Sh
are Deny None;Extended Properties="""";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database
Password="""";Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking
Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk
Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create
System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't
Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;Jet OLEDB:SFP=False");
//
// oleDbConnection2
//
//this.oleDbConnection2.ConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source=C:\Inetpub\wwwroot\TrainOfThought\data\TrainOfThought.mdb;Mode=Sh
are Deny None;Extended Properties="""";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database
Password="""";Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking
Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk
Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create
System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't
Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;Jet OLEDB:SFP=False";
this.Load += new System.EventHandler(this.Page_Load);
*/
}

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



} // end class TrainOfThought
} // end namespace TrainOfThought
 
C

Curt Emich

I put this code in the InitializeComponent() method, but still no dice:



this.oleDbConnection2 = new
System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Pas
sword="""";User ID=Admin;Data
Source=C:\Inetpub\wwwroot\TrainOfThought\data\TrainOfThought.mdb;Mode=Sh
are Deny None;Extended Properties="""";Jet OLEDB:System
database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database
Password="""";Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking
Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk
Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create
System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't
Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica
Repair=False;Jet OLEDB:SFP=False");
//
 
C

Chris R. Timmons

I'm using the Microsoft Development Environment 2002. I
couldn't see a patch on the link you gave me, so I clicked
"check for updates", but was told I have the latest.

- .Net version - 1.0.3705

Curt,

My mistake. I meant to say .Net 1.0, not VSNet 2002. Do you have
the .Net 1.0 Service Pack 2 installed? If not, you can get it here:

http://msdn.microsoft.com/netframework/downloads/updates/sp/default.a
spx

or

http://tinyurl.com/b34o

I'll paste the code below. Everything is commented out, so I'm
not sure why I'd get this problem.

The code doesn't compile. The variable "e" is used twice in the
Page_Load method (once in the parameter list, and once in the "catch"
block). Removing the "e" variable from the "catch" block allowed the
code to compile. I compiled and ran it under .Net 1.1, and it worked
fine (I don't have 1.0 installed).

Chris.
 
C

Curt Emich

I changed the second "e" to "ex" and that seemed to solve the problem.
Thanks.

Now it doesn't seem to want to go into the InitializeComponent() method.
Any ideas as to why this might be?
 
C

Chris R. Timmons

I changed the second "e" to "ex" and that seemed to solve the
problem. Thanks.

Now it doesn't seem to want to go into the InitializeComponent()
method. Any ideas as to why this might be?

Curt,

VS should have created a method like this for your page:

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

If it's not there, then InitializeComponent won't get called.


Chris.
 
R

Ravichandran J.V.

It could be due to the oleDbConnection2 object that you have used. You
are opening the oleDbConnection2 but you are using the oleDbConnection1
object.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
C

Curt Emich

Somehow OnInit() got erased. That was the problem. Thanks to everyone
for your help!
 

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