-1

我在连接到我的 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

然后我又得到了几次,但我认为上面的例子就足够了。

由于上面的示例中没有“使用块变量”,因此唯一的其他选择是它与对象相关。我尝试了不同的方法来定义和重新定义与错误相关的对象变量。然而,结果是一样的。

作为对并置的回答,我原来的问题已经解决了,但是因为我打开了 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 不允许从“对象”到“字符串”的隐式转换。

有问题的代码是:

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

它成功了......所以我感谢你们所有人的时间和耐心。

4

4 回答 4

4

You should always (99.999999% of the time) write VB.NET code with Option Strict On, unless you are writing interop code or interfacing with an esoteric database provider.

Simply place the words "Option Strict On" at the top of your file.

This will allow you to catch errors like the one you are dealing with.

Without Option Strict On you are allowed to write code like you have written:

Form1.Label1.Text = ListBox1.SelectedItem

The issue with that code is that is implicity tries to convert an object (ListBox1.SelectedItem) to a string (Form1.Label1.Text).

Turn option strict on and the compiler will give you an error up front.

You will then be forced to rewrite your code as such:

If ListBox1.SelectItem IsNot Nothing then
    Form1.Label1.Text = ListBox1.SelectedItem
End If
于 2011-09-04T22:07:38.097 回答
1

暂时关注这一行:

Form1.Label1.Text = ListBox1.SelectedItem

如果您在此行收到 NullReferenceException,则必须满足以下条件之一:

  • Form1 为空
  • Form1.Label1 为空
  • ListBox1 为空

您可以尝试通过在上述行之前添加如下行来确定这一点:

Console.Writeline("Form1: " & (Form1 Is Nothing))
Console.Writeline("Form1.Label1: " & (Form1.Label1 Is Nothing))
Console.Writeline("ListBox1:" & (ListBox1 Is Nothing))

您应该看到输出 true 的行;这是第一个线索。但接下来的问题是,为什么它是空的?从你到目前为止所展示的情况来看,我不能说。

于 2011-09-04T20:34:31.950 回答
1

确保ListBox1.SelectedItem在这两种情况下都不是 Nothing。

于 2011-09-04T20:35:07.270 回答
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 意味着您必须显式转换数据类型。

于 2011-09-05T07:59:12.093 回答