Thursday, March 22, 2012

Object reference not set to an instance of an object Again!

Hi All,

I searched through the forum for this problem and found many replies however could not get the exact cause of the problem. Here is the problem I am facing:

I build a oledbconnection that reside in a function in another module and use the connection from an aspx page. The code works fine if I open a single page (session) however if I open another page and execute the code in order to simulate multiple connections to the page I get the below message with the below stack

Object reference not set to an instance of an object.

Line 141: strSQL = "SELECT Count(Islem_No) FROM tbl_mambar02_manuel_islem where islem_tipi = 2 and tarih >= to_date('" & dtBaslang?c.ToShortDateString() & "','dd.mm.yyyy') and tarih <= to_date('" & dtBitis.ToShortDateString() & "','dd.mm.yyyy') and to_char(tarih,'hh24:mi:ss') >= '" & dtZamanBaslangic.ToLongTimeString() & "' and to_char(tarih,'hh24:mi:ss') <= '" & dtZamanBitis.ToLongTimeString() & "'"


Line 142: dbcmdPalet.CommandText = strSQL

Line 143: m_stPaletHareket.lManuelGiris += dbcmdPalet.ExecuteScalar()

---

NullReferenceException: Object reference not set to an instance of an object.]
System.Data.OleDb.OleDbConnection.IDBCreateCommand() +26
System.Data.OleDb.OleDbConnection.ICommandText() +47
System.Data.OleDb.OleDbCommand.InitializeCommand(CommandBehavior behavior, Boolean throwifnotsupported) +64
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +22
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +112
System.Data.OleDb.OleDbCommand.ExecuteScalar() +184
Mambar2Web.PaletHareketIzleme.PaletHareketBul(DateTime dtBaslang?c, DateTime dtBitis, DateTime dtZamanBaslangic, DateTime dtZamanBitis) in c:\inetpub\wwwroot\Mambar2Web\PaletHareketIzleme.aspx.vb:143
Mambar2Web.PaletHareketIzleme.btnGoster_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Mambar2Web\PaletHareketIzleme.aspx.vb:283
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277

The related code is as follows:

In Aspx page:

OpenDB()

Dim dbcmdPaletAs OleDbCommand =New OleDbCommand

strSQL = "SELECT Count(distinct G.Giris_No) FROM tbl_mambar02_giris G,tbl_mambar02_palet P where G.Giris_No = P.Giris_No and G.Giris_Tipi = 0 and G.Ambara_giris_tarihi >= to_date('" & dtBaslang?c.ToShortDateString() & "','dd.mm.yyyy') and G.Ambara_Giris_Tarihi <= to_date('" & dtBitis.ToShortDateString() & "','dd.mm.yyyy') and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') >= '" & dtZamanBaslangic.ToLongTimeString() & "' and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') <= '" & dtZamanBitis.ToLongTimeString() & "' order by G.Giris_No"

dbcmdPalet.CommandText = strSQL

dbcmdPalet.Connection = db

m_stPaletHareket.lYeniGiris += dbcmdPalet.ExecuteScalar()

--

db.close()

--OpenDB is defined as follows in Module1 :

Module Module1

Public dbAsOleDb.OleDbConnection

PubicSub OpenDB()

db =New OleDb.OleDbConnection("Provider=MSDAORA.1;Password=MAMBUSER;User ID=XXXXX;Data Source=CAYIROVA")

db.Open()

EndSub

EndModule

--

If I utilize a local connection in the ASPX page it works OK. However other ASPX pages need to utilize the same connection also . Any help would be appreciated.

Best regards

Asaf

Where is the connection opened in the code showing in the error?

m_stPaletHareket.lManuelGiris += dbcmdPalet.ExecuteScalar()

In addition, the code shown in the error is different from the code you posted.


Hi Brian,

OK I skipped some code. The code is as follows:

OpenDB()

strSQL = "SELECT Count(distinct G.Giris_No) FROM tbl_mambar02_giris G,tbl_mambar02_palet P where G.Giris_No = P.Giris_No and G.Giris_Tipi = 0 and G.Ambara_giris_tarihi >= to_date('" & dtBaslang?c.ToShortDateString() & "','dd.mm.yyyy') and G.Ambara_Giris_Tarihi <= to_date('" & dtBitis.ToShortDateString() & "','dd.mm.yyyy') and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') >= '" & dtZamanBaslangic.ToLongTimeString() & "' and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') <= '" & dtZamanBitis.ToLongTimeString() & "' order by G.Giris_No"

dbcmdPalet.CommandText = strSQL

dbcmdPalet.Connection = db

m_stPaletHareket.lYeniGiris += dbcmdPalet.ExecuteScalar()

strSQL = "SELECT Count(distinct G.Giris_No) FROM tbl_mambar02_giris G,tbl_mambar02_palet P where G.Giris_No = P.Giris_No and G.Giris_Tipi = 1 and G.Ambara_giris_tarihi >= to_date('" & dtBaslang?c.ToShortDateString() & "','dd.mm.yyyy') and G.Ambara_Giris_Tarihi <= to_date('" & dtBitis.ToShortDateString() & "','dd.mm.yyyy') and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') >= '" & dtZamanBaslangic.ToLongTimeString() & "' and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') <= '" & dtZamanBitis.ToLongTimeString() & "'order by G.Giris_No"

dbcmdPalet.CommandText = strSQL

m_stPaletHareket.lGeriDonus += dbcmdPalet.ExecuteScalar()

