2/8/19

VB.NET DB Connectivity Coding Reference part - 4 | List Box, Combo Box View the Data


Previous post we have learned
VB.NET Database Connectivity Coding Reference part - 1 | Database Connection,
VB.NET DB Connectivity Coding Reference part - 2 | CRUD & Navigation Operation.
VB.NET DB Connectivity Coding Reference part - 3 | CRUD & Change Password
In this post, We're going to learn how to List Box, Combo Box View the Data .

This tutorial have two file.

1. FormComboBox
2. FromListViewData

FormComboBox (Design View):



FormComboBox (Code View):

Imports System.Data.SqlClient
Public Class FormComboBox

    Dim SqlConn As New SqlConnection("Server=Najathi\SQLEXPRESS;Database=Student;Integrated Security=True")

    Dim SqlCmd As SqlCommand
    Dim sqlDA As SqlDataAdapter
    Dim dt As DataTable

    Private Sub FormComboBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Dim cmd As String = "SELECT DISTINCT Address FROM Staff"
        Dim cmd As String = "SELECT * FROM Staff ORDER BY ID DESC"

        SqlCmd = New SqlCommand(cmd, SqlConn)
        sqlDA = New SqlDataAdapter(SqlCmd)
        dt = New DataTable
        sqlDA.Fill(dt)

        ComboBoxView.DataSource = dt
        ComboBoxView.DisplayMember = "ID"
        ComboBoxView.ValueMember = "Address"


    End Sub
End Class



FromListViewData (Design View):




FromListViewData (Code View):

Imports System.Data.SqlClient
Public Class FormListBoxViewData

    Dim sqlConn As New SqlConnection("Server=Najathi\SQLEXPRESS;Database=Student;Integrated Security=True")

    Dim sqlCmd As SqlCommand
    Dim sqlDA As SqlDataAdapter
    Dim dt As DataTable

    Private Sub FormListBoxViewData_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        sqlCmd = New SqlCommand("SELECT * FROM Staff", sqlConn)
        sqlDA = New SqlDataAdapter(sqlCmd)
        dt = New DataTable
        sqlDA.Fill(dt)

        ListBoxDisplay.DataSource = dt
        ListBoxDisplay.DisplayMember = "Address"
        ListBoxDisplay.ValueMember = "ID"

    End Sub
End Class

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *