ASP.NET w/ VB.NET - Winforms 到 Web - NullReferenceException 字符串拆分/字符串生成器错误

发布于 2024-10-31 18:43:12 字数 2930 浏览 1 评论 0原文

背景:我有一个 winForm 应用程序,它根据提供的信息在数据库中注册用户,自动生成随机密码和用户名,并通过电子邮件向用户发送一个链接,以根据所选的营销公司进行申请。

问题:

  • 当用户单击“发送邀请”并在除调试中的捆绑列表框之外的所有字段中输入数据时,会引发 NullReferenceException 错误,突出显示该行: Dim MChort As String = Trim(splitMC(0))

如何解决此问题

以下是 Web 应用程序的屏幕截图:

web 应用程序屏幕截图

以下是 default.asx.vb 中的 Send_Button_Click 方法的代码:

Private Sub Send_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send_Button.Click

    'TODO Add code to validate that all selections that are reaquired are met.
    'ccemail and the additional message are not required
    Dim firstname As String = txtFirstName.Text
    Dim lastname As String = txtLastName.Text
    Dim ccEmail As String = txtccEmail.Text

    Dim sb As New StringBuilder

    sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
    sb.AppendLine("<root>")
    sb.AppendLine("<MarketingCompany>")
    sb.AppendLine("<MarketingCompanyName>")

    ''Get Marketing Company Short Name
    Dim splitMC As String() = Split(marketingCo.SelectedItem, "|")
    Dim MCShort As String = Trim(splitMC(0))
    sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", MCShort))


    'sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", My.Settings.MarketingCompanyShortName))

    sb.AppendLine(String.Format("<ccEmail>{0}</ccEmail>", txtccEmail.Text))
    sb.AppendLine(String.Format("<emailMessage>{0}</emailMessage>", txtMessage.Text))
    sb.AppendLine(String.Format("<MarketerName>{0}</MarketerName>", txtMarketerName.Text))
    sb.AppendLine("<agent>")
    sb.AppendLine(String.Format("<FirstName>{0}</FirstName>", txtFirstName.Text))
    sb.AppendLine(String.Format("<LastName>{0}</LastName>", txtLastName.Text))
    sb.AppendLine(String.Format("<Email>{0}</Email>", txtEmail.Text))
    sb.AppendLine("<CRMGuid>123456</CRMGuid>")

    Dim spltBundles() As String

    For Each item In bundles.Items
        If Trim(item) <> "" Then
            spltBundles = Split(item, "|")
            sb.AppendLine("<ContractingOpportunity>")
            sb.AppendLine(String.Format("<Carrier>{0}</Carrier>", Trim(spltBundles(0))))
            sb.AppendLine(String.Format("<ContractingOpportunityName>{0}</ContractingOpportunityName>", Trim(spltBundles(1))))
            sb.AppendLine("</ContractingOpportunity>")
        End If
    Next

    sb.AppendLine("</agent>")
    sb.AppendLine("</MarketingCompanyName>")
    sb.AppendLine(" </MarketingCompany>")
    sb.AppendLine(" </root>")
    Dim xmlStr = sb.ToString


    Dim int1 As Boolean = proxy.AddContractOpportunity(xmlStr.ToString, "test", "test")

    MsgBox("aComp Invitation Sent! :)")


End Sub

Background: I have a winForm app that registers a user in the database based on the information provided, auto-generates a random password and username, and e-mails the user a link to take an application based on the marketing company selected.

Problem:

  • When the user clicks "Send Invitation" with data inputted iton all fields except the bundles listbox in debug, the NullReferenceException Error is thrown highlighting the line: Dim MCShort As String = Trim(splitMC(0))

How do I fix this issue

Here's a screenshot of the web app:

web app screenshot

Here's the code of the Send_Button_Click method off default.asx.vb:

