Smarton Smarton - 11 days ago 5
Vb.net Question

Reload a Datagrid View returns Nothing

I have the a Function that loads data into from a Microsoft Access DB into a DGV as shown in code below:

Public Sub LoaddgvCombined()
Try
'//Clear DataGrid
ds.Clear()
dgvSales.DataSource = Nothing
'dgvSales.Refresh()
dgvSales.DataSource = ds

'//Establish Connection to DB
con.ConnectionString = dbProvider & dbSource
tables = ds.Tables
con.Open()

sql = "SELECT * FROM [tblINV_SalesRecord] WHERE CDate(adddate & ' ' &addtime) < CDate('" & selectfrm & "')) ORDER BY temID ASC"

da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "tblINV_SalesRecord")
con.Close()

Dim view As New DataView(tables(0))
source1.DataSource = view
dgvSales.DataSource = view
dgvSales.AllowUserToAddRows = False

If dgvSales.RowCount < 1 Then
MessageBox.Show("No Sales Recorded Found From " & selectfrm)
End If

Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
MessageBox.Show("An Error Occured While Loading Sales Record ")
End Try
End Sub


It works fine the first time, but when I call the function the second time it empties the DGV but this time it does not load any Data.

Please can anyone point out what I am doing wrong.

Answer

The thing I noticed here is that you are passing to tables BEFORE you are getting the result of your query.

Try passing the values for tables after you fill ds.

Like this:

da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "tblINV_SalesRecord")
con.Close()
tables = ds.Tables '<=== transfer it here

It's showing empty because at the point you are passing to tables, your ds variable is not yet filled from your query.

Comments