-1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
daniel11
  • 2,027
  • 10
  • 38
  • 46
  • It's a bad idea to substitute user input (and listbox items _are_ user input -- you can send an http request that uses _anything_ for a listbox input, even if you don't supply an item for it) into your query. I know query parameters aren't option here, but you can at least use information_schema to verify it first, or move this to a stored procedure that does that. – Joel Coehoorn Sep 04 '11 at 20:41
  • could you provide an example and a brief explanation for your idea? that sounds like it could work! – daniel11 Sep 04 '11 at 20:59
  • ill admit im not that good at all when it comes to debugging a program.... thats why im asking for help. yes i have many books and websites about the topic at my disposal however i learn from examples and seeing how code is applied to various situations. but were getting off topic so like i said id like to see an example of Joel Coehoorn's suggested solution! – daniel11 Sep 04 '11 at 23:54

4 Answers4

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
  • This is news to me! however not only did it solve my problem, it created two new ones... they should be simple to fix though. Ill post the details in an Update. – daniel11 Sep 04 '11 at 22:53
  • 1
    Instead of using the phrase Option Strict On at the top of every file, you can click on Project > Properties, switch to the Compile tab and set Option Strict On for the entire project – briddums Sep 05 '11 at 07:29
1

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
dnuttle
  • 3,810
  • 2
  • 19
  • 19
  • they all exist, that much is true, and when i paste your code above the specified line of code it says that the '+' operator cannot be used for strings. and i tried omitting the quotations and it said it cannot be used for form objects... just to be clear im using visual basic 2010 express on a windows 7 gateway laptop. – daniel11 Sep 04 '11 at 20:40
  • 1
    I've not tried this but I don't see how you can `Response.Write` in a WinForm. – Ash Burlaczenko Sep 04 '11 at 20:41
  • Sorry...it's been a while since I did any C# coding, and I don't have C# on this machine...I guess the concatenation operator in C# is &. Looks like Joel fixed the code for me. – dnuttle Sep 04 '11 at 20:45
  • now it says that "Response" is not declared... do i need to import something for this to work? – daniel11 Sep 04 '11 at 20:57
  • @daniel My bad, somewhere I thought I saw a reference to a page, and therefore that this was for asp.net rather than winforms – Joel Coehoorn Sep 04 '11 at 21:04
  • You missed the one that really is null: SelectedItem. The list box doesn't have a selection. – Hans Passant Sep 04 '11 at 21:07
  • well there was a list of options, i clicked one, it was highlighted upon clicking it, and it produced an error ... im pretty sure something was selected. read my question again i updated it with what i found. – daniel11 Sep 04 '11 at 21:12
1

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

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

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.

briddums
  • 1,826
  • 18
  • 31