Private Sub Send_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send_Button.Click

    'TODO Add code to validate that all selections that are reaquired are met.
    'ccemail and the additional message are not required
    Dim firstname As String = txtFirstName.Text
    Dim lastname As String = txtLastName.Text
    Dim ccEmail As String = txtccEmail.Text

    Dim sb As New StringBuilder

    sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
    sb.AppendLine("<root>")
    sb.AppendLine("<MarketingCompany>")
    sb.AppendLine("<MarketingCompanyName>")

    ''Get Marketing Company Short Name
    Dim splitMC As String() = Split(marketingCo.SelectedItem, "|")
    Dim MCShort As String = Trim(splitMC(0))
    sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", MCShort))


    'sb.AppendLine(String.Format("<MCNAme>{0}</MCNAme>", My.Settings.MarketingCompanyShortName))

    sb.AppendLine(String.Format("<ccEmail>{0}</ccEmail>", txtccEmail.Text))
    sb.AppendLine(String.Format("<emailMessage>{0}</emailMessage>", txtMessage.Text))
    sb.AppendLine(String.Format("<MarketerName>{0}</MarketerName>", txtMarketerName.Text))
    sb.AppendLine("<agent>")
    sb.AppendLine(String.Format("<FirstName>{0}</FirstName>", txtFirstName.Text))
    sb.AppendLine(String.Format("<LastName>{0}</LastName>", txtLastName.Text))
    sb.AppendLine(String.Format("<Email>{0}</Email>", txtEmail.Text))
    sb.AppendLine("<CRMGuid>123456</CRMGuid>")

    Dim spltBundles() As String

    For Each item In bundles.Items
        If Trim(item) <> "" Then
            spltBundles = Split(item, "|")
            sb.AppendLine("<ContractingOpportunity>")
            sb.AppendLine(String.Format("<Carrier>{0}</Carrier>", Trim(spltBundles(0))))
            sb.AppendLine(String.Format("<ContractingOpportunityName>{0}</ContractingOpportunityName>", Trim(spltBundles(1))))
            sb.AppendLine("</ContractingOpportunity>")
        End If
    Next

    sb.AppendLine("</agent>")
    sb.AppendLine("</MarketingCompanyName>")
    sb.AppendLine(" </MarketingCompany>")
    sb.AppendLine(" </root>")
    Dim xmlStr = sb.ToString


    Dim int1 As Boolean = proxy.AddContractOpportunity(xmlStr.ToString, "test", "test")

    MsgBox("aComp Invitation Sent! :)")


End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2024-11-07 18:43:12

对于第一个问题,您可以使用 lbCarriers 的 OnSelectedIndexChanged 事件。

关于提交时缺少数据的问题,您应该在提交的页面上使用某种验证。 ASP.Net 包含一组验证控件,使工作变得简单。在最简单的级别上,您可以将表单控件设置为必需,这将允许表单在触发代码隐藏事件之前提示用户填写所有必填字段。

For your first question, you can use the OnSelectedIndexChanged event for lbCarriers.

Regarding the missing data question when you submit, you should use some sort of validation on the submitted page. ASP.Net includes a set of validation controls that make the job easy. At the simplest level, you can set your form controls to be required and that will allow the form to prompt the user to fill out all the required fields before your code-behind events will fire.

铜锣湾横着走 2024-11-07 18:43:12

我重写了以下代码区域以消除错误:

    ''Get Marketing Company Short Name
    Dim splitMC As String() = marketingCo.SelectedItem.ToString().Split("|")
    Dim MCShort As String = Trim(splitMC(0))


Protected Sub lbCarriers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbCarriers.SelectedIndexChanged
    Dim splt() As String
    Dim ac1 As Array
    bundles.Items.Clear()
    Dim item As ListItem = lbCarriers.SelectedItem
    splt = item.ToString().Split("|")
    ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
    For Each Pitem In ac1
        bundles.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
    Next
End Sub

I rewrote the following areas of the code to get rid of the error:

    ''Get Marketing Company Short Name
    Dim splitMC As String() = marketingCo.SelectedItem.ToString().Split("|")
    Dim MCShort As String = Trim(splitMC(0))


Protected Sub lbCarriers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbCarriers.SelectedIndexChanged
    Dim splt() As String
    Dim ac1 As Array
    bundles.Items.Clear()
    Dim item As ListItem = lbCarriers.SelectedItem
    splt = item.ToString().Split("|")
    ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
    For Each Pitem In ac1
        bundles.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
    Next
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文