Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'Dropdown list' has a SelectedValue which is invalid because it does not exist in the list of items

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
0
0
GB
I have a range of textboxes and dropdown lists on my page. On the load event I am populating to these controls with a record from a table in the database.

I have a field called AgeRange. I want to display the value in the field as the selected item of a dropdownlist. Then they can change if required.

The datasource for the dropdown list is a list of AgeRanges from another table.

the error occurs if the value in the agerange field of the main record is NULL or empty

Code:
 Protected Sub PopulatePersonalDetails()

        Dim strConnString As String = ConfigurationManager.ConnectionStrings("HostLinkConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        Dim reader As SqlDataReader
        Dim sqlDa As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()

        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "spGetPersonalDetails"
        cmd.Parameters.Add("@HostID", SqlDbType.Int).Value = Session("HostID")
        cmd.Connection = con
        con.Open()
        sqlDa.Fill(dt)
        If dt.Rows.Count > 0 Then

            txtFirstname.Text = dt.Rows(0)("Firstname").ToString()  'Where ColumnName is the Field from the DB that you want to display
            txtSurname.Text = dt.Rows(0)("Surname").ToString()
            ddlAgeRange.SelectedValue = dt.Rows(0)("AgeRange").ToString()
            ddlGender.SelectedValue = dt.Rows(0)("Gender").ToString()
            txtOccupation.Text = dt.Rows(0)("Occupation").ToString()
            ddlNationalities.SelectedValue = dt.Rows(0)("Nationality").ToString()
            ddlReligions.SelectedValue = dt.Rows(0)("Religion").ToString()
            ddlEthnicity.SelectedValue = dt.Rows(0)("Ethnicity").ToString()


        End If

        reader = cmd.ExecuteReader
        con.Close()

    End Sub

Code:
<asp:DropDownList ID="ddlAgeRange" runat="server" DataSourceID="GetAgeRanges" 
                            DataTextField="AgeRange" DataValueField="AgeRange" AppendDataBoundItems="True">
                            <asp:ListItem Value="" >Please Select</asp:ListItem>
                        </asp:DropDownList>
 
just add try
Code:
Protected Sub PopulatePersonalDetails()

        Dim strConnString As String = ConfigurationManager.ConnectionStrings("HostLinkConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        Dim reader As SqlDataReader
        Dim sqlDa As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()

        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "spGetPersonalDetails"
        cmd.Parameters.Add("@HostID", SqlDbType.Int).Value = Session("HostID")
        cmd.Connection = con
        con.Open()
        sqlDa.Fill(dt)
        If dt.Rows.Count > 0 Then

            txtFirstname.Text = dt.Rows(0)("Firstname").ToString()  'Where ColumnName is the Field from the DB that you want to display
            txtSurname.Text = dt.Rows(0)("Surname").ToString()
           [COLOR=#EF2929] try
            {
            ddlAgeRange.SelectedValue = dt.Rows(0)("AgeRange").ToString()
             }
             catch {}[/color]
            ddlGender.SelectedValue = dt.Rows(0)("Gender").ToString()
            txtOccupation.Text = dt.Rows(0)("Occupation").ToString()
            ddlNationalities.SelectedValue = dt.Rows(0)("Nationality").ToString()
            ddlReligions.SelectedValue = dt.Rows(0)("Religion").ToString()
            ddlEthnicity.SelectedValue = dt.Rows(0)("Ethnicity").ToString()


        End If

        reader = cmd.ExecuteReader
        con.Close()

    End Sub
 
I do code this way to avoid the error:
Code:
dim listItemSelected as ListItem = Me.ddlAgeRange.Items.FindByText(dt.Rows(0)("AgeRange").ToString()))
If listItemSelected IsNot Nothing Then
   listItemSelected.Selected = True
End If
 
Both options work in the fact the error dissappears however jbenseon your code doesnt pull through the value stored in the database if there is a value. any ideas?
 
I'm not sure what you mean by your reply. The FindByText() will look for the text of your database value in the list.
If you want to look it up by value then use FindByValue(). I am not sure how your dropdown list is structured and what the values in your DB are, so you will have to decide.
If you post your ddl markup and some sample data that you expect from the DB, I can help you further.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top