Generating combo box items from a SQL database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TS

    Generating combo box items from a SQL database

    Hi all,
    From my windows form, I opened a connection to a SQL database. Now I need to
    generate a combo box from a SELECT statement pointing to the last name column
    in the SQL tables. I am stuck with the code I should use to generate this
    combo box. I used to use ADO to open a connection to SQL in VB6 then use Do
    until EOF - Loop to loop through the recodset and the cbo.additems to place
    the result of the recordset in the combo box. With .Net I am lost.The code I
    used to open a connection to the SQL database in the .net form is the
    following:-

    Private Sub ConnectSQL_Clic k(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles ConnectSQL.Clic k
    Dim SQLEV As SqlClient.SqlCo nnection = New SqlClient.SqlCo nnection
    SQLEV.Connectio nString = "Persist Security Info=true;Integ rated
    Security=false; User
    ID=sa;Password= demento;databas e=SSC;server=US EGNYDC3\CLIENT_ DB"
    SQLEV.Open()
    Dim SQLLine As String
    SQLLine = "SELECT last_name FROM qmf_cint"
    Dim objcmd As New SqlClient.SqlCo mmand(SQLLine, SQLEV)

    What should I do next?

    --
    TS
  • Ken Tucker [MVP]

    #2
    Re: Generating combo box items from a SQL database

    Hi,

    Here are 2 methods. I prefer to bind the combobox to a dataset.

    Dim conn As SqlConnection

    Dim strConn As String

    Dim drCustomer As SqlDataReader

    Dim daCustomer As SqlDataAdapter

    Dim cmd As SqlCommand

    Dim ds As New DataSet

    strConn = "Server = " & Environment.Mac hineName & ";"

    strConn &= "Database = NorthWind;"

    strConn &= "Integrated Security = SSPI;"

    conn = New SqlConnection(s trConn)

    cmd = New SqlCommand("Sel ect * from Customers", conn)

    conn.Open()

    drCustomer = cmd.ExecuteRead er

    ComboBox1.Items .Clear()

    Do While drCustomer.Read

    ComboBox1.Items .Add(drCustomer .Item("Customer ID").ToString )

    Loop

    conn.Close()

    daCustomer = New SqlDataAdapter( "Select * from Customers", conn)

    daCustomer.Fill (ds, "Customers" )

    ComboBox2.DataS ource = ds.Tables("Cust omers")

    ComboBox2.Displ ayMember = "CustomerID "



    Ken

    ----------------------------

    "TS" <TS@discussions .microsoft.com> wrote in message
    news:4D056921-C8FF-4D60-BAB8-AA5D92ECC307@mi crosoft.com...
    Hi all,
    From my windows form, I opened a connection to a SQL database. Now I need to
    generate a combo box from a SELECT statement pointing to the last name
    column
    in the SQL tables. I am stuck with the code I should use to generate this
    combo box. I used to use ADO to open a connection to SQL in VB6 then use Do
    until EOF - Loop to loop through the recodset and the cbo.additems to place
    the result of the recordset in the combo box. With .Net I am lost.The code I
    used to open a connection to the SQL database in the .net form is the
    following:-

    Private Sub ConnectSQL_Clic k(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles ConnectSQL.Clic k
    Dim SQLEV As SqlClient.SqlCo nnection = New SqlClient.SqlCo nnection
    SQLEV.Connectio nString = "Persist Security Info=true;Integ rated
    Security=false; User
    ID=sa;Password= demento;databas e=SSC;server=US EGNYDC3\CLIENT_ DB"
    SQLEV.Open()
    Dim SQLLine As String
    SQLLine = "SELECT last_name FROM qmf_cint"
    Dim objcmd As New SqlClient.SqlCo mmand(SQLLine, SQLEV)

    What should I do next?

    --
    TS


    Comment

    • TS

      #3
      Re: Generating combo box items from a SQL database

      Thanks for your reply. I tried the code you sent me, it didn't return an
      error message when I ran the form and clicked on the command button but it
      didn't generate the contents of the combo box either. Below is the code.
      Anything there prohibts the generation of the contents in the combo box?
      Another thing; how can I include the first name as well in the combo box
      beside the last name

      Private Sub ConnectSQL_Clic k(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles ConnectSQL.Clic k
      Dim SQLEV As SqlClient.SqlCo nnection = New SqlClient.SqlCo nnection
      Dim cmd As SqlClient.SqlCo mmand
      Dim ClientName As SqlClient.SqlDa taReader

      SQLEV.Connectio nString = "Persist Security Info=true;Integ rated
      Security=false; User
      ID=sa;Password= XXXX;database=d atabasename;ser ver=servername"
      cmd = New SqlClient.SqlCo mmand("SELECT * FROM qmf_cint", SQLEV)
      SQLEV.Open()
      ClientName = cmd.ExecuteRead er
      cbo_ChildName.I tems.Clear()
      Do While ClientName.Read
      cbo_ChildName.I tems.Add(Client Name.Item("Last _Name").ToStrin g)
      Loop
      SQLEV.Close()

      "Ken Tucker [MVP]" wrote:
      [color=blue]
      > Hi,
      >
      > Here are 2 methods. I prefer to bind the combobox to a dataset.
      >
      > Dim conn As SqlConnection
      >
      > Dim strConn As String
      >
      > Dim drCustomer As SqlDataReader
      >
      > Dim daCustomer As SqlDataAdapter
      >
      > Dim cmd As SqlCommand
      >
      > Dim ds As New DataSet
      >
      > strConn = "Server = " & Environment.Mac hineName & ";"
      >
      > strConn &= "Database = NorthWind;"
      >
      > strConn &= "Integrated Security = SSPI;"
      >
      > conn = New SqlConnection(s trConn)
      >
      > cmd = New SqlCommand("Sel ect * from Customers", conn)
      >
      > conn.Open()
      >
      > drCustomer = cmd.ExecuteRead er
      >
      > ComboBox1.Items .Clear()
      >
      > Do While drCustomer.Read
      >
      > ComboBox1.Items .Add(drCustomer .Item("Customer ID").ToString )
      >
      > Loop
      >
      > conn.Close()
      >
      > daCustomer = New SqlDataAdapter( "Select * from Customers", conn)
      >
      > daCustomer.Fill (ds, "Customers" )
      >
      > ComboBox2.DataS ource = ds.Tables("Cust omers")
      >
      > ComboBox2.Displ ayMember = "CustomerID "
      >
      >
      >
      > Ken
      >
      > ----------------------------
      >
      > "TS" <TS@discussions .microsoft.com> wrote in message
      > news:4D056921-C8FF-4D60-BAB8-AA5D92ECC307@mi crosoft.com...
      > Hi all,
      > From my windows form, I opened a connection to a SQL database. Now I need to
      > generate a combo box from a SELECT statement pointing to the last name
      > column
      > in the SQL tables. I am stuck with the code I should use to generate this
      > combo box. I used to use ADO to open a connection to SQL in VB6 then use Do
      > until EOF - Loop to loop through the recodset and the cbo.additems to place
      > the result of the recordset in the combo box. With .Net I am lost.The code I
      > used to open a connection to the SQL database in the .net form is the
      > following:-
      >
      > Private Sub ConnectSQL_Clic k(ByVal sender As Object, ByVal e As
      > System.EventArg s) Handles ConnectSQL.Clic k
      > Dim SQLEV As SqlClient.SqlCo nnection = New SqlClient.SqlCo nnection
      > SQLEV.Connectio nString = "Persist Security Info=true;Integ rated
      > Security=false; User
      > ID=sa;Password= demento;databas e=SSC;server=US EGNYDC3\CLIENT_ DB"
      > SQLEV.Open()
      > Dim SQLLine As String
      > SQLLine = "SELECT last_name FROM qmf_cint"
      > Dim objcmd As New SqlClient.SqlCo mmand(SQLLine, SQLEV)
      >
      > What should I do next?
      >
      > --
      > TS
      >
      >
      >[/color]

      Comment

      Working...