SelectedText on DropDownList

I have the following statement to fill a DropDownList from sql server ...

myconnection = New SqlClient.SqlConnection("Server=myserver;Database=my_database;Trusted_Connection=yes")
        myda = New SqlClient.SqlDataAdapter("Select status from STATUS_VW order by status", myconnection)
        ds = New DataSet
        myda.Fill(ds, "AllTables")
        status.DataSource = ds
        status.DataSource = ds.Tables(0)
        status.DataTextField = ds.Tables(0).Columns("status").ColumnName.ToString()
        status.DataBind()
        Dim ii As Integer
        For ii = 0 To status.Items.Count - 1
        Next
        status.Items.Insert(0, New ListItem("Select", 0))


QUESTION -
I would like to have an item pre-selected in the dropdown based on a flag on the table called default_select.

How can I get the SelectedText to be from a second query where default_select = 1 ?  or do I even need a second query?

many thanks in advance
LVL 1
H-SCAsked:
Who is Participating?
 
AmmarRConnect With a Mentor Commented:
hi dear

use FindByValue

status.SelectedIndex = status.Items.IndexOf(status.Items.FindByValue("1"))



0
 
AmmarRCommented:
hi use this line

you can added to the end of your script


        status.SelectedIndex = status.Items.IndexOf(status.Items.FindByText("default text"))
0
 
CmdoProg2Connect With a Mentor Commented:
try adding the column to your adaptor and use a dataview to find it..  I assume default_select equals 1 in this snippet.

myconnection = New SqlClient.SqlConnection("Server=myserver;Database=my_database;Trusted_Connection=yes")
    myda = New SqlClient.SqlDataAdapter("Select status, default_select from STATUS_VW order by status", myconnection)
    Dim dv As Data.DataView
    ds = New DataSet
    myda.Fill(ds, "AllTables")
    status.DataSource = ds
    status.DataSource = ds.Tables(0)
    status.DataTextField = "status" '  ds.Tables(0).Columns("status").ColumnName.ToString()
    status.DataValueField = "status"
    status.DataBind()

    'Dim ii As Integer
    'For ii = 0 To status.Items.Count - 1
    'Next
    status.Items.Insert(0, New ListItem("Select", 0))
    dv = ds.Tables(0).DefaultView
    dv.RowFilter = "default_select = 1"
    If dv.Count > 0 Then status.SelectedValue = dv(0)("status")
    dv.RowFilter = ""

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
H-SCAuthor Commented:
AmmarR

Thanks for the reply.  I am not sure if this will work.  I am looking for a column value to be true or false on a table.  For example,

the table looks like this

STATUS    DEFAULT_SELECT
item1         0
item2         1
item3         0
item4         0

so in this case item2 would be pre-selected because the default_select column has a 1, and not because I would be looking for "item2" in the status column.
0
 
H-SCAuthor Commented:
AmmarR,

Works perfect!  Many thanks
0
 
H-SCAuthor Commented:
CmdoProq2,

Your solution works perfect as well!!  Many thanks
0
 
H-SCAuthor Commented:
AmmarR and CmdoProq2,

Do you mind if I split points? since both solutions will work..

0
 
CmdoProg2Commented:
No problem, but it's up to your judgement on how you award points.  Thanks for asking though.
0
 
AmmarRCommented:
Dear H-SC:

sorry for delay i was out, and yeah do what ever you like

Regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.