Solved

Setting Combo Box Defaults in vb Express

Posted on 2014-02-03
3
227 Views
Last Modified: 2014-02-06
I have a combobox linked to an SQL table, see attached code

'Read SQL data source in combobox
        cmd = New SqlCommand("SELECT * FROM tbl_ProductStandards", con)
        If con.State = ConnectionState.Closed Then con.Open()
        Dim sdr As SqlDataReader = cmd.ExecuteReader()
        While sdr.Read = True
            cboProductCode.Items.Add(sdr.Item("ProductCode"))
        End While
        sdr.Close()

But i would like to make an automatic selection in the combo box on form open if the combo box is blank

I have tried a variety of things including .selecteditem and although the code does not error, nothing vible ever appears in the combobox.

All suggestions very welcome.
0
Comment
Question by:SweetingA
3 Comments
 
LVL 17

Accepted Solution

by:
Kent Dyer earned 500 total points
ID: 39831765
What you are looking for is the selected attribute..

<option value="audi" selected>Audi</option>

Open in new window

You will want to make this the default position if you will.
0
 
LVL 40
ID: 39831790
cmd = New SqlCommand("SELECT * FROM tbl_ProductStandards", con)
        If con.State = ConnectionState.Closed Then con.Open()
        Dim sdr As SqlDataReader = cmd.ExecuteReader()
If srd.HasRows then
       While sdr.Read = True
            cboProductCode.Items.Add(sdr.Item("ProductCode"))
        End While
Else
     cboProductCode.Items.Add("Sorry, no product to show")
End If
       sdr.Close()
0
 

Author Comment

by:SweetingA
ID: 39834132
Hello KentDyer,

I have tried various forms of using selected property but none work (no error just no visble selection in combo box field)

Could you please list the exact code
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now