-1

I am having some trouble getting my page running. I keep getting a NullException Error and am not sure what to do to get my code to be able to run. Here is my current code.

Imports System.Data
Imports System.Data.SqlClient

Partial Class Proj_product
    Inherits System.Web.UI.Page   

    Private Sub ProductDisplayValues(ByVal temp As String)
        Dim SearchId As Int32 = Convert.ToInt32(temp) 'Searching for a product with this ID
        Session("ProductPicID") = SearchId 'Record SearchID as a session variable
        Dim myGlobal As New clsGlobal

        'declare and open a connection to the Catalog table. 
        'Select the product with ID number equal to SearchID
        Dim cnnSQL As New SqlConnection(myGlobal.cnnstring)
        Dim cmd As New SqlCommand("Select PROD_NAME, PROD_PRICE, PROD_DESC from CATALOG Where PROD_ID = " & SearchId, cnnSQL)

        Dim rdr As SqlDataReader
        Try
            cnnSQL.Open()
            rdr = cmd.ExecuteReader()
            If rdr.Read Then
                lblName.Text = rdr(0) 'Read Name
                lblPrice.Text = rdr(1) 'Read Price
                'Change price format to 2 decimal places
                lblPrice.Text = "$" & Mid(lblPrice.Text, 1, Len(lblPrice.Text) - 2)
                txtDescription.Text = rdr(2) 'Read Description
            End If
            rdr.Close()
            cnnSQL.Close()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim CaptureId() As String

        'ValueID is a collection of parameters passed from the catalog page
        'by the Java code and the click event that we wrote for thumbnails
        'In our case there is just one parameter: ID
        Dim ValueID As System.Collections.Specialized.NameValueCollection
        ValueID = Request.QueryString
        'Retrieve parameter value from the collection and assign it to CaptureID
        CaptureId = ValueID.GetValues("Id")
        'Call procedure that displays product information
        Call ProductDisplayValues(CaptureId(0).ToString)     "Here is where the NullException Occurs"
    End Sub    

    Private Sub btnReturnCatalog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturnCatalog.Click
        Response.Redirect("catalog.aspx")
    End Sub

End Class
royhowie
  • 10,605
  • 14
  • 45
  • 66
Nick
  • 1
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Nov 17 '14 at 06:14

2 Answers2

0

If CaptureId(0) is null, calling ToString on it will cause the exception. Check if the value is null before the call.

BTW, elements in CaptureId are anyways String so you can omit method call all together.

danish
  • 5,324
  • 2
  • 23
  • 28
-2

You can check the Length of CaptureId which must be greater than 0 before using CaptureId(0).

  • Could you please explain your solution so the OP has a better understanding of your solution. If not that's fine as well, but better off as a comment. – zaggler Nov 17 '14 at 15:21