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

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.
KaporchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nayernaguibCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.