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