• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 839
  • Last Modified:

DetailsView dropdownlist selected value?

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)

                stCon.Open()

                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

                stDataReader.Close()
                stCon.Close()
            End Using

        End If

    End Sub

Open in new window

ErrorSQL1SQL2
TIA,
Andrew
0
Ahelbling
Asked:
Ahelbling
1 Solution
 
Alan WarrenCommented:
Hi Andrew,

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

Change:
selectCommand1.Parameters.AddWithValue("STID", sSTID)
To:
selectCommand1.Parameters.AddWithValue("@STID", sSTID)

Alan
0
 
Kumaraswamy RCommented:
Hi

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

http://weblogs.asp.net/sukumarraju/archive/2009/12/23/populating-drop-down-list-selected-item-while-detailsview-in-edit-mode.aspx

http://stackoverflow.com/questions/2700028/binding-a-dropdownlist-inside-a-detailsview
0
 
AhelblingAuthor Commented:
Thank you!
0

Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now