[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

dynamically select item of a combo-box

Posted on 2011-10-08
7
Medium Priority
?
640 Views
Last Modified: 2012-05-12

I have this code. it is supposed to select a value of the combobox and display the item

  If drd(g).ToString.Trim.Length > 0 Then CType(frm.Controls(drd.GetName(g)), ComboBox).SelectedValue = CType(drd(g), Int32)

This is how i populate the combobox

        tstr = "SELECT * FROM [villageUnit] WHERE ((([country])= '" & CType(xtry.SelectedItem, strValue).Value & "'));"

        cmd = New OleDb.OleDbCommand
        cmd.CommandText = tstr
        cmd.Connection = db
        dr = cmd.ExecuteReader

        If Not arrfxbu.Count = 0 Then Exit Sub
        arrfxbu = New ArrayList
        arrfxbu.Add(New cValue(" ", -1))
        arrfxbu.Add(New cValue("ALL", 0))

        If dr.HasRows Then
            Do While dr.Read
                arrfxbu.Add(New cValue(dr("villageName").ToString, dr("vid")))
            Loop
        End If
        dr.Close()

        ufxbunit.DataSource = arrfxbu
        ufxbunit.DisplayMember = "Display"
        ufxbunit.ValueMember = "Value"

before reaching this line
  If drd(g).ToString.Trim.Length > 0 Then CType(frm.Controls(drd.GetName(g)), ComboBox).SelectedValue = CType(drd(g), Int32)

 CType(frm.Controls(drd.GetName(g)), ComboBox).SelectedValue = -1

but after it is equal to "nothing"
0
Comment
Question by:Anthony Matovu
  • 4
  • 3
7 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36935863
As I said in your previous question, you can not use DisplayMember and ValueMember when using array as the datasource. I showed you how to use a DataTable. Do you need an example of loading the datatable from DB and setting as datasource? Its easy

Dim dbadp As New OleDbDataAdapter("select * from ...", "connection string here")
Dim dTable As New DataTable
dbadp.Fill(dTable)
dbadp.Dispose

ufxbunit.DataSource = dTable
ufxbunit.DisplayMember = "column name"
ufxbunit.ValueMember = "column name"
0
 
LVL 1

Author Comment

by:Anthony Matovu
ID: 36935879
Thank you, what is the efficient code to read the selected value from the combobox populated by the above code
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36935887
Try using

ufxbunit.SelectedValue
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 1

Author Comment

by:Anthony Matovu
ID: 36935911
I sometimes get this error message

Overload resolution failed because no Public '=' can be called with these arguments:     'Public Shared Operator =(a As String, b As String) As Boolean':         Argument matching parameter 'a' cannot convert from 'DataRowView' to 'String'. especially when selection has not been made yet. I need to test if it is no selection is made  yet (but how)

Thanks
0
 
LVL 1

Author Comment

by:Anthony Matovu
ID: 36935918
In this example both the display and value are string
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36935920
What code produces this error? You can check for no selection by using

If ufxb.SelectedIndex > -1 Then
...
End If
0
 
LVL 1

Author Comment

by:Anthony Matovu
ID: 36935928
I have used selectedtext and it seems to work

Thanks

Anthony
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

829 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