Friday, March 16, 2012

Object reference not set to an instance of an object.

Hi.

After finish a site on my own pc with VS2005 and i have run it with no problems i uploadet it to my host, i get this error:

Object reference not set to an instance of an object.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 156: ' Database Conn 2 Luk DBLine 157: Private Sub CloseDB2()Line 158: DBConnection2.Close()Line 159: DBConnection2 = NothingLine 160: DBAdapter2.Dispose()


Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] DefaultVB.CloseDB2() in d:\web\localuser\mydomain.dk\public_html\Default.aspx.vb:158 DefaultVB.Page_Unload(Object sender, EventArgs e) in d:\web\localuser\mydomain.dk\public_html\Default.aspx.vb:66 System.Web.UI.Control.OnUnload(EventArgs e) +2067916 System.Web.UI.Control.UnloadRecursive(Boolean dispose) +267 System.Web.UI.Page.UnloadRecursive(Boolean dispose) +20 System.Web.UI.Page.ProcessRequestCleanup() +40 System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +154 System.Web.UI.Page.ProcessRequest() +86 System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +18 System.Web.UI.Page.ProcessRequest(HttpContext context) +49 ASP.default_aspx.ProcessRequest(HttpContext context) +29 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +154 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64

My code_behind file is:

Imports SystemImports Telerik.QuickStartImports Telerik.WebControlsImports System.CollectionsImports System.ComponentModelImports System.DataImports System.DrawingImports System.WebImports System.Web.SessionStateImports System.Web.UIImports System.Web.UI.WebControlsImports System.Web.UI.HtmlControlsImports System.Data.OleDbImports System.ConfigurationImports Microsoft.VisualBasicPartialPublic Class DefaultVBInherits System.Web.UI.Page' Dim As s?tninger hvis skal bruges i flere Sub'sDim DBConnectionAs OleDbConnectionDim DBAdapterAs OleDbDataAdapterDim DBDataSetAs DataSetDim DBDataViewAs DataViewDim SQLStringAs String Dim DBConnection2As OleDbConnectionDim DBAdapter2As OleDbDataAdapterDim DBDataSet2As DataSetDim DBDataView2As DataViewDim SQLString2As String Dim DBConnection3As OleDbConnectionDim DBAdapter3As OleDbDataAdapterDim DBDataSet3As DataSetDim DBDataView3As DataViewDim SQLString3As String Dim DBConnection4As OleDbConnectionDim DBAdapter4As OleDbDataAdapterDim DBDataSet4As DataSetDim DBDataView4As DataViewDim SQLString4As String Dim DBConnection5As OleDbConnectionDim DBAdapter5As OleDbDataAdapterDim DBDataSet5As DataSetDim DBDataView5As DataViewDim SQLString5As String' K?rsel af diverse ting i Page_LoadSub Page_Load(ByVal senderAs Object,ByVal eAs EventArgs)Handles Me.LoadIf Request.QueryString("pageidentity") =Nothing OrElse Request.QueryString("pageidentity") =""Then Session("pageid") ="1"ElseIf Request.QueryString("pageidentity") <>""Then Session("pageid") = Request.QueryString("pageidentity").ToStringEnd If' Subs der skal k?res i Page_Load OpenDB1() OpenDB2() OpenDB3() OpenDB4() OpenDB5() LoadMenu()End Sub' K?rsel af diverse ting i Page_UnloadSub Page_Unload(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Unload' Subs der skal k?res i Page_Unload CloseDB1() CloseDB2() CloseDB3() CloseDB4() CloseDB5()End Sub' Database Conn 1 ?ben DBPrivate Sub OpenDB1()' Connection til en database samt SQL Select forsp?rgsel DBConnection =New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDogs").ConnectionString) SQLString ="SELECT * FROM KennelEnggaardDogs WHERE OwnDog ='YES' ORDER BY DogID ASC;" DBAdapter = New OleDbDataAdapter(SQLString, DBConnection) DBDataSet = New DataSet() DBAdapter.Fill(DBDataSet, "KennelEnggaardDogs")' Hvis der ikke er data i databasen eller ingen recorder at vise, udskriv til en Label med id=NoRows If (DBDataSet.Tables(0).Rows.Count = 0) Then NoRows.Text = "<center><b>Vi har pt. ingen hunde selv.</b></center>" Else' Udskriv data til en Repeater med id=RepeaterShowOwnDogs DBDataView = New DataView(DBDataSet.Tables("KennelEnggaardDogs")) RepeaterShowOwnDogs.DataSource = DBDataView RepeaterShowOwnDogs.DataBind() End If End Sub' Database Conn 2 ?ben DB Private Sub OpenDB2()' Connection til en database samt SQL Select forsp?rgsel DBConnection2 = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDogs").ConnectionString) SQLString2 = "SELECT * FROM KennelEnggaardDogs WHERE OwnDog ='YES' ORDER BY DogID ASC;" DBAdapter2 =New OleDbDataAdapter(SQLString2, DBConnection2) DBDataSet2 =New DataSet() DBAdapter2.Fill(DBDataSet2,"KennelEnggaardDogs")' Hvis der ikke er data i databasen eller ingen recorder at vise, udskriv til en Label med id=NoRows2If (DBDataSet2.Tables(0).Rows.Count = 0)Then NoRows2.Text ="<center><b>Vi har pt. ingen hunde til salg, men kontakt os endelig.</b></center>"Else' Udskriv data til en Repeater med id=RepeaterShowSalesDogs DBDataView2 =New DataView(DBDataSet2.Tables("KennelEnggaardDogs")) RepeaterShowSalesDogs.DataSource = DBDataView2 RepeaterShowSalesDogs.DataBind()End If End Sub' Database Conn 3 ?ben DBPrivate Sub OpenDB3()' Connection til en database samt SQL Select forsp?rgsel DBConnection3 =New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDogs").ConnectionString) SQLString3 ="SELECT * FROM KennelEnggaardText WHERE MainTextID =" & Session("pageid") &"" DBAdapter3 =New OleDbDataAdapter(SQLString3, DBConnection3) DBDataSet3 =New DataSet() DBAdapter3.Fill(DBDataSet3,"KennelEnggaardText")' Udskriv data til en FormView med id=FormViewMainText DBDataView3 =New DataView(DBDataSet3.Tables("KennelEnggaardText")) FormViewMainText.DataSource = DBDataView3 FormViewMainText.DataBind()End Sub' Database Conn 4 ?ben DBPrivate Sub OpenDB4()' Connection til en database samt SQL Select forsp?rgsel DBConnection4 =New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDogs").ConnectionString) SQLString4 ="SELECT * FROM KennelEnggaardText WHERE MainTextID = 2" DBAdapter4 =New OleDbDataAdapter(SQLString4, DBConnection4) DBDataSet4 =New DataSet() DBAdapter4.Fill(DBDataSet4,"KennelEnggaardText")' Udskriv data til en FormView med id=FormViewSubText DBDataView4 =New DataView(DBDataSet4.Tables("KennelEnggaardText")) FormViewSubText.DataSource = DBDataView4 FormViewSubText.DataBind()End Sub' Database Conn 5 ?ben DBPrivate Sub OpenDB5()' Connection til en database samt SQL Select forsp?rgsel DBConnection5 =New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDogs").ConnectionString) SQLString5 ="SELECT * FROM KennelEnggaardCont WHERE ContID = 1" DBAdapter5 =New OleDbDataAdapter(SQLString5, DBConnection5) DBDataSet5 =New DataSet() DBAdapter5.Fill(DBDataSet5,"KennelEnggaardCont")' Udskriv data til en FormView med id=FormViewCont DBDataView5 =New DataView(DBDataSet5.Tables("KennelEnggaardCont")) FormViewCont.DataSource = DBDataView5 FormViewCont.DataBind()End Sub' Database Conn 1 Luk DBPrivate Sub CloseDB1() DBConnection.Close() DBConnection =Nothing DBAdapter.Dispose() DBAdapter =Nothing End Sub' Database Conn 2 Luk DBPrivate Sub CloseDB2() DBConnection2.Close() DBConnection2 =Nothing DBAdapter2.Dispose() DBAdapter2 =Nothing End Sub' Database Conn 3 Luk DBPrivate Sub CloseDB3() DBConnection3.Close() DBConnection3 =Nothing DBAdapter3.Dispose() DBAdapter3 =Nothing End Sub' Database Conn 4 Luk DBPrivate Sub CloseDB4() DBConnection4.Close() DBConnection4 =Nothing DBAdapter4.Dispose() DBAdapter4 =Nothing End Sub' Database Conn 5 Luk DBPrivate Sub CloseDB5() DBConnection5.Close() DBConnection5 =Nothing DBAdapter5.Dispose() DBAdapter5 =Nothing End Sub' Behandling af MenuTabs 1/3Private Sub LoadMenu()Dim tabAs Tab = RadTabStrip1.FindTabByUrl(Request.Url.PathAndQuery)If Not (tabIs Nothing)Then tab.SelectParents()End If End Sub' Behandling af MenuTabs 2/3Protected Overrides Sub OnInit(ByVal eAs EventArgs) InitializeComponent()MyBase.OnInit(e)End Sub'OnInit ' Behandling af MenuTabs 3/3Private Sub InitializeComponent()End Sub'InitializeComponent ' Omregning af Hundens alder i ?r og Mdr. via datediff af dato nu og f?dtPublic Function GetAge(ByVal objAs Object)As String Dim bornAs DateTime = DateTime.ParseExact(obj.ToString(),"dd-MM-yyyy hh:mm:ss", System.Threading.Thread.CurrentThread.CurrentCulture)Dim ageAs TimeSpan = DateTime.Now.Subtract(born)Dim theRealAgeAs New DateTime(age.Ticks)Return (theRealAge.Year - 1) &" ?r " & (theRealAge.Month - 1) &" m?neder"End FunctionEnd Class

