We help IT Professionals succeed at work.

Specified argument was out of the range of valid values. Parameter name: value in ASP .net combo box

Kaporch
Kaporch asked
on
Medium Priority
8,096 Views
Last Modified: 2012-06-21
I have the following code in my ASP .net application to fill a combo box with information from the database:

Private Sub FillSummonsNo()
        'BK - changed summons no from being free-form text entry to being a dropdown
        Dim strSQL As String
        Dim drcmbSummonNo As SqlDataReader

        Try
            'Set the Data Value and Text fields to the db column
            cmbSummonNo.DataValueField = "summonsid"
            cmbSummonNo.DataTextField = "summonscode"

            strSQL = "select S.* from tblSummons S where S.status_code=1 order by S.summonsid"
            Dim cmd As SqlCommand = New SqlCommand(strSQL, Conn)
            drcmbSummonNo = cmd.ExecuteReader()

            cmbSummonNo.DataSource = drcmbSummonNo
            cmbSummonNo.DataBind()

            'Insert an item at the begining of the list.
            cmbSummonNo.Items.Insert(0, "<--- Select --->")

            'Add the rest of the fields from the database.
            Do While drcmbSummonNo.Read()
                cmbSummonNo.Items.Add(New ListItem(drcmbSummonNo.GetString(1), drcmbSummonNo.GetString(0)))
                cmbSummonNo.DataSource = drcmbSummonNo
                cmbSummonNo.DataBind()
            Loop


        Catch ex As Exception
            Me.lblErrMsg.Text = ex.Message.ToString
        Finally
            'Close the reader
            drcmbSummonNo.Close()
        End Try
    End Sub

As soon as the code hits the databind statement, I get this error:  Specified argument was out of the range of valid values. Parameter name: value .  This error appears on my web page, but the combo box is correctly filled.  Any ideas on how to get rid of this error appreciated.  The tblSummons database table has the following fields:

SummonsID               numeric              key
SummonsCode           varchar(50)
SummonsDescription  varchar(255)
StatusCode                bit

At one time, I was showing using the summonscode as the datavalue field, and the summonsdescription as the datatextfield, and I didn't have this error.  Any insights helpful.
Comment
Watch Question

Head of Software Services
Commented:
Hi Kaporch,

Lets simplify a little:

Private Sub FillSummonsNo()
        'BK - changed summons no from being free-form text entry to being a dropdown
        Dim strSQL As String
        Dim drcmbSummonNo As SqlDataReader

        Try
            'Set the Data Value and Text fields to the db column
            cmbSummonNo.DataValueField = "summonsid"
            cmbSummonNo.DataTextField = "summonscode"

            strSQL = "Select 0 As SummonsID,'<--- Select --->' As SummonsCode Union All select SummonsID, SummonsCode from tblSummons S where S.status_code=1 order by S.summonsid"
            Dim cmd As SqlCommand = New SqlCommand(strSQL, Conn)
            drcmbSummonNo = cmd.ExecuteReader()

            cmbSummonNo.DataSource = drcmbSummonNo
            cmbSummonNo.DataBind()

        Catch ex As Exception
            Me.lblErrMsg.Text = ex.Message.ToString
        Finally
            'Close the reader
            drcmbSummonNo.Close()
        End Try
    End Sub

This simply adds the extra item automatically and avoids having to insert it, also you appeared to be adding data again and again if I was reading the code correctly.

Also limiting the field list in the query (rather than using * is more efficient).

Tim Cottee

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
This would do the job:

_____________________________________________

        Dim strSQL As String
        Dim drcmbSummonNo As System.Data.SqlClient.SqlDataReader

        strSQL = "select S.* from tblSummons S where S.status_code=1 order by S.summonsid"
        Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(strSQL, Conn)
        drcmbSummonNo = cmd.ExecuteReader()

        Do While drcmbSummonNo.Read()
            cmbSummonNo.Items.Add(New ListItem(drcmbSummonNo.GetString(1), drcmbSummonNo.GetString(0)))
        Loop

        drcmbSummonNo.Close()

_____________________________________________

You can also use an SqlDataAdapter to fill a DataSet, and set the DataSource property of the DropDownList to point to the DataSet, and set the DataTextField and the DataValueField properties of the DropDownList as appropriate, without having to manually fill the DropDownList items.

_______________

  Nayer Naguib
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.