Search code examples
sqlvb.netlinqdapper

How to retrieve data from Dapper ExecuteReader() into a textbox with MS-ACCESS database in VB.NET


I want to retrieve data in Form2 in TextBox1 which is Invono and TextBox2 which is Created. Actually, I have created a function method, which is GetItemTransfersMaster but I don't know how to use to display in the textbox. Please guide.

Thanks

Code in Form1

Public Class Form1
    Dim itrservice As New ItemtransfersService()
    Private Invno As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = itrservice.GetLoadItemTransfersMaster()
        DataGridView1.ReadOnly = True
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = 0 Then
            If DataGridView1.SelectedRows.Count > 0 Then ' make sure user select at least 1 row
                Dim Invno As String = DataGridView1.SelectedRows(0).Cells(0).Value & String.Empty
                Using frm = New Form2(Invno)
                    If frm.ShowDialog() = DialogResult.OK Then

                    End If
                End Using
            End If
        End If
    End Sub
End Class
Public Class ItemtransfersService
    Public Function GetOledbConnectionString() As String
        Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dapperdemo.accdb;Persist Security Info=False;"
    End Function
    Private ReadOnly _conn As OleDbConnection
    Private _connectionString As String = GetOledbConnectionString()
    Public Sub New()
        _conn = New OleDbConnection(_connectionString)
    End Sub
    Public Function GetLoadItemTransfersMaster() As IEnumerable(Of ItemTransfers)
        Dim sql = "SELECT * FROM ItemTransfers"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfers)(sql).ToList()
        End Using
    End Function
    Public Function GetItemTransfersDetail(ByVal Invno As String) As IEnumerable(Of ItemTransfersDetail)
        Dim sql = $"SELECT * FROM ItemTransfersDetail WHERE Invno = '{Invno}'"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfersDetail)(sql).ToList()
        End Using
    End Function
    Public Function GetItemTransfersMaster(ByVal Invno As String) As IEnumerable(Of ItemTransfers)
        Dim sql = $"SELECT * FROM ItemTransfers WHERE Invno = '{Invno}'"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of ItemTransfers)(sql).ToList()
        End Using
    End Function
End Class
Public Class ItemTransfers
    Public Property Invno() As String
    Public Property HeaderInvno() As Integer
    Public Property CreatedBy() As String
    Public Property Created() As DateTime
    Public Property ModifiedBy() As String
    Public Property Modified() As DateTime
    Public Property ItemTransfersDetail() As New List(Of ItemTransfersDetail)()
End Class
Public Class ItemTransfersDetail
    Public Property Id() As Integer
    Public Property No() As Integer
    Public Property Invno() As String
    Public Property CodeProduct() As String
    Public Property Barcode() As String
    Public Property Colorcode() As String
    Public Property Size() As String
    Public Property Qty() As Integer
End Class

Code in Form2

Public Class Form2
    Inherits Form
    Dim itrservice As New ItemtransfersService()
    Sub New()
        InitializeComponent()
    End Sub
    Public Sub New(Invno As String)
        Me.New
        DataGridView1.DataSource = itrservice.GetItemTransfersDetail(Invno)
        DataGridView1.Columns(0).Visible = False
    End Sub
End Class

view in form1

view in form1

view in form2

view in form2


Solution

  • I thank you very much for the guidance from @dr.null

    Code In form1

    Public Class Form1
        Dim itrservice As New ItemtransfersService()
        Private Invno As String
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.DataSource = itrservice.GetLoadItemTransfersMaster()
            DataGridView1.ReadOnly = True
        End Sub
    
        Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If e.ColumnIndex = 0 Then
                If DataGridView1.SelectedRows.Count > 0 Then ' make sure user select at least 1 row
                    Dim Invno As String = DataGridView1.SelectedRows(0).Cells(0).Value.tostring & String.Empty
     Dim item = DirectCast(DataGridView1.SelectedRows(0).DataBoundItem, ItemTransfers)
                    Using frm = New Form2(item)
                        If frm.ShowDialog() = DialogResult.OK Then
    
                        End If
                    End Using
                End If
            End If
        End Sub
    End Class
    Public Class ItemtransfersService
        Public Function GetOledbConnectionString() As String
            Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dapperdemo.accdb;Persist Security Info=False;"
        End Function
        Private ReadOnly _conn As OleDbConnection
        Private _connectionString As String = GetOledbConnectionString()
        Public Sub New()
            _conn = New OleDbConnection(_connectionString)
        End Sub
        Public Function GetLoadItemTransfersMaster() As IEnumerable(Of ItemTransfers)
            Dim sql = "SELECT * FROM ItemTransfers"
            Using _conn = New OleDbConnection(GetOledbConnectionString())
                Return _conn.Query(Of ItemTransfers)(sql).ToList()
            End Using
        End Function
        Public Function GetItemTransfersDetail(ByVal Invno As String) As IEnumerable(Of ItemTransfersDetail)
            Dim sql = $"SELECT * FROM ItemTransfersDetail WHERE Invno = '{Invno}'"
            Using _conn = New OleDbConnection(GetOledbConnectionString())
                Return _conn.Query(Of ItemTransfersDetail)(sql).ToList()
            End Using
        End Function
        Public Function GetItemTransfersMaster(ByVal Invno As String) As IEnumerable(Of ItemTransfers)
            Dim sql = $"SELECT * FROM ItemTransfers WHERE Invno = '{Invno}'"
            Using _conn = New OleDbConnection(GetOledbConnectionString())
                Return _conn.Query(Of ItemTransfers)(sql).ToList()
            End Using
        End Function
    End Class
    Public Class ItemTransfers
        Public Property Invno() As String
        Public Property HeaderInvno() As Integer
        Public Property CreatedBy() As String
        Public Property Created() As DateTime
        Public Property ModifiedBy() As String
        Public Property Modified() As DateTime
        Public Property ItemTransfersDetail() As New List(Of ItemTransfersDetail)()
    End Class
    Public Class ItemTransfersDetail
        Public Property Id() As Integer
        Public Property No() As Integer
        Public Property Invno() As String
        Public Property CodeProduct() As String
        Public Property Barcode() As String
        Public Property Colorcode() As String
        Public Property Size() As String
        Public Property Qty() As Integer
    End Class
    
    

    Code In form2

    Public Class Form2
        Inherits Form
    Dim itrservice As New ItemtransfersService()
        Sub New()
            InitializeComponent()
        End Sub
     Public Sub New(item As ItemTransfers)
            Me.New
            DataGridView1.DataSource = itrservice.GetItemTransfersDetail(Invno)
            DataGridView1.Columns(0).Visible = False
            TextBox1.Text = item.Invno
            TextBox2.Text = item.Created
     End Sub
    End Class