datatable was nothing or null

Viewed 45

am trying to make a login system with permissions and i used SQL as a direct object with datagridview but i keep getting that the (dt) = nothing i took this video as a referencehttps ://www.youtube.com/watch?v=qjjEe00Pyr0&ab_channel=ProgrammingforEverybody database imagebut when i had an error i literally change my whole code the same as the video i tried to remove the dt.clear didn't work out and tried to fill the dt from sql still didn't work out am kinda stuck btw this is the first time posting in StackOverflow so if there is something i don't know about posting a q let me know code image

1 Answers
 Public Sub DisplayDataGrid(ByVal dgv As DataGridView, ByVal SQLSTR As String)
            dgv.DataSource = Nothing
            Try
                Dim dbBindSource As New BindingSource
                Dim strCon As String = 'Put your connection string here
                Dim strSQL As String = SQLSTR
    
                Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(strSQL, strCon)
                dataAdapter.SelectCommand.CommandTimeout = 1800
    
                Dim commandBuilder As SqlCommandBuilder = New SqlCommandBuilder(dataAdapter)
    
    
    
                'Populate a new data table and bind it to the BindingSource.
                Dim table As DataTable = New DataTable()
                table.Locale = System.Globalization.CultureInfo.InvariantCulture
                dataAdapter.Fill(table)
                dbBindSource.DataSource = table
    
    
                'Resize the DataGridView columns to fit the newly loaded content.
                dgv.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
                dgv.DataSource = dbBindSource
    
    
    
                'dgv.Columns(2).Width = 200
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "error")
            End Try
        End Sub
Related