3 Replies - 384 Views - Last Post: 29 September 2011 - 07:30 AM Rate Topic: -----

#1 bemis82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-June 11

Help With Object Reference not being set

Posted 28 September 2011 - 09:55 PM

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.

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


Is This A Good Question/Topic? 0
  • +

Replies To: Help With Object Reference not being set

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Help With Object Reference not being set

Posted 28 September 2011 - 10:16 PM

Youre not declaring a new SqlReader or whatever it is you are trying to use. Because of this, it is trying to refer to an object that does not exist, therefore bringing up an error. Try
dim myreadet as new sqlreader

Then change all of your references to the object myreader.
Hth
Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Help With Object Reference not being set

Posted 28 September 2011 - 10:22 PM

Sorry, that should've been Dim connection as new SQLConnection
Sorry!
Was This Post Helpful? 0
  • +
  • -

#4 bemis82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-June 11

Re: Help With Object Reference not being set

Posted 29 September 2011 - 07:30 AM

Thanks, the part that's causing the error is the textbox variable, what i'm trying to do is have the query loop and then in the query you can see the 'dirtybag' object. I want 'dirtybag' to be textboxdirtybag1 for the first time through the loop and then dirtybag2 for the second time through the loop and so on and so forth. I did declare dirty bag as new textbox but for some reason it still isn't working...

thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1