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