strSQL = "SELECT Count(Islem_No) FROM tbl_mambar02_manuel_islem where islem_tipi = 2 and tarih >= to_date('" & dtBaslang?c.ToShortDateString() & "','dd.mm.yyyy') and tarih <= to_date('" & dtBitis.ToShortDateString() & "','dd.mm.yyyy') and to_char(tarih,'hh24:mi:ss') >= '" & dtZamanBaslangic.ToLongTimeString() & "' and to_char(tarih,'hh24:mi:ss') <= '" & dtZamanBitis.ToLongTimeString() & "'"

dbcmdPalet.CommandText = strSQL

m_stPaletHareket.lManuelGiris += dbcmdPalet.ExecuteScalar()

Hence the connection is assigned as "db" oledbconnection, which is declared in the module as public variable.

Thanks for your attention.

Asaf


Hi Brian,

OK I skipped some code. The code is as follows:

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

OpenDB()

strSQL = "SELECT Count(distinct G.Giris_No) FROM tbl_mambar02_giris G,tbl_mambar02_palet P where G.Giris_No = P.Giris_No and G.Giris_Tipi = 0 and G.Ambara_giris_tarihi >= to_date('" & dtBaslang?c.ToShortDateString() & "','dd.mm.yyyy') and G.Ambara_Giris_Tarihi <= to_date('" & dtBitis.ToShortDateString() & "','dd.mm.yyyy') and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') >= '" & dtZamanBaslangic.ToLongTimeString() & "' and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') <= '" & dtZamanBitis.ToLongTimeString() & "' order by G.Giris_No"

dbcmdPalet.CommandText = strSQL

dbcmdPalet.Connection = db

m_stPaletHareket.lYeniGiris += dbcmdPalet.ExecuteScalar()

strSQL = "SELECT Count(distinct G.Giris_No) FROM tbl_mambar02_giris G,tbl_mambar02_palet P where G.Giris_No = P.Giris_No and G.Giris_Tipi = 1 and G.Ambara_giris_tarihi >= to_date('" & dtBaslang?c.ToShortDateString() & "','dd.mm.yyyy') and G.Ambara_Giris_Tarihi <= to_date('" & dtBitis.ToShortDateString() & "','dd.mm.yyyy') and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') >= '" & dtZamanBaslangic.ToLongTimeString() & "' and to_char(G.Ambara_Giris_Tarihi,'hh24:mi:ss') <= '" & dtZamanBitis.ToLongTimeString() & "'order by G.Giris_No"

dbcmdPalet.CommandText = strSQL

m_stPaletHareket.lGeriDonus += dbcmdPalet.ExecuteScalar()

strSQL = "SELECT Count(Islem_No) FROM tbl_mambar02_manuel_islem where islem_tipi = 2 and tarih >= to_date('" & dtBaslang?c.ToShortDateString() & "','dd.mm.yyyy') and tarih <= to_date('" & dtBitis.ToShortDateString() & "','dd.mm.yyyy') and to_char(tarih,'hh24:mi:ss') >= '" & dtZamanBaslangic.ToLongTimeString() & "' and to_char(tarih,'hh24:mi:ss') <= '" & dtZamanBitis.ToLongTimeString() & "'"

dbcmdPalet.CommandText = strSQL

m_stPaletHareket.lManuelGiris += dbcmdPalet.ExecuteScalar()

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

Hence the connection is assigned as "db" oledbconnection, which is declared in the module as public variable.

Thanks for your attention.

Asaf


What is:

m_stPaletHareket.lYeniGiris
m_stPaletHareket.lGeriDonus
m_stPaletHareket.lManuelGiris

Is this a business object, or a custom class, or what?


I defined it as a structure in the aspx.vb codebehind page:

PublicStructure stGirisCikisSayisi

Public lYeniGirisAsLong

Public lGeriDonusAsLong

Public lManuelGirisAsLong

Public lCikisAsLong

Public lManuelCikisAsLong

EndStructure

PublicClass PaletHareketIzleme

Inherits System.Web.UI.Page

Dim m_stPaletHareketAsstGirisCikisSayisi

--

EndClass

It is basically a structure that keeps records of the variables defined.

Sorry due to time zone difference, there is a delay in my responses. Thanks for your attention.

Asaf


Did you instantiate the structure reference in your code, and are you storing the reference among postbacks? You need to store it in the cache or session across page postbacks to retain the reference.


Brian,

If I declare the oledbconnection localy in the aspx page and call a local function as follows, the problem is solved :

Page1.Aspx.vb:

Dim oledbDataAs OleDb.OleDbConnection

PublicSub OpenDataDB()

oledbData =New OleDb.OleDbConnection("Provider=MSDAORA.1;Password=MAMBUSER;User ID=XXXXX;Data Source=CAYIROVA")

oledbData.Open()

EndSub

On the other hand if I utilize the oledbconnection defined globally in the module as I stated previously, I get the error.

Module Module1

Public dbAsOleDb.OleDbConnection

PubicSub OpenDB()

db =New OleDb.OleDbConnection("Provider=MSDAORA.1;Password=MAMBUSER;User ID=XXXXX;Data Source=CAYIROVA")

db.Open()

EndSub

EndModule

If I use this global variable, I get the "Object reference not set to an instance of an object. " when multiple connections are made to the page. Iwould appreciate your answer.

Best Regards

Asaf


Modules are static classes; I would think that you would have to reference it by Module1.db.

Tried it and added Module1 to the global variable "db" as Module1.db and called Module1.OpenDb() , however the same error message is displayed. Even though I solved the problem by providing a local oledbconnection, it will be nice to know why it is behaving this way. Any ideas appreciated!

Asaf

0 comments:

Post a Comment