Specified argument was out of the range of valid values. Parameter name: value

Hi
I am getting this error:
Specified argument was out of the range of valid values. Parameter name: value

when i try to populate a DDL with a preselected value...
the code that (i THINK) is relevant is:

Sub Page_Load(Sender As Object, E As EventArgs)
           Dim ConnectToDatabase as new SQLConnection("UID=SA;PWD=hello; Data Source=(local); database=builders;")
           Dim DDLAdapterStatus as new SQLDataAdapter("SELECT * from tbl_status",connecttodatabase)
           Dim DS1 as new Dataset
           Dim DDLAdapterProperty as new SQLDataAdapter("SELECT DISTINCT RTRIM(ISNULL(address1, '')) + ' ' + RTRIM(ISNULL(address2, '')) AS title, propertyID FROM tbl_property",connecttodatabase)
           Dim DS2 as new Dataset
           Dim DDLAdapterCalltype as new SQLDataAdapter("SELECT * from tbl_calltype",connecttodatabase)
           Dim DS3 as new Dataset

           connecttodatabase.open
           DDLAdapterStatus.Fill(Ds1, "status")
           DDLAdapterProperty.Fill(Ds2, "property")
           DDLAdapterCalltype.Fill(Ds3, "calltype")
           connecttodatabase.close
...
...
...
If Not IsPostBack Then

           jobID = Trim(request.querystring("jobID"))
           GetJob(jobid)
           Row = ClassyDS.Tables("Jobs").Rows(0)
           jobtitle.Text = Row.Item("jobtitle").ToString()
           jobdescription.Text = Row.Item("jobdescription").ToString()
           began.Text = Row.Item("began").ToString()
           enddate.Text = Row.Item("enddate").ToString()
           extensiondate.Text = Row.Item("extensiondate").ToString()
           drawingref.Text = Row.Item("drawingref").ToString()

                    ddlstatus.DataSource = Ds1               '<--- (Dataset Name)
                    ddlstatus.DataMember = "status"               '<--- (Table Name)
                    ddlstatus.DataTextField = "statusname"               '<---    (Column Name)
                    ddlstatus.DataValueField = "statusid"               '<--- (Column Name)
                    ddlStatus.SelectedValue = Row.Item("statusid")

                    ddlproperty.DataSource = Ds2               '<--- (Dataset Name)
                    ddlproperty.DataMember = "property"               '<--- (Table Name)
                    ddlproperty.DataTextField = "title"               '<---    (Column Name)
                    ddlproperty.DataValueField = "propertyid"               '<--- (Column Name)
                    ddlproperty.SelectedValue = Row.Item("propertyid")

                    ddlcalltype.DataSource = Ds3               '<--- (Dataset Name)
                    ddlcalltype.DataMember = "calltype"               '<--- (Table Name)
                    ddlcalltype.DataTextField = "typename"               '<---    (Column Name)
                    ddlcalltype.DataValueField = "typeid"               '<--- (Column Name)
                    'ddlcalltype.SelectedValue = Row.Item("calltype")  '<-----------THIS IS THE LINE THAT IS CAUSING PROBLEMS
                    page.databind

       End If
...
...
...
Sub GetJob(JobID As Integer)
       SelectStatement = "Select * From tbl_job Where jobid =" & JobID
       ConnectString = "Provider=SQLOLEDB; UID=SA;PWD=hello; Data Source=(local); database=builders;"
       Connect.ConnectionString = ConnectString
       Adapter.SelectCommand =  new OleDbCommand(SelectStatement, Connect)
       ClassyCB  = New OleDbCommandBuilder(Adapter)
       Adapter.Fill(ClassyDS,"jobs")
 End Sub

The field in tbl_job i want to use as the preselected field is called "Calltype". When i comment out this line the page works fine...

any ideas? let me know if you need more info

thanks
Nick
lz7cjcAsked:
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.

shivam2kCommented:
if already one is selected, setting like this would give error
ddlcalltype.SelectedValue = Row.Item("calltype")  

u can use like this
ddlcalltype.SelectedIndex = - 1
ddlcalltype.SelectedValue = Row.Item("calltype")  

or
stringvariable = Convert.ToString(Row.Item("calltype"))
ddlcalltype.selectedindex =  ddlcalltype.Items.IndexOf(ddlcalltype.Items.FindByValue(stringvariable))

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
lz7cjcAuthor Commented:
would you mind explaining to me why that is the case for this drop down list only? whilst i try the code you have given me
thanks
lz7cjcAuthor Commented:
i get the same error with the first suggestion and the second doesn't choose the right item in the ddl
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

TorrwinCommented:
I believe you are getting this error, because you are trying to set a selected value for a value that doesn't exist, or you are trying to select the value of the the type using the type name.

Try changing
ddlcalltype.SelectedValue = Row.Item("calltype")  
to
ddlcalltype.SelectedText = Row.Item("calltype")

Or change:
ddlcalltype.SelectedValue = Row.Item("calltype")  
to
ddlcalltype.SelectedValue = Row.Item("typeid")
lz7cjcAuthor Commented:
thanks - i have merged your two answers and it now works... what worries me is that there is no reason i can think of why the numbering is out by one - any thoughts? obviously i don't want to cover up one bug with a seperate fix
                    stringvariable = Convert.ToString(Row.Item("calltype"))
                    ddlcalltype.selectedindex =  stringvariable - 1

thanks
lz7cjcAuthor Commented:
calltype is storing a number... that is the same as typeid as a foreign key... i appreciate this is pretty bad coding practice but it is down to a lack of planning and i would rather not change now!
TorrwinCommented:
I'm not sure why your index is off by one. Perhaps you should open a new question regarding that issue since this question is resolved?
shivam2kCommented:
dropdown list index is zero based index

if u had the drop down list like

1
2
3
4

then ddname.selectedindex = 0 selects value 1
ddname.selectedindex = 1 selects value 2

use only if the value is going to be sequential without any gaps

ddlcalltype.selectedindex =  stringvariable - 1

best practise would be always

ddlcalltype.SelectedIndex = - 1 ' resets the selected index, if was preselected
'if you want to select by value
        If Not ddlcalltype.Items.FindByValue(Convert.ToString(Row.Item("calltype"))) Is Nothing Then
          ddlcalltype.SelectedValue = Convert.ToString(Row.Item("calltype"))
        End If
' or if you want to select by text
        If Not ddlcalltype.Items.FindByText(Convert.ToString(Row.Item("calltype"))) Is Nothing Then
          ddlcalltype.SelectedText = Convert.ToString(Row.Item("calltype"))
        End If
 here you check if the value or text exists in the dropdown list

or simply use

ddlcalltype.SelectedIndex = - 1 ' resets the selected index, if was preselected
and
ddlcalltype.SelectedIndex = ddlcalltype.Items.IndexOf(ddlcalltype.Items.FindByValue(stringvariable))
or
ddlcalltype.SelectedIndex = ddlcalltype.Items.IndexOf(ddlcalltype.Items.FindByText(stringvariable))
lz7cjcAuthor Commented:
thanks for your help on this - and thanks for the explanation
all the best
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
ASP.NET

From novice to tech pro — start learning today.