我在连接到我的SQL数据库的VB.NET应用程序中收到错误。它连接正常,但由于某种原因我无法修复此错误。当我尝试修复它时,它会从我的脚本的一部分移动到我的脚本的另一部分(这两部分都在昨天工作)。错误详情如下:
不幸的是,我很难描述我是如何产生这个结果的,因为它发生在我的代码的多个部分中,这些部分的共同点就是它们与Listbox1的交互。
获得此错误的代码的第一部分是:
Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM [" & frmMain.ListBox1.SelectedItem.value & "]", con)
然后我得到了同样的错误:
Private Sub ListBox1_SelectedValueChanged( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedValueChanged
Try
Form1.Label1.Text = ListBox1.SelectedItem
Form1.Show()
Catch myerror As MySqlException
MessageBox.Show("Error Setting Up Project Page: " & myerror.Message)
End Try
End Sub
更具体地说:
Form1.Label1.Text = ListBox1.SelectedItem
然后我又多了几次,但我认为上面的例子就足够了。
由于上面的示例中没有“With Block Variables”,因此唯一的另一个选择是它与对象相关。我尝试过不同的方法来定义和重新定义与错误相关的对象变量。但是,结果是一样的。
为了回应Juxtaposition的回答,我原来的问题已经解决,但是因为我开启了Option Strict,所以出现了两个新的问题。
Error1:Option Strict On禁止后期绑定。
有问题的代码是:
Try
' Retrieving the projects list.
con.Open()
DataAdapter2.SelectCommand = sqlprojects
DataAdapter2.Fill(ds2, "projects")
ListBox1.Items.Clear()
For Each DataRow In ds2.Tables("projects").Rows
' Error occurs on the line below
ListBox1.Items.Add(DataRow("project_name"))
Next
con.Close()
Catch myerror As MySqlException
MessageBox.Show("Error Retrieving Projects List: " & myerror.Message)
End Try
错误2:Option Strict On禁止从“Object”到“String”的隐式转换。
有问题的代码是:
Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
Try
If ListBox1.SelectedItem IsNot Nothing Then
' Error occurs on the line below
Form1.Label1.Text = ListBox1.SelectedItem
End If
Form1.Show()
Catch myerror As MySqlException
MessageBox.Show("Error Setting Up Project Page: " & myerror.Message)
End Try
End Sub
它成功了...所以我感谢你们所有人的时间和耐心。
答案 0 :(得分:4)
你应该总是(99.999999%的时间)用Option Strict On
编写VB.NET代码,除非你正在编写互操作代码或与深奥的数据库提供者接口。
只需在文件顶部放置“Option Strict On”字样即可。
这将允许您捕获与您正在处理的错误一样的错误。
如果没有Option Strict On
,您可以像编写的那样编写代码:
Form1.Label1.Text = ListBox1.SelectedItem
该代码的问题在于暗示将对象(ListBox1.SelectedItem)转换为字符串(Form1.Label1.Text)。
启用选项严格打开,编译器会预先给出错误。
然后您将被迫重写您的代码:
If ListBox1.SelectItem IsNot Nothing then
Form1.Label1.Text = ListBox1.SelectedItem
End If
答案 1 :(得分:1)
暂时关注这一行:
Form1.Label1.Text = ListBox1.SelectedItem
如果您在此行上收到NullReferenceException,则必须满足以下条件之一:
您可以尝试通过在上一行之前添加类似这样的行来确定:
Console.Writeline("Form1: " & (Form1 Is Nothing))
Console.Writeline("Form1.Label1: " & (Form1.Label1 Is Nothing))
Console.Writeline("ListBox1:" & (ListBox1 Is Nothing))
你应该看到一行输出true;这是第一个线索。但接下来的问题是,为什么它是空的?从你到目前为止所展示的情况来看,我不能说。
答案 2 :(得分:1)
确保在这两种情况下ListBox1.SelectedItem
都不是。
答案 3 :(得分:0)
您可以修复原始错误,而无需使用Option Explicit On。您需要确保Listbox.SelectedItem在使用之前具有值。代码应写为:
If frmMain.ListBox1.SelectedItem IsNot Nothing Then
Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM [" & frmMain.ListBox1.SelectedItem.value & "]", con)
End If
和
Try
If ListBox1.SelectedItem IsNot Nothing Then
Form1.Label1.Text = ListBox1.SelectedItem
End If
Form1.Show()
Catch myerror As MySqlException
MessageBox.Show("Error Setting Up Project Page: " & myerror.Message)
End Try
更新#2
应通过将代码更改为:
If ListBox1.SelectedItem IsNot Nothing Then
Form1.Label1.Text = ListBox1.SelectedItem.ToString
End If
Option Explicit On表示您必须显式转换数据类型。