I try to use this piece of code...
Dim CatID As Integer
CatID = (Request.QueryString("ID"))
CatID = 1
myProductsByCategory.Text = (GetProductsByCategory(CatID))
inside my Private Sub Page_Load but I get a System.NullReferenceException error from this.
What am I missing here?
Regards
MMy guess would be that the Querystring "ID" doesnt exist and you are getting a NULL exception when VB is converting it to an integer.
I hit Post too soon.. you might want to try to comment out the line that is assigning the value from the Querystring, since you are hardcoding it anyway (to 1).. see if thats the issue.
Hi
I comment out the querystring part, but that didn't help...
Regards
M
By the way... the function I try to call looks like this..
Function GetProductsByCategory(ByVal intID As Integer)
Dim connStr As String
connStr = ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connStr")Dim sb As StringBuilder = New StringBuilder
Dim sbOutput As StringDim MyConnection As SqlConnection
Dim MyCommand As SqlCommand
Dim MyDataReader As SqlDataReaderMyConnection = New SqlConnection(connStr)
Try
MyConnection.Open()
MyCommand = New SqlCommand("dbo.p_ShowProductsByCategories", MyConnection)
MyCommand.CommandType = CommandType.StoredProcedure
MyCommand.Parameters.Add("@.ID", SqlDbType.Int, 4).Value = intID
MyDataReader = MyCommand.ExecuteReader()sb.Append("<table width=""123"" border=""0"" cellpadding=""0"" cellspacing=""0"">")
While MyDataReader.Read
sb.Append("<tr>")
sb.Append("<td class=""menuitem"" onmouseover=""this.bgColor='#EEEEEE'"" onmouseout=""this.bgColor='#FDFDFD'"" style=""HEIGHT: 16px""> <a ref=""productdetail.aspx?ID=""" & (MyDataReader("ID")) & """>" & (MyDataReader("Description")) & "</a></td>" & vbCrLf)
sb.Append("</tr>")
End Whilesb.Append("</table>")
sbOutput = sb.ToString()
MyDataReader.Close()
MyConnection.Close()Catch ex As Exception
myErrorCode.Visible = True
myErrorCode.Text = (" ! An error occured <br> while accesing<br> products")
'Response.Write(ex.Message.ToString)
End Try
GetProductsByCategory = sbOutput
End Function
Regards
M
Ok.. is the runtime error not giving you the line number of the error? Are you sure the error is happening in those original 4 lines of code posted?
Try setting a breakpoint on the offending line and stepping through the GetProductsByCategory method to see where the problem lies.
(If you don't have Visual Studio you can use the CLR debugger in the .net framework SDK. You'll find it at
C:\Program Files\Microsoft.NET\FrameworkSDK\GuiDebug\DbgCLR.exe
You'll need to attach it to the aspnet_wp.exe process first. Choose "Debug Processes..." on the "Tools" menu, select aspnet_wp.exe and click "Attach".)
You are right, sorry! The line where the error is ...
myProductsByCategory.Text = (GetProductsByCategory(1))
Regards
M
Ok.. best case, your myProductsByCategory.Text is the problem.. worse case its a problem in the function, which is most likely the problem :)
connStr = ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connStr")
Are yousure that you have this entry in your web.config file?
Yes I have, I use that part in the sub page_load so I know thats working.
Regards
M
Time to step through your code and see whats up then :)
Hmmm... I found the problem, I have forgotten to add the runat=server to the label holding the result.
Regards
M
0 comments:
Post a Comment