0

I am using ctype to convert from string to textbox. These textboxes exists on the form. After conversion, i get data and display in the textbox. The first time I open the form all goes well. After exiting and rerunning the form again , the ctype throws an error "NullReferenceException" . On debug , I find that one ctype is returning nothing. Why does this happen?

Code as below :

Private Sub CompanyId_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CompanyId.SelectedIndexChanged, FundType.SelectedIndexChanged, FundGroup.SelectedIndexChanged, Currency.SelectedIndexChanged, Frequency.SelectedIndexChanged, MngmtFees.SelectedIndexChanged
               If INLOAD = True Then Exit Sub
        Dim cmb As ComboBox
        cmb = DirectCast(sender, ComboBox)

        Dim TXTNAME As String
        TXTNAME = cmb.Name & "_Name"

        Dim NEWTEXT As TextBox = CType(Me.Controls(TXTNAME), TextBox)

        *****If NEWTEXT Is Nothing Then MsgBox("hOW TO???")*****


        If cmb.Name = "CompanyId" Then NEWTEXT.Text = dc.Tables("Company").Rows(cmb.FindStringExact(cmb.Text)).Item(1).ToString
        If InStr(cmb.Name, "Fees") > 0 Then NEWTEXT.Text = dc.Tables("Fees").Rows(cmb.FindStringExact(cmb.Text)).Item(1).ToString
        If NEWTEXT.Text = "" Then NEWTEXT.Text = dc.Tables(cmb.Name).Rows(cmb.FindStringExact(cmb.Text)).Item(1).ToString()
Phil Murray
  • 6,396
  • 9
  • 45
  • 95
Nelsons
  • 47
  • 1
  • 11

3 Answers3

3

The only thing I can see that throws that exception would be the Me.Controls(TXTNAME). In this case either the value of TXTNAME is not correctly set or the controls have not loaded so the Me.Controls would return nothing. This means you are casting nothing to TextBox which would give you the NullReferenceException

Change your cast to use a TryCast

Dim NEWTEXT As TextBox = CType(Me.Controls(TXTNAME), TextBox)

Should be

Dim NEWTEXT As TextBox = TryCast(Me.Controls(TXTNAME), TextBox)

You can then check NEWTEXT for nulls

If NEWTEXT isnot nothing then

else

endif
Phil Murray
  • 6,396
  • 9
  • 45
  • 95
  • Thanks Phil. The problem I have is that although I have the textbox on the form , it still throws null. I am wondering why? If such a scenario happens what should we do? – Nelsons Apr 28 '13 at 16:18
  • Is the exception thrown on the CType or DirectCast? – Phil Murray Apr 28 '13 at 16:20
  • Can you check you have the correct value in TXTNAME before attempting the cast? – Phil Murray Apr 28 '13 at 16:21
  • I did!! The txtname shows an existing textbox name . And as I continue to rerun the form again, the error keeps showing on different textboxes. – Nelsons Apr 28 '13 at 16:25
  • Just for arguments sake can you put a break point on the code and stop it on the second load then in the immediate windows do a [TextBox].Name? Where [TextBox] is the value of TXTNAME. – Phil Murray Apr 28 '13 at 16:30
  • Hi Phil, just noticed that the event is looping twice through the same field ( I dont know why this happens) . the value of newtext.name is correct in the first loop. In the second it returns nothing. – Nelsons Apr 28 '13 at 16:39
  • Sounds like a problem caused by the PageLoad event firing you SelectionChanged event multiple times as it loads the controls. I would just implement the TryCast and allow for it to happen. – Phil Murray Apr 28 '13 at 16:42
0

Try changes this part ..

Dim cmb As ComboBox
cmb = DirectCast(sender, ComboBox)

Dim TXTNAME As String
TXTNAME = cmb.Name & "_Name"

Dim NEWTEXT As TextBox = CType(Me.Controls(TXTNAME), TextBox)

with

Dim cmb As ComboBox
Dim TXTNAME As String

cmb = CType(sender, ComboBox)
TXTNAME = cmb.Name.ToString & "_Name"

Dim NEWTEXT As TextBox = CType(Me.Controls(TXTNAME), TextBox)

It's worked for me ..

matzone
  • 5,703
  • 3
  • 17
  • 20
0

Note: it's not clear if this is for winforms or for webforms (asp.net). I see indications for both in your code. This is written assuming webforms. Even if that is wrong, much of what is here is still accurate for winforms.

Private Sub CompanyId_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CompanyId.SelectedIndexChanged, FundType.SelectedIndexChanged, FundGroup.SelectedIndexChanged, Currency.SelectedIndexChanged, Frequency.SelectedIndexChanged, MngmtFees.SelectedIndexChanged
    If INLOAD Then Exit Sub

    Dim cmb As ComboBox = TryCast(sender, ComboBox)
    Dim TXTNAME As String= If(cmb.Name,"") & "_Name"

    Dim NEWTEXT As TextBox = TryCast(Me.FindControl(TXTNAME), TextBox)

    If NEWTEXT Is Nothing Then
        MsgArea.Visible = True
        MsgValue.Text = " ... "
    End If

    If cmb.Name = "CompanyId" Then NEWTEXT.Text = dc.Tables("Company").Rows(cmb.FindStringExact(cmb.Text)).Item(1).ToString
    If cmb.Name.Contains("Fees") Then NEWTEXT.Text = dc.Tables("Fees").Rows(cmb.FindStringExact(cmb.Text)).Item(1).ToString()
    If String.IsNullOrWhitespace(NEWTEXT.Text) Then NEWTEXT.Text = dc.Tables(cmb.Name).Rows(cmb.FindStringExact(cmb.Text)).Item(1).ToString()
End Sub

I made a good number of small changes to that, so take the time to find and understand all of them.

One of of those changes will need some extra explanation. You cannot show a message box from asp.net. If you use this code on a production web server, your users will never see the message box and you will quickly lock up your server by running it out of threads. The problem is that you're showing the message box on the desktop of the web server. You are not showing it in the web browser. Instead, I wrote the code as if you have a panel control that you will hide/show at the appropriate times, and a label control within the panel. Together, these would act as a message box.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794