I am currently using vb.net windows forms with an SQL
back end. I have a windows form that has multiple Datagridviews and I need to run through all the line items and run an sql check on each row. My curent code is long and would require me to copy and paste the code over and over again and this form is already pushing 1.2k lines of code so I want to keep it short. Considering each DGV has a specific name I was wondering if I could generate a list of strings to loop through or somehow use a pane. Here is my code thus far:
Try
Dim Chickenz As String
Dim MyDogTank As String
'Set up loop
For cn As Integer = 1 To DGV1.RowCount
Dim variable1 As Date = DGV1.Rows(cn - 1).Cells(1).Value
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As New SqlCommand (sqlcommand, conn1)
comm1.Parameters.AddWithValue("@Parameter", variable1)
Dim dt As New DataTable
Dim sql As New SqlDataAdapter(comm1)
sql.Fill(dt)
For Each row As DataRow In dt.Rows
Chickenz = row.Item("col2")
MyDogTank = row.Item("col1")
'--------Sql to check
Using conn2 As New SqlConnection(connstring)
conn2.Open()
Using comm2 As New SqlCommand(sqlcommand, conn2)
comm2.Parameters.AddWithValue("@Job", Chickenz)
Dim Eggz As New DataTable
Dim Yolk As New SqlDataAdapter(comm2)
Yolk.Fill(Eggz)
If Eggz.Rows.Count >= 1 Then
MsgBox("warningbox")
End If
End Using
conn2.Close()
End Using
Next
End Using
conn1.Close()
End Using
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
In this example, DGV1 would change between multiple DGV names and depending on how I do the code variable 1 could also change. The question is how to change DGV to the different names I need it to become. Otherwise I am going to have to copy and paste this code 8 times.