Nelsons
Nelsons

Reputation: 47

Ctype conversion is throwing an error vb.net

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()

Upvotes: 0

Views: 8014

Answers (3)

Joel Coehoorn
Joel Coehoorn

Reputation: 415630

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.

Upvotes: 0

matzone
matzone

Reputation: 5719

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 ..

Upvotes: 0

Phil Murray
Phil Murray

Reputation: 6554

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

Upvotes: 3

Related Questions