INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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> 

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

just add try

CODE --> vb

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()
            try
            {
            ddlAgeRange.SelectedValue = dt.Rows(0)("AgeRange").ToString()
             }
             catch {}
            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 

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

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 

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

(OP)
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?

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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close