DetailsView dropdownlist selected value?

Posted on 2012-09-21
Last Modified: 2012-09-24
Hello, having trouble figuring out how to properly assign a selected value to a data-driven dropdownlist control within a detailsview in Edit mode.  I have working the population of the dropdownlist with the data from the Plant table, but need to assign the selected value as the result of my first selectCommand (original value before any editing editing done)... Any help appreciated.
    Protected Sub DVSeedTracker_DataBound(sender As Object, e As System.EventArgs) Handles DVSeedTracker.DataBound

        Dim ddPlant As DropDownList = TryCast(DVSeedTracker.FindControl("ddPlant"), DropDownList)
        Dim row As DataRowView = TryCast(DVSeedTracker.DataItem, DataRowView)

        If ddPlant IsNot Nothing Then

            Using stCon As New SqlConnection(ConfigurationManager.ConnectionStrings("SeedTrackerConnectionString").ConnectionString)


                Dim selectCommand1 As New SqlCommand("SELECT Plant FROM SeedTracker WHERE STID=@STID", stCon)
                selectCommand1.Parameters.AddWithValue("STID", sSTID)
                Dim sPlantID As String = CType(selectCommand1.ExecuteScalar, String)

                Dim selectCommand2 As New SqlCommand("SELECT PLID, PLANT, LID FROM Plant ORDER BY Plant DESC", stCon)
                Dim stDataReader As SqlDataReader = selectCommand2.ExecuteReader
                If stDataReader.HasRows Then
                    While stDataReader.Read()
                        If Not stDataReader.Item("PLID") Is DBNull.Value Then
                            Dim intPLID As Integer = stDataReader.Item("PLID")
                            Dim sPLant As String = stDataReader.Item("Plant")
                            Dim sLabID As String = stDataReader.Item("LID")
                            ddPlant.Items.Insert(0, sPLant)
                            If intPLID = sPlantID Then
                                ddPlant.SelectedIndex = ddPlant.Items.IndexOf(ddPlant.Items.FindByValue(row("PLID").ToString()))
                            End If
                        End If
                    End While
                End If

            End Using

        End If

    End Sub

Open in new window

Question by:Ahelbling
    LVL 26

    Expert Comment

    by:Alan Warren
    Hi Andrew,

    try modifying the SqlParameterCollection.AddWithValue Method to include the SQL parameter prefix '@'.

    selectCommand1.Parameters.AddWithValue("STID", sSTID)
    selectCommand1.Parameters.AddWithValue("@STID", sSTID)

    LVL 29

    Accepted Solution


    if(DropDownList1.Items.Contains(DropDownList1.Items.FindByValue("Value"))) {     DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Value)); }

    Author Closing Comment

    Thank you!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    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…
    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my 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…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 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

    22 Experts available now in Live!

    Get 1:1 Help Now