Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

reading a value from a database to a combobox

Posted on 2011-10-04
1
Medium Priority
?
339 Views
Last Modified: 2012-06-22
I want to read data from a datafile and display on the form. I have problems with the comboboxes. I want to select a combobox value and its displays the corresponding text.

I am using the code below,
ctl is a group box, i check if the control is in that group box,

drd = cmd.ExecuteReader
        If drd.HasRows Then
            Do While drd.Read
                For t As Int16 = 3 To 5
                    ctl = Me.Controls("TabControl1").Controls("tab" & t - 2.ToString & "nc").Controls("GroupBox" & t.ToString)
                    If ctl.Controls.ContainsKey("T" & drd("vcode") & "") Then
                        CType(ctl.Controls("T" & drd("vcode")), ComboBox).SelectedValue = 1
                        CType(ctl.Controls("C" & drd("vcode") & ""), TextBox).Text = drd("addnotes") & ""
                        CType(ctl.Controls("C" & drd("vcode") & ""), TextBox).ForeColor = Color.Black
                    End If
                Next
            Loop
        End If


This is how i populated the combox,


arryn = New ArrayList
        arryn.Add(New cValue(" ", 0))
        arryn.Add(New cValue("Yes", 1))
        arryn.Add(New cValue("No", 2))

         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).DataSource = arryn
         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).DisplayMember = "Display"
         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).ValueMember = "Value"
0
Comment
Question by:Anthony Matovu
1 Comment
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36915606
I think you problem is that you are using an array as datasource so your DisplayMember and ValueMember are not really working. Try replacing

arryn = New ArrayList
        arryn.Add(New cValue(" ", 0))
        arryn.Add(New cValue("Yes", 1))
        arryn.Add(New cValue("No", 2))

         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).DataSource = arryn
         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).DisplayMember = "Display"
         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).ValueMember = "Value"



with this


Dim dTable As New DataTable
dTable.Columns.Add("Display", GetType(String))
dTable.Columns.Add("Value", GetType(Integer))
Dim dRow as DataRow = dTable.NewRow()
dRow(0) = ""
dRow(1) = 0
dTable.Rows.Add(dRow)
Dim dRow2 as DataRow = dTable.NewRow()
dRow2(0) = "Yes"
dRow2(1) = 1
dTable.Rows.Add(dRow2)
Dim dRow3 as DataRow = dTable.NewRow()
dRow3(0) = "No"
dRow3(1) = 2
dTable.Rows.Add(dRow3)

         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).DataSource = dTable
         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).DisplayMember = "Display"
         CType(ctl.Controls("TN" & Format(i, "00").ToString), ComboBox).ValueMember = "Value"
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question