Hi.

So what do i have to do !? just remove the closeDB() !? or !?

and how do i use the Try...Finally code im my code !?


DBAdapter2.Fill(DBDataSet2,"KennelEnggaardDogs")
Hello,

Your problem is puzzling, but I believe theres an easy way to prevent it from happening, First issue is this line:

siraero:

Private Sub OpenDB2()' Connection til en database samt SQL Select forsp?rgsel DBConnection2 =New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringDogs").ConnectionString)
Because your not explicitly calling DBConnection2.Open() , your DataAdapter on the following line is both opening the connection and closing it when you are done (seehttp://authors.aspalliance.com/aspxtreme/sys/data/Common/DataAdapterClassFill.aspx):

siraero:

DBAdapter2.Fill(DBDataSet2,"KennelEnggaardDogs")
 Therefore the way you are using this, there is no need for you to call your close methods on your Page.Unload event because your connections are already closed. For an even better way you should consider using Try..Finally block to ensure your connections are always closed.
 

before the unload event the data adapter will automatically close your connections, so to prevent this on load event insert your code inside the try catch finally blocks.

HC


Yes but how can i make my connection, maybe Sub OpenDB(), so it works with the Try-catch-finally and do i still just call em in the Page_Load as now but delete the CloseDB subs.

Im new in .net so hope u can help me.


I believe you dont have to call yourSub CloseDB1(),Sub CloseDB2(), etc on page_unload event, first because the data adapter will automatically open and close the connection to the databse so you dont need to handle it manually.Just to try it remove the code insideSub Page_Unload and try it,

HC


hi.

i just place a ' tage infront of the CloseDB#() and now it works.

do i then need the try cacht finally !?


It's better to put them in the try catch finally blocks in this case if an error occured during opening, retrieving data, and binding, you can handle it in the catch area.

try

{

OpenDB1()
OpenDB2()
OpenDB3()
OpenDB4()
OpenDB5()
LoadMenu()
}

catch(Exception ex)

{

Response.Write(ex.Message)

}

finally
{

CloseDB1()
CloseDB2()
CloseDB3()
CloseDB4()
CloseDB5()
}

HC

0 comments:

Post a Comment