Filling DropDownList, but getting error when selected value is 0

Hi there,

I'm trying to fill a dropdownlist in a datagrid and I'm running into an error:

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

When the value of the dropdownlist is 0.

Here's my aspx:
asp:DropDownList
Enabled="false"
ID="dropStates"                                                       
DataTextField="State_Acronym"
DataValueField="ID"
DataSource="<%# Load_States() %>"
SelectedValue='<%# Container.DataItem("State") %>'

here's my code-behind:

Public Function Load_States()
            Dim cmdSelect As SqlCommand
            Dim dtrStates As SqlDataReader
            Dim strSelect AS string
            Dim ddlDataSet as DataSet = New DataSet()
            Dim conPubs As New SqlConnection(ConfigurationSettings.appSettings("sqlconnectionString"))
            
            strSelect = "Select tblStates.* From tblStates"
            Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSelect, conPubs)  
            
            myDataAdapter.Fill(ddlDataSet, "Categories")
       
            Return ddlDataSet            
End Function

There's no value in the tblStates with a 0 so that's what's throwing the error.  Any thoughts on how to get this work?

Full Williams                                                      
                                                      Runat="Server" />
FullWilliamsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
DarthModConnect With a Mentor Commented:
PAQed with points refunded (500)

DarthMod
Community Support Moderator
0
 
bastibartelCommented:
Hi FullWilliams,

Try selecting 1.
As far as I remember VB has 1-based arrays.

Cheers!
0
 
RainMan82Commented:
yeah bastibartel is correct....they are all 1-based....
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
FullWilliamsAuthor Commented:
Selecting 1 would be the first state though and that's not correct.  Also, would I select it in the code or in the aspx?
0
 
FullWilliamsAuthor Commented:
I tried putting in:

myDataAdapter.SelectedValue = 0
 and got this error:

BC30456: 'SelectedValue' is not a member of 'System.Data.SqlClient.SqlDataAdapter'
0
 
FullWilliamsAuthor Commented:
increasing the points as I need to get this resolved.
0
 
FullWilliamsAuthor Commented:
I got it to by modifying my code behind:

myDataAdapter.Fill(ddlDataSet, "Categories")

        'adding a row
        dim row as datarow
        row = ddlDataSet.tables("Categories").newrow
        row("State_Acronym") = ""
        row("ID") = 0  
       
        ddlDataSet.tables("Categories").rows.add(row)

Please close this question
0
All Courses

From novice to tech pro — start learning today.