Direct Cast giving null error for datagridview

169 Views Asked by At

I am currently working in VB.NET express for desktop, 2013. I am having a hard time binding SQL data to some datagridviews on a loop with an array. I am receiving an object null error and its because on the direct cast line its not pulling the datagridview. I have multiple datagridviews on a tab control tool, one datagridview per tab. Here is my code:

 try
 Dim array() As Integer = {"2", "3", "4", "7", "8", "10", "11", "12"}

        For Each value As Integer In array
            Dim RelativeDGV = DirectCast(Me.Controls("DataGridLine" & value), DataGridView)
            Using conn1 As New SqlConnection(connstring)
                conn1.Open()
                Using comm1 As New SqlCommand("SELECT LineNumber FROM tableA where LineNumber = @LineNumber", conn1)
                    comm1.Parameters.AddWithValue("@LineNumber", value)
                    Dim dt As New DataTable
                    Dim sql As New SqlDataAdapter(comm1)
                    sql.Fill(dt)
                   RelativeDGV.DataSource = dt
                End Using
                conn1.Close()
            End Using 
        Next

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

The error is on line

 Dim RelativeDGV = DirectCast(Me.Controls("DataGridLine" & value), DataGridView)

But the null error dosen't trigger until

  RelativeDGV.DataSource = dt
2

There are 2 best solutions below

3
On BEST ANSWER

Try to use list of DataGridView like this :

Try
    Dim array() As DataGridView = {DataGridLine2, DataGridLine3, DataGridLine4, DataGridLine7, DataGridLine8, DataGridLine10, DataGridLine11, DataGridLine12}
    For Each RelativeDGV As DataGridView In array
        Dim value As Integer = Regex.Replace(RelativeDGV.Name, "[^0-9]+", String.Empty)
        'or like this
        'Dim value As Integer = RelativeDGV.Name.Substring(12, RelativeDGV.Name.Length - 12)
        Using conn1 As New SqlConnection(connstring)
            conn1.Open()
            Using comm1 As New SqlCommand("SELECT LineNumber FROM tableA where LineNumber = @LineNumber", conn1)
                comm1.Parameters.AddWithValue("@LineNumber", value)
                Dim dt As New DataTable
                Dim sql As New SqlDataAdapter(comm1)
                sql.Fill(dt)
                RelativeDGV.DataSource = dt
            End Using
            conn1.Close()
        End Using
    Next

Catch ex As Exception
    MsgBox(ex.ToString)
End Try
0
On

If the various DGV controls are on other tabs, they wont be in Me.Controls. Rather than fish them out and cast them, you can iterate an array of them since you know the name. You also do not need to create a new connection for each nor duplicate datatables for each:

Dim dgvCtrls As DataGridView() = {DataGridLine2, DataGridLine3, DataGridLine4}

Using conn1 As New SqlConnection(connstring)
    conn1.Open()
    Using comm1 As New SqlCommand("SELECT LineNumber FROM...", conn1)
        '    ...
        dt.Load(comm1.ExecuteReader())
    End Using

    conn1.Close()
End Using

For Each dgv In dgvCtrls
    dgv.DataSource = dt
Next

You'd only need 8 identical DataTables if you dont want each grid to automatically reflect changes made in the others. For that, use a dataset on the same connection to create the tables:

Dim SQL = "..."
Dim dgvCtrls As DataGridView() = {dgv5, dgv2, dgv3,...}
Dim ds = New DataSet

Using dbcon As New SqlConnection(SQLConnStr)
    Using cmd As New SqlCommand(SQL, dbcon)
        dbcon.Open()
        For n As Int32 = 0 To dgvCtrls.Count - 1
            ds.Load(cmd.ExecuteReader, LoadOption.OverwriteChanges, dgvCtrls(n).Name)
        Next
    End Using
End Using

For Each dgv In dgvCtrls
    dgv.DataSource = ds.Tables(dgv.Name)
Next