Ctype 转换抛出错误 vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16264878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:25:16  来源:igfitidea点击:

Ctype conversion is throwing an error vb.net

vb.net

提问by Nelsons

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?

我正在使用 ctype 从字符串转换为文本框。这些文本框存在于表单上。转换后,我获取数据并显示在文本框中。我第一次打开表单时一切顺利。退出并再次重新运行表单后,ctype 会抛出错误“NullReferenceException”。在调试时,我发现一个 ctype 没有返回任何内容。为什么会发生这种情况?

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

回答by Phil Murray

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

我唯一能看到抛出该异常的是Me.Controls(TXTNAME). 在这种情况下,要么 的值TXTNAME设置不正确,要么控件未加载,因此Me.Controls将不返回任何内容。这意味着您没有向 TextBox 投射任何内容,这会给您NullReferenceException

Change your cast to use a TryCast

更改演员表以使用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

然后您可以检查 NEWTEXT 是否为空值

If NEWTEXT isnot nothing then

else

endif

回答by matzone

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

它对我有用..

回答by Joel Coehoorn

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.

注意:不清楚这是针对 winforms 还是针对 webforms (asp.net)。我在您的代码中看到了两者的指示。这是在假设网络表单的情况下编写的。即使这是错误的,这里的大部分内容对于 winform 来说仍然是准确的。

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.

其中一项更改需要一些额外的解释。您不能显示来自 asp.net 的消息框。如果您在生产 Web 服务器上使用此代码,您的用户将永远不会看到消息框,并且您将通过运行线程来快速锁定您的服务器。问题是您在 Web 服务器的桌面上显示消息框。您没有在网络浏览器中显示它。相反,我编写了代码,就像您有一个将在适当时间隐藏/显示的面板控件以及面板中的标签控件一样。这些将一起用作消息框。