Full width home advertisement

Post Page Advertisement [Top]

Bind ComboBox in vb




Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connetionString As String = Nothing
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter()
        Dim ds As New DataSet()
        Dim i As Integer = 0
        Dim sql As String = Nothing
        connetionString = "Data Source=ServerName;Initial Catalog=databasename;User         ID=userid;Password=yourpassword"
        sql = "select au_id,au_lname from authors"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(ds)
            adapter.Dispose()
            command.Dispose()
            connection.Close()
            ComboBox1.DataSource = ds.Tables(0)
            ComboBox1.ValueMember = "au_id"
            ComboBox1.DisplayMember = "au_lname"
        Catch ex As Exception
            MessageBox.Show("Can not open connection ! ")
        End Try
    End Sub
--------------------------------------------------------------------------------------------------------------------------
References..
http://vb.net-informations.com

Bind ComboBox in C#

SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=mynewdata;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);           
       
           
            comboBox1.ValueMember = "id";
       
            comboBox1.DisplayMember = "movie";
            comboBox1.DataSource = dt;
           

            con.Close(); 
--------------------------------------------------------------------------------------------------------------------------
References..
http://www.c-sharpcorner.com




No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib