Emerald Emerald - 1 year ago 52
Vb.net Question

Display data from database into DataGridView

I have two tables in my Access Database where I used to store the information of

Item In
and
Item Out
. I am displaying those data from both table in a DataGridView using a
DataSet
. Item In displayed in DataGridView1 and Item Out displayed in DataGridView2.

Here is my Module Function

Function to display Item In

Public Sub load_item_in()
ds_i.Reset()
Dim i_sql As String
Dim conn_i As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = access-database-file-dir")
i_sql = "SELECT * FROM item_in"
da_i = New OleDb.OleDbDataAdapter(i_sql, conn_i)
da_i.Fill(ds_i, "InventoryIn")
Form6.DataGridView1.DataSource = ds_i.Tables("InventoryIn")
Form6.DataGridView1.ReadOnly = True
End Sub


Function to display Item Out

Public Sub load_item_out()
ds_i.Reset()
Dim i_sql As String
Dim conn_i As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = access-database-file-dir")
i_sql = "SELECT * FROM item_out"
da_i = New OleDb.OleDbDataAdapter(i_sql, conn_i)
da_i.Fill(ds_i, "InventoryOut")
Form6.DataGridView2.DataSource = ds_i.Tables("InventoryOut")
Form6.DataGridView2.ReadOnly = True
End Sub


Calling out the function when button clicked

myModule.load_item_in()
myModule.load_item_out()


As you can see there, I am displaying two different DataGridView in a same form.

The problem I faced here is, I have table displayed in the
DataGridView1
but no data displayed there. My
DataGridView2
is able to display all data. So I am wondering either the system get confused of which data to called out because unable to display data at
DataGridView1

Answer Source

Looks like you're using the same DataSet in both functions and resetting it before each call.

The ds_i.Reset() method in load_item_out() is clearing the data that the first method load_item_in() retrieved. Remove your DataSet "reset" and add a DataTable clear before filling the DataAdapter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download