简体   繁体   中英

How to Display records of table in MS Access using List View vb.net

Help me guys.. I want to display the records of my table in MS access using the List view in Visual Studio 2010(vb.net)..

My program steps:

Input the Tablename in textbox

Then click button to Search in MS Access

If found the tablename, display the records in Listview

(sorry bad english)

How to Display the records on table in MS Access using List View vb.net?

here is my code:::

Imports System.Data.OleDb
Public Class Search
Dim shcon As New OleDbConnection
Dim shqry As String = Nothing
Dim shcmd As New OleDbCommand
Dim shdr As OleDbDataReader
Dim shdel As String = Nothing
Dim shstr As String = Nothing
'Co-connect to Database
#Region "Connection"
Sub ConnToDB()
    Try
        With shcon
            If .State = ConnectionState.Open Then .Close()
            .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\masterlist.accdb"
            .Open()
        End With
    Catch ex As Exception
        MessageBox.Show("Unable to connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Application.Exit()
    End Try
End Sub
#End Region
' Add Columns sa Listview
#Region "LVSettings"
    Sub LVsettings()
    With SLV.Columns
        .Add("Date", 50, HorizontalAlignment.Left)
        .Add("[AM]Time-in", 100, HorizontalAlignment.Left)
        .Add("[AM]Time-out", 100, HorizontalAlignment.Left)
        .Add("[PM]Time-in", 100, HorizontalAlignment.Left)
        .Add("[PM]Time-out", 100, HorizontalAlignment.Left)
    End With
End Sub
#End Region
'display the data from database
#Region "FillListView"

Sub FillListview()
    SLV.Items.Clear()
    shqry = "SELECT * from " & txtSID.text & " ORDER BY id ASC"
    shcmd = New OleDbCommand(shqry, shcon)
    shdr = shcmd.ExecuteReader
    While shdr.Read
        With SLV
            .Items.Add(shdr("DATE"))
            With .Items(.Items.Count - 1)
                .SubItems.Add(shdr("AM-TIME-IN"))
                .SubItems.Add(shdr("AM-TIME-OUT"))
                .SubItems.Add(shdr("PM-TIME-IN"))
                .SubItems.Add(shdr("PM-TIME-OUT"))

            End With
        End With
    End While
End Sub
#End Region
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
    Call FillListview()

End Sub

Private Sub LV1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SLV.SelectedIndexChanged

End Sub

Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call LVsettings()
    Call ConnToDB()
End Sub
End Class

Anyway in time of waiting. I manage to find the way.. here's the UPDATE:

before:

 While shdr.Read
        With SLV
            .Items.Add(shdr("DATE"))
             With .Items(.Items.Count - 1)
            .SubItems.Add(shdr("AM-TIME-IN"))
            .SubItems.Add(shdr("AM-TIME-OUT"))
            .SubItems.Add(shdr("PM-TIME-IN"))
            .SubItems.Add(shdr("PM-TIME-OUT"))
        End With
    End While

After:

 While shdr.Read
        With SLV
            .Items.Add(shdr("DATE"))
            With .Items(.Items.Count - 1)
                If Not IsDBNull(shdr("AM-TIME-IN")) Then .SubItems.Add(shdr("AM-TIME-IN"))
                If Not IsDBNull(shdr("AM-TIME-OUT")) Then .SubItems.Add(shdr("AM-TIME-OUT"))
                If Not IsDBNull(shdr("PM-TIME-IN")) Then .SubItems.Add(shdr("PM-TIME-IN"))
                If Not IsDBNull(shdr("PM-TIME-OUT")) Then .SubItems.Add(shdr("PM-TIME-OUT"))

            End With
        End With
    End While

Thank you for the time.

Here is my full code:

Imports System.Data.OleDb
Public Class Search
Dim shcon As New OleDbConnection
Dim shqry As String = Nothing
Dim shcmd As New OleDbCommand
Dim shdr As OleDbDataReader
Dim shdel As String = Nothing
Dim shstr As String = Nothing
'Co-connect to Database
#Region "Connection"
Sub ConnToDB()
    Try
        With shcon
            If .State = ConnectionState.Open Then .Close()
            .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database\masterlist.accdb"
            .Open()
        End With
    Catch ex As Exception
        MessageBox.Show("Unable to connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Application.Exit()
    End Try
End Sub
#End Region
' Add Columns sa Listview
#Region "LVSettings"
Sub LVsettings()
    With SLV.Columns
        .Add("Date", 50, HorizontalAlignment.Left)
        .Add("[AM]Time-in", 100, HorizontalAlignment.Left)
        .Add("[AM]Time-out", 100, HorizontalAlignment.Left)
        .Add("[PM]Time-in", 100, HorizontalAlignment.Left)
        .Add("[PM]Time-out", 100, HorizontalAlignment.Left)
    End With
End Sub
#End Region
'display the data from database
#Region "FillListView"

Sub FillListview()
    SLV.Items.Clear()
    shqry = "SELECT * from " & txtSID.text & " ORDER BY id ASC"
    shcmd = New OleDbCommand(shqry, shcon)
    shdr = shcmd.ExecuteReader
    While shdr.Read
        With SLV
            .Items.Add(shdr("DATE"))
            With .Items(.Items.Count - 1)
                If Not IsDBNull(shdr("AM-TIME-IN")) Then .SubItems.Add(shdr("AM-TIME-IN"))
                If Not IsDBNull(shdr("AM-TIME-OUT")) Then .SubItems.Add(shdr("AM-TIME-OUT"))
                If Not IsDBNull(shdr("PM-TIME-IN")) Then .SubItems.Add(shdr("PM-TIME-IN"))
                If Not IsDBNull(shdr("PM-TIME-OUT")) Then .SubItems.Add(shdr("PM-TIME-OUT"))

            End With
        End With
    End While
End Sub
#End Region
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
    Call FillListview()

End Sub

Private Sub LV1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SLV.SelectedIndexChanged

End Sub

Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call LVsettings()
    Call ConnToDB()
End Sub
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM