?
Solved

SelectedText on DropDownList

Posted on 2011-10-25
9
Medium Priority
?
249 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:H-SC
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:AmmarR
ID: 37029572
hi use this line

you can added to the end of your script


        status.SelectedIndex = status.Items.IndexOf(status.Items.FindByText("default text"))
0
 
LVL 12

Assisted Solution

by:CmdoProg2
CmdoProg2 earned 1000 total points
ID: 37030418
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
 
LVL 1

Author Comment

by:H-SC
ID: 37030478
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
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!

 
LVL 15

Accepted Solution

by:
AmmarR earned 1000 total points
ID: 37036282
hi dear

use FindByValue

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



0
 
LVL 1

Author Comment

by:H-SC
ID: 37037550
AmmarR,

Works perfect!  Many thanks
0
 
LVL 1

Author Comment

by:H-SC
ID: 37037558
CmdoProq2,

Your solution works perfect as well!!  Many thanks
0
 
LVL 1

Author Comment

by:H-SC
ID: 37037563
AmmarR and CmdoProq2,

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

0
 
LVL 12

Expert Comment

by:CmdoProg2
ID: 37037875
No problem, but it's up to your judgement on how you award points.  Thanks for asking though.
0
 
LVL 15

Expert Comment

by:AmmarR
ID: 37040695
Dear H-SC:

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

Regards
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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

862 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