This is a migrated thread and some comments may be shown as answers.

Selection out of range Parameter name: value

2 Answers 323 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 23 Apr 2012, 07:14 PM

Hi

I am going round in circles trying to beat this problem which I fairly sure is a result trying bind the value of the combos box item to a column which may contain null value. The error occurs as soon as the the grid's 'Edit' button is clicked with some database items/

The relevant grid declaration is like this

<telerik:GridTemplateColumn DataField="Marshals" HeaderText="Marshal"
            SortExpression="Marshals" UniqueName="Marshals" Visible="False">
              
            <EditItemTemplate>         
                   <telerik:RadComboBox ID="RadComboBox1" Runat="server"
                       AppendDataBoundItems="True" SelectedValue='<%# bind("Marshals") %>'
                       Width="225px">
                   </telerik:RadComboBox>
  
            </EditItemTemplate>
 <ItemTemplate>
                <asp:Label ID="MarshalsLabel" runat="server" Text='<%# Eval("Marshals") %>'></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>

And the code behind builds a dataset and binds to the combo
Protected Sub RadGrid1_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
 
        If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
            Dim editform As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
            Dim combo As RadComboBox = DirectCast(editform.FindControl("RadComboBox1"), RadComboBox)
            Bind(combo)
            Dim combo2 As RadComboBox = DirectCast(editform.FindControl("RadComboBox2"), RadComboBox)
            Bind(combo2)
        End If
    End Sub
 
    Private Sub Bind(combo As RadComboBox)
        Dim strMemberID As String = ""
        Dim sb As New StringBuilder
 
 
        Dim Ds As New DataSet
        Dim UserTable As New DataTable("UserTable")
        UserTable.Columns.Add("Text", GetType(String))
        UserTable.Columns.Add("Value", GetType(String))
        UserTable.Rows.Add("Pick one", "0")
 
        Dim rdr As SqlDataReader = ConstClass.getCurrentMembers()
        If rdr.HasRows Then
            While rdr.Read()
                ' build a row of the combo
                ' Text
                If Not IsDBNull(rdr("FirstName")) Then
                    sb.Append(rdr("FirstName"))
                Else
                    sb.Append("")
                End If
                sb.Append(" ")
                If Not IsDBNull(rdr("LastName")) Then
                    sb.Append(rdr("LastName"))
                Else
                    sb.Append("")
                End If
                sb.Append(" & ")
                If Not IsDBNull(rdr("PartnerFirstName")) Then
                    sb.Append(rdr("PartnerFirstName"))
                Else
                    sb.Append("")
                End If
                sb.Append(" ")
                If Not IsDBNull(rdr("PartnerLastName")) Then
                    sb.Append(rdr("PartnerLastName"))
                Else
                    sb.Append("")
                End If
 
                If Not IsDBNull("MemberID") Then
                    strMemberID = rdr("MemberID")
                Else
                    strMemberID = "0"
                End If
 
                UserTable.Rows.Add(sb.ToString, strMemberID)
                sb.Clear()
            End While
            UserTable.AcceptChanges()
            Ds.Tables.Add(UserTable)
            Ds.AcceptChanges()
            rdr.Close()
 
 
            LiteralTest.Text = sb.ToString
        Else
            UserTable.Rows.Add(sb.ToString, strMemberID)
            UserTable.AcceptChanges()
            Ds.Tables.Add(UserTable)
            Ds.AcceptChanges()
            rdr.Close()
        End If
 
        combo.DataTextField = "Text"
        combo.DataValueField = "Value"
        combo.DataSource = Ds
 
    End Sub

This all works just fine until when you try to edit an item that has a Null value for 'Marshals' (which is an integer) in the record.
How do I escape?  I have tried several of the solutions in this forum that seem as if they might be my case but to no avail so far.

BTW I am using Q2 2010 on this website.

Thanks

Clive

2 Answers, 1 is accepted

Sort by
0
Clive Hoggar
Top achievements
Rank 1
answered on 24 Apr 2012, 04:47 PM
I have been able to focus in a bit.  The root cause of the error is not a null value when trying to bind the selectedValue to the existing field content, it is that there there are cases where there the current column content is no match to any of the current list items, although there may have been in the past. 

In this case I would just like to show the default item 'Pick one'
0
Dimitar Terziev
Telerik team
answered on 25 Apr 2012, 12:24 PM
Hello Clive,

The reason for the experienced behavior is indeed the fact that you want to bind a value which is not presented among the items of the RadComboBox. In such scenarios like yours you shouldn't define the SelectedValue using binding expression in the mark up, but rather set it manually from code behind if an item with such value exists. In order to do that you could use the ItemDataBound event of the RadGrid and preselect the selected value of the RadComboBox in the event handler function.

Kind regards,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
ComboBox
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Clive Hoggar
Top achievements
Rank 1
Dimitar Terziev
Telerik team
Share this question
or