Results 1 to 6 of 6

Thread: Combo Box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73

    Combo Box

    I am using VB.net with a SQL database. I have all the code to connect to the database. My combo Box is called cboItemNumber.
    The table that Im pulling it from is called AUTO, FieldName that I want displayed in the combo box is called ItemNumber. What type of database code do I use to get this baby running?

  2. #2
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    VB Code:
    1. cn = New SqlConnection("integrated security=true;initial catalog=northwind")
    2.       'connecting to northwind if northwind is the selected database
    3.  
    4.       Dim cmdtext As String = "select * from categories"
    5.       'selecting * from categories table
    6.  
    7.       Dim da As New SqlDataAdapter()
    8.       da.SelectCommand = New SqlCommand(cmdtext, cn)
    9.       Dim dt As New DataTable("Category Table")
    10.       da.Fill(dt)
    11.       With ComboBox1 'combobox used
    12.          .DataSource = dt
    13.          .DisplayMember = "CategoryName"
    14.          'field name you want to put to combo
    15.       End With
    like this? if not, sorry...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    what does the (dt) refer too.

  4. #4
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    it's a datatable and get filled by the dataadapter... it then become a datasource where the combo gets info...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73

    ayan check this out. (or anyone else)

    This is the code that I got. Justin is the database name, auto is the table that im looking in, and ItemNumber is what I want to put in the combo Box. Im using my local server group and my server name is JNASI. When I run this I get no errors but my combo box is not populated. Any advise?

    Thanks





    Imports System.data.sqlclient
    Imports System.Data

    Public Class Form1

    Inherits System.Windows.Forms.Form

    Dim cn As New SqlConnection
    Dim mycommand As New SqlCommand
    Dim myDS As DataSet = New DataSet
    Dim Mydatagrid As New DataGrid
    Dim da As New SqlDataAdapter
    Dim objConnection As SqlConnection = New SqlConnection("server=(JNASI);database=justin;user id=sa;password=")
    Dim cmdtext As String = "select itemnumber from auto"
    Dim dt As New DataTable("auto")

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboItemNumber.SelectedIndexChanged
    'open the database connection

    cn = New SqlConnection("integrated security=true;initial catalog=auto")

    da.SelectCommand = New SqlCommand(cmdtext, cn)

    da.Fill(dt)
    With cboItemNumber
    .DataSource = dt
    .DisplayMember = "ItemNumber"

    End With

    End Sub
    End Class

  6. #6
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    i guess it's something like this...
    VB Code:
    1. Dim cn As New SqlConnection()
    2.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.       cn.ConnectionString = "integrated security=true;initial catalog=justin"
    4.       cn.Open()
    5.  
    6.       Dim cmdtext As String = "select * from [auto]"
    7.       Dim da As New SqlDataAdapter()
    8.       da.SelectCommand = New SqlCommand(cmdtext, cn)
    9.  
    10.       Dim dt As New DataTable("Table Auto")
    11.       da.Fill(dt)
    12.       With ComboBox1
    13.          .DataSource = dt
    14.          .DisplayMember = "ItemNumber"
    15.       End With
    16.    End Sub
    not in the selectedindexchanged event of your combo... hope this helps...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width