ASP.NET w/ VB.NET - Winforms to Web - NullReferenceException Error on string split / string builder

402 Views Asked by At

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
2

There are 2 best solutions below

1
On BEST ANSWER

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.

0
On

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