Results 1 to 9 of 9

Thread: Combo Box Challenge Does Anyone know the answer

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    32

    Combo Box Challenge Does Anyone know the answer

    Has anyone got a good example of loading a data reader into a combo box. I don't want to to just load the data but also have the Id field in there as well. I can do this with a dataset, but the perfomance is very slow. I can't find any examples of doing this a datareader. Also in Vb6 you could also make performance even faster by outputing the recordset into a .dat file and loading it into the combo box from there. Has anyone got any good examples of this. I looked extensively on both these subjects and can find nothing. Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  2. #2

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    32
    Surely someone must know this. If I had posted a thread entitled VB.net is crap I going back to VB6 I would of had 20 replies By now..........

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    There are a few different ways of doing this, but mainly the same method as the dataset should work with the datareader. You can also make your own datarow class and inherit the other one to override the tostring method and add that. You could make a custom class with the info you want and add that to the combo. If you switch around the method you use for the dataset it should look something like this:

    VB Code:
    1. Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
    2.         Combo1.Items.Clear()
    3.  
    4.         Do While dr.Read()
    5.             Combo1.Items.Add(New PtDiagnosisItem(dr.Item("PtDxID"), dr.Item("ICD9"), dr.Item("Description")))
    6.         Loop
    7.         dr.Close()
    8.         dr = Nothing
    9.         cmd.Dispose()

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    32
    Sorry I did not make myself clear on this. I want the id to be hidden but the description to be shown, after creating a dataset I would use the following code to do this

    With CmbEquipment
    .DataSource = MydataSet
    .DisplayMember = "Equipment"
    .ValueMember = "EquipmentId"
    End With

    How do I get the same but with a datareader?

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Do you mean you want to bind a control to a datareader?

    You can't the datareader is a forward only, read only reccordset design. You can fill it from the datareader but you can't bind to it, besides databind sucks for reasons like this.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    32
    the combo box will be used to update a table through an update statement but the table holds the value not the description. So I want to update the value back to the database. The user though will need to see only the description as the Id is meaningless to him

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    So why is it that you want to use a datareader? You can't update with a datareader. In fact you can't even move backwards in a datareader. You should be able to use a dataset just fine.

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    32
    the issue as I mentioned is speed the dataset is slower than a datareader. The form as such is then painfully slow to run at runtime. I just want to be able to populate the combo box with a description and hide the ID.

    You could do this in VB6 with a forwardly only cursor then update the database with an update sql statement. Is it seriously the case that this cannot be done in VB.net. if it is, then VB.net is a big step backwards.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can populate with a datareader but you can't use the datasource property to do it, that is just for databinding.

    Actually I guess you canuse the datasource property for more than just standard databinding, you can even bind to classes. So with that then I'm not sure if you can bind directly to the datareader, but if not then you can fill a genericlist class and use it.

    VB Code:
    1. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'make data to fill list
    3.         Dim MyList As New ArrayList()
    4.         MyList.Add(New GenericList("Jack Hammer", 1))
    5.         MyList.Add(New GenericList("Sledge Hammer", 2))
    6.         MyList.Add(New GenericList("Screwdriver", 3))
    7.         MyList.Add(New GenericList("Wrench", 4))
    8.  
    9.         'bind list
    10.         ComboBox1.DataSource = MyList
    11.         ComboBox1.DisplayMember = "Display"
    12.         ComboBox1.ValueMember = "ID"
    13.     End Sub
    14.  
    15. End Class
    16.  
    17. Public Class GenericList
    18.  
    19.     Private _Display As String
    20.     Private _ID As Integer
    21.  
    22.     Public Property Display() As String
    23.         Get
    24.             Return _Display
    25.         End Get
    26.         Set(ByVal Value As String)
    27.             _Display = Value
    28.         End Set
    29.     End Property
    30.  
    31.     Public Property ID() As Integer
    32.         Get
    33.             Return _ID
    34.         End Get
    35.         Set(ByVal Value As Integer)
    36.             _ID = Value
    37.         End Set
    38.     End Property
    39.  
    40.     Public Sub New()
    41.         MyBase.new()
    42.     End Sub
    43.  
    44.     Public Sub New(ByVal display As String, ByVal id As Integer)
    45.         Me.Display = display
    46.         Me.ID = id
    47.     End Sub
    48.  
    49. End Class

    Or to fill the class from the datareader:
    VB Code:
    1. Dim dr As New Data.SqlClient.SqlDataReader(cmd)
    2.         Dim MyList As New ArrayList()
    3.         Do While dr.read
    4.             MyList.Add(New GenericList(dr.Item("Equipment"), dr.Item("EquipmentID"))
    5.         Loop
    6.  
    7.         'bind list
    8.         ComboBox1.DataSource = MyList
    9.         ComboBox1.DisplayMember = "Display"
    10.         ComboBox1.ValueMember = "ID"
    11.     End Sub

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