无法修复的错误?

发布于 2024-12-03 10:06:52 字数 2449 浏览 1 评论 0原文

我在连接到 SQL 数据库的 VB.NET 应用程序中遇到错误。它连接正常,但由于某种原因我无法修复此错误。当我尝试修复它时,它从脚本的一个部分移动到脚本的另一部分(这两个部分昨天都在工作)。错误详细信息为:

Error

不幸的是,我很难描述我如何产生这个结果,因为它已经发生在多个我的代码的一部分,这些部分唯一的共同点是它们与 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

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

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

作为对 Juxtaposition 的回答,我原来的问题已经解决,但是由于我打开了 Option Strict,所以特别出现了两个新问题。

  • 第一个是:

错误1: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

它成功了......所以我感谢大家的时间和耐心。

I'm getting an error in my VB.NET application that connects to my SQL database. It connects fine, but for some reason I can't fix this error. When I try to fix it, it moves from one part of my script to another part of my script (both of which were working yesterday). The error details are:

Error

Unfortunately, it's difficult for me to describe how I produced this result, because it has happened in multiple parts of my code, and the only thing that these parts have in common is their interaction with Listbox1.

The first part of code to get this error was:

Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM [" & frmMain.ListBox1.SelectedItem.value & "]", con)

Then I got the same exact error for:

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

More specifically:

Form1.Label1.Text = ListBox1.SelectedItem

And then I got it a few more times, but I think the examples above will suffice.

Since there are no "With Block Variables" in the examples above then the only other option is that it's object related. I've tried different methods of defining and redefining the object variables related to the error. However, the results are the same.

In response to Juxtaposition's answer, my original problem has been solved however two new problems have come up specifically because I turned Option Strict on.

  • The first is:

Error1: Option Strict On disallows late binding.

The code in question is:

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
  • The second is:

Error 2: Option Strict On disallows implicit conversions from 'Object' to 'String'.

The code in question is:

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

It worked out... so I thank all of you for your time and patience.

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

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

发布评论

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

评论(4

败给现实 2024-12-10 10:06:52

您应该始终(99.999999% 的时间)使用 Option Strict On 编写 VB.NET 代码,除非您正在编写互操作代码或与深奥的数据库提供程序交互。

只需将“Option Strict On”字样放在文件顶部即可。

这将使您能够捕获像您正在处理的错误一样的错误。

如果没有Option Strict On,您可以像您所写的那样编写代码:

Form1.Label1.Text = ListBox1.SelectedItem

该代码的问题是隐式尝试将对象(ListBox1.SelectedItem)转换为字符串(Form1.Label1.Text) )。

打开选项 strict ,编译器会预先给你一个错误。

然后你将被迫重写你的代码,如下所示:

If ListBox1.SelectItem IsNot Nothing then
    Form1.Label1.Text = ListBox1.SelectedItem
End If

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
夜清冷一曲。 2024-12-10 10:06:52

暂时关注这一行:

Form1.Label1.Text = ListBox1.SelectedItem

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

  • Form1 为 null
  • Form1.Label1 为 null
  • ListBox1 为 null

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

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

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

Focus on this line for the moment:

Form1.Label1.Text = ListBox1.SelectedItem

If you're getting a NullReferenceException on this line, then one of the following has to be true:

  • Form1 is null
  • Form1.Label1 is null
  • ListBox1 is null

You can try to determine this by adding lines like these just before the above line:

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

You should see a line that outputs true; that's the first clue. But then the next question is, why is it null? From what you've shown so far, I can't say.

三五鸿雁 2024-12-10 10:06:52

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

Make sure ListBox1.SelectedItem is not Nothing in both of those circumstances.

美人迟暮 2024-12-10 10:06:52

无需使用 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 意味着您必须显式转换数据类型。

Your original errors can be fixed without having to use Option Explicit On. You need to ensure that the Listbox.SelectedItem has a value before using it. The code should be written as:

If frmMain.ListBox1.SelectedItem IsNot Nothing Then
    Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM [" & frmMain.ListBox1.SelectedItem.value & "]", con)
End If 

and

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

Update #2
Your second error should be fixed by changing the code to:

If ListBox1.SelectedItem IsNot Nothing Then
    Form1.Label1.Text = ListBox1.SelectedItem.ToString
End If

Option Explicit On means that you have to explicitly convert data types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文