0

I'm trying to get what seems like it should be a simple chunk of code to work, but I keep getting the ole Null Reference Exception...

Dim materialID As Integer = CInt(Request.QueryString("MaterialID"))
    Dim s As String = Nothing
    DBTable = MaterialStuff.GetMaterialHeader(materialID)

    Using DBTable

        s = CType(DBTable.Rows(0).Item("MaterialID"), String)

        Try

            If CType(DBTable.Rows(0).Item("MaterialID"), String) IsNot Nothing Then

                CType(MaterialIdControl, TextBox).Text = s
            Else

            End If

        Catch ex As NullReferenceException

            MsgBox(ex.ToString)
        End Try
    End Using

Here is where MaterialIdControl is coming from:

Dim MaterialIdControl As Control = FindControl("txtMaterial")

When stepping through it s is equal to 970 (970 is not a string I know but previous programmer messed up this program) Anyway, the right value is there but it's still saying that there is an exception, any idea where I can fix this at?

bbesase
  • 791
  • 4
  • 18
  • 31
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 13 '14 at 18:38
  • WHERE do you get the exception? Maybe it comes from DBTable.Rows, or from DBTable.Rows[0].Item... – Kilazur May 13 '14 at 18:54
  • I figured it out guys, I had the `im MaterialIdControl As Control = FindControl("txtMaterial")` in the wrong spot, and had the wrong syntax so it was messing up. Thanks anyways :) – bbesase May 13 '14 at 19:05

1 Answers1

0

There are several possibilities:

1) The querystring "MaterialID" doesnot exists in the page.

Request.QueryString("MaterialID")

This might result in null exception raised by MaterialStuff.GetMaterialHeader(materialID). Check the address bar of browser if the query string exists or not.

2) The control "txtMaterial" might not exists in the page.

FindControl("txtMaterial")

This might result in null exception raised by CType(MaterialIdControl, TextBox).Text = s

Please check you code, if you still didnt find the cause of exception, please paste the exception message generated by debugger here.

Amit Mittal
  • 1,129
  • 11
  • 30