Solved

Setting Combo Box Defaults in vb Express

Posted on 2014-02-03
3
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

636 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