ASP.NET w/ VB.NET - Winforms 到 Web - NullReferenceException 字符串拆分/字符串生成器错误
背景:我有一个 winForm 应用程序,它根据提供的信息在数据库中注册用户,自动生成随机密码和用户名,并通过电子邮件向用户发送一个链接,以根据所选的营销公司进行申请。
问题:
- 当用户单击“发送邀请”并在除调试中的捆绑列表框之外的所有字段中输入数据时,会引发 NullReferenceException 错误,突出显示该行: Dim MChort As String = Trim(splitMC(0))
如何解决此问题
以下是 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于第一个问题,您可以使用 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.
我重写了以下代码区域以消除错误:
I rewrote the following areas of the code to get rid of the error: