Help with Object reference not set to instance of object error...

bemis82

Member
Joined
Jun 4, 2011
Messages
14
Programming Experience
Beginner
Hi all, have some code that should be looping through a mysql query and running the query for each text box that has text in it but i keep getting the stupid object reference not set to an instance of an object error when it runs... Below is the code i have. The Object reference error is referring to the DirtyBag as Textbox in the SQL Query...

VB.NET:
Public Sub CheckIfExistingBag()
        SQLConnection.ConnectionString = connectionstring
        Try
            SQLConnection.Close()
            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
            End If

            Dim x As Integer
            Dim Bag As Integer = 0
            If TextBoxDirtyBag1.Text <> "" And TextBoxDirtyBag2.Text = "" Then
                x = 1
            ElseIf TextBoxDirtyBag1.Text <> "" And TextBoxDirtyBag2.Text <> "" And TextBoxDirtyBag3.Text = "" Then
                x = 2
            ElseIf TextBoxDirtyBag1.Text <> "" And TextBoxDirtyBag2.Text <> "" And TextBoxDirtyBag3.Text <> "" And TextBoxDirtyBag4.Text = "" Then
                x = 3
            ElseIf TextBoxDirtyBag1.Text <> "" And TextBoxDirtyBag2.Text <> "" And TextBoxDirtyBag3.Text <> "" And TextBoxDirtyBag4.Text <> "" And TextBoxDirtyBag5.Text = "" Then
                x = 4
            ElseIf TextBoxDirtyBag1.Text <> "" And TextBoxDirtyBag2.Text <> "" And TextBoxDirtyBag3.Text <> "" And TextBoxDirtyBag4.Text <> "" And TextBoxDirtyBag5.Text <> "" Then
                x = 5
            End If

            Dim i As Integer = 1
            For Me.i = 1 To x
                               Dim Dirtybag As New TextBox
                Dirtybag = Me.Controls("TextboxDirtyBag" & i)
                Dim Now As DateTime = DateTime.Now
                Dim sqlquery = "SELECT Dirty_Bag_Skew From transaction_inventory where transaction_item_status = 'Pending' And Dirty_Bag_Skew = '" & Dirtybag.Text & "'"
                Dim mycommand As New MySqlCommand()
                Dim myadapter As New MySqlDataAdapter()
                Dim Table As New DataTable

                mycommand.Connection = SQLConnection
                mycommand.CommandText = sqlquery
                myadapter.SelectCommand = mycommand
                Dim mydata As MySqlDataReader
                myadapter.Fill(Table)
                Me.DataGridView10.DataSource = Table
                mydata = mycommand.ExecuteReader()
                mydata.Close()
                SQLConnection.Close()
                If DataGridView10.Rows.Count - 1 > 0 Then
                    My.Computer.Audio.Play("C:\Windows\Media\Windows User Account Control.wav")
                    MsgBox("Bag " & Dirtybag.Text & "Is Already Associated With A Pending Transaction")
                    Bag = Bag + 1
                End If
                i = i + 1
            Next
            If Bag = 0 Then
                Call InsertDirtyLaundry1()
            End If

        Catch ex As Exception
            SQLConnection.Close()
            TransparentForm.Show()
            NoSQLConnection.Show()
            MsgBox(ex.ToString)
        End Try
 
It's not a stupid error. It's the error that occurs when you try to access a member of an object that doesn't exist. Now, your code has lots of issues, some critical and some not, but let's address this one first. Are you absolutely sure that the exception is thrown on that line? Is the 'Dirtybag' variable Nothing on that line? What is the value of 'i' when the exception is thrown?
 
Back
Top