Monday, March 26, 2012

Object reference not set to an instance of an object

Hi: I have a problem, the error message is:

Object reference not set to an instance of an object

If GetDataFromTable(strMyTableName, GridView1)Then....End IfProtected Function GetDataFromTable(ByVal strMyTableNameAs String,ByRef gvMyGridViewAs GridView)As Boolean Try Dim dsAs New Data.DataSet'DataSetDim daAs OleDbDataAdapterDim strSQLAs String Dim tempRowAs Data.DataRowDim contblRepAs OleDbConnectionDim strConnAs String Dim strTypeAs String Dim intRepAs Int32Dim intRowsAs Int32 strConn = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString strSQL ="Select FoodCode,FoodName,FoodPrice,IsSpicy from " & strMyTableName contblRep =New OleDbConnection(strConn) da =New OleDbDataAdapter(strSQL, contblRep) da.Fill(ds)'fill the ds from da ds.Tables(0).Columns.Add("imgFile")'add extra column intRows = ds.Tables(0).Rows.Count.ToString()'get the total rows in the tables GridView1.DataSource = ds GridView1.DataBind() contblRep.Close()Return True'Catch ex As ExceptionCatch exAs ExceptionDim lblAs New LabelDim strMsgAs String = ex.Message lbl.Text ="<script language='javascript'>" & Environment.NewLine _ & "window.alert(" & "'" & strMsg & "'" & ")</script>" Page.Controls.Add(lbl)Return False End TryEnd Function

I don't mean you help me to read the code line by line, will be great if you point out why the error happens. The error message doesn't show on my local machine using ASP Express, but if I upload to a web hosting server, it pops up.

Thanks a lot.

Jt

Have you tried stripping the code out of the function, and just populating the Gridview, using the code (if so, does that work)?

What line does the error fall on?


Hi: Augustwind

Thanks for your suggestion, this is what I found:

Line1 strSQL = "Select FoodCode,FoodName,FoodPrice,IsSpicy from " & strMyTableName

Line2 strConn = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString

Line3 contblRep = New OleDbConnection(strConn)

I found the Line2 Line3 will cause the Erro msg,

But If I write:

strSQL = "Select FoodCode,FoodName,FoodPrice,IsSpicy from " & strMyTableName

contblRep = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\NewPandaDB.mdb")

Works find, that means: ConfigurationManager.ConnectionStrings is not working in my web hosting server. (It works here in my Pc, I am using ASP2.0 Express, OS is XP). I have no idea why. So far, I have to hard code the line: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\NewPandaDB.mdb, I have to find a better way.

If you have any suggestion, please let me know. thanks.


I think the Namespace System.Configuration is missing:

try:

System.Configuration.ConfigurationManager.ConnectionStrings("myConnectionString").ToString

Chris


Hi,

I think you are missing the entry in Web.Config for the following key: "myConnectionString" in ConnectionStrings section. Are you just copy pasting the pages you need on the web server? Try using Publishing the WebSite. To 'Publish', right click on the Web Site in Solution Explorer and select 'Publish'.


Hi: Chris and Sujitm

Thanks for your suggestions, I did both, it works fine now. I am not exactly sure which one works for me, may be combine both.

Cheers

Jt

0 comments:

Post a Comment