?
Solved

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

Posted on 2006-03-31
2
Medium Priority
?
8,079 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.
0
Comment
Question by:Kaporch
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 750 total points
ID: 16342386
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
 
LVL 14

Expert Comment

by:nayernaguib
ID: 16343615
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Make the most of your online learning experience.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question