Results 1 to 4 of 4

Thread: combo box question regarding data reading

  1. #1

    Thread Starter
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112

    combo box question regarding data reading

    halu,

    my friend and i got this problem a while ago. we don't know what wrong. it's about combo box with data reading. i don't know if my question meets our need here. sorry for that. hehe. cheers...

    it's about retreiving Categories Table in the NorthWind and adding the CategoryName in the combo box. when name selected it supposed to display the CategoryID on the combo box text property. i don't know what's wrong... what event should we override? i tried this one...
    VB Code:
    1. Dim cn As New SqlConnection("integrated security=true;initial catalog=northwind")
    2.     Dim cm As New SqlCommand()
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         cn.Open()
    6.         cm.Connection = cn
    7.         cm.CommandType = CommandType.StoredProcedure
    8.         With cm
    9.             .Parameters.Clear()
    10.             .CommandText = "GetCategoriesTable"
    11.             Dim dr As SqlDataReader = .ExecuteReader
    12.             While dr.Read
    13.                 ComboBox1.Items.Add(dr.GetValue(1))
    14.             End While
    15.             dr.Close()
    16.         End With
    17.     End Sub
    18.  
    19.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    20.         With cm
    21.             .Parameters.Clear()
    22.             .CommandText = "GetCategoriesRowOnName"
    23.             .Parameters.Add("@Name", SqlDbType.NVarChar, 15).Direction = ParameterDirection.Input
    24.             .Parameters("@Name").Value = ComboBox1.SelectedItem
    25.             Dim dr As SqlDataReader = .ExecuteReader
    26.             dr.Read()
    27.             ComboBox1.Text = dr.GetValue(0)
    28.             dr.Close()
    29.         End With
    30.     End Sub
    what did i miss? thanx in advance guys...

    --ayan

  2. #2
    Lively Member nokia8210's Avatar
    Join Date
    Dec 2002
    Location
    Coventry
    Posts
    92
    i would do somthing easier than that i would use a dataset and read all the data from the categories table into that like this:

    set up your sql dataadapter using command type as text

    dim dsTable as new dataset

    dsTable.Tables.Add("Table")

    sqldataadapter.commandtext ="Select * From Categories"

    sqldataadapter.fill(dsTable.Tables("Table"))

    With Combobox
    .datasource = dsTable.Tables("Table")
    .displaymember = "CategoryName"
    .Valuemember = "CategoryID"
    end with

    that should load them all into the combobox
    Last edited by nokia8210; Feb 18th, 2004 at 07:07 AM.

  3. #3
    Lively Member nokia8210's Avatar
    Join Date
    Dec 2002
    Location
    Coventry
    Posts
    92
    here is the code needed:
    Attached Files Attached Files

  4. #4

    Thread Starter
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    thanx nokia... i just got something today. i learn something... nice code you have. thanx man, i know what you mean... i would have a masking-like. having the name on combo box and the id as value... thanx so much...

    cheers,
    --ayan

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