This is a migrated thread and some comments may be shown as answers.
Conversion from type 'DBNull' to type 'Boolean' is not valid.
5 Answers 252 Views
This is a migrated thread and some comments may be shown as answers.
Shehab
Top achievements
Rank 1
Shehab asked on 11 Jul 2008, 12:40 PM
Hi All,

I have a RadGrid that you can edit and add a new record in. when I click on Edit, it show the edit form and works fine, and when I click on Add new record it also works fine because I added this code in the commandItem of the grid:
    Protected Sub rgResidentAdmin_ItemCommand(ByVal source As ObjectByVal e As Telerik.WebControls.GridCommandEventArgs) Handles rgResidentAdmin.ItemCommand  
        If (e.CommandName = RadGrid.InitInsertCommandName) Then 
            e.Canceled = True 
            Dim newValues As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary()  
            newValues("ResIndic") = "1" 
            e.Item.OwnerTableView.InsertItem(newValues)  
        End If 
    End Sub 
Now if I click on Add new record and without clicking on Insert or Cancle then click on edit for one of the other record, it give me this error: Conversion from type 'DBNull' to type 'Boolean' is not valid.
That is because I have a checkbox in the EditItemTemplate:
                          <radG:GridTemplateColumn DataField="ResIndic" HeaderText="Active" UniqueName="ResIndic" AllowFiltering="False">  
                                <ItemTemplate> 
                                    <asp:CheckBox ID="cbResIndic" runat="server" Enabled="false" /> 
                                </ItemTemplate> 
                                <EditItemTemplate> 
                                    <asp:CheckBox ID="ResIndicCB" runat="server" Checked='<%# Bind("ResIndic")%>' /> 
                                </EditItemTemplate> 
                          </radG:GridTemplateColumn> 
Is there a way to fix this by adding a line of code or a way around it by disabling the user to click edit when Add new record button is clicked(inserting a new record)?

Thank you in advance
Shehab

5 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 11 Jul 2008, 01:14 PM
Hello Shehab,

The problem lies in the checkbox control not being able to convert a null value returned from the data source to a boolean value. If you want to workaround the problem, the best approach would be to either check each the EdiFormTemplate's ParentItem in ItemDataBound event and assigne a false value for the checkbox when its data field is null. Another approach would be to extend your CheckBox control class and implement your own checkbox which handles DBNull values. Here is a sample:

public class CustomCheckBox : CheckBox 
    private bool _checked; 
 
    public CustomCheckBox() 
    { 
        _checked = base.Checked; 
    } 
 
    public override bool Checked 
    { 
        get 
        { 
            return _checked; 
        } 
        set 
        { 
            if (value.GetType() == DBNull.Value.GetType()) 
                _checked = false
            else if (value.GetType() == typeof(bool)) 
                _checked = (bool)value; 
            else 
                _checked = false
        } 
    } 

Now you can use the custom checkbox control inside a GridTemplateColumn instead of the default one.

All the best,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Dustin
Top achievements
Rank 1
answered on 27 Sep 2010, 08:39 PM
Wouldn't creating an override property on Checked fail if you pass a DBNull to the property because the value's data type is boolean which does not convert to DBNull? Even though you are handling a passed DBNull in the set statement, it wouldn't make it that far because of conversion issues when converting the DBNull to boolean on the input variable.
0
Cori
Top achievements
Rank 2
answered on 27 Sep 2010, 09:01 PM
Hello Shehab,

Why don't you use a GridCheckBoxColumn instead, as it does the same thing, but is capable of handling the null situation.

I hope that helps.

EDIT: You can also set a DefaultValue for it as well.
0
Dustin
Top achievements
Rank 1
answered on 27 Sep 2010, 09:05 PM
Sorry, my response was more in general and not specially for a rad control.

I ran into a similar issue in the radGrid FormTemplate where I had to an actual checkbox. My solution ended up being

-- New Check control --

Public Class CheckBox
        Inherits Web.UI.WebControls.CheckBox

        Public Property CheckedOverride() As Object
            Get
                Return MyBase.Checked
            End Get
            Set(ByVal value As Object)
                If IsDBNull(value) Then
                    MyBase.Checked = False
                ElseIf value.GetType() = GetType(Boolean) Then
                    MyBase.Checked = value
                Else
                    MyBase.Checked = False
                End If
            End Set
        End Property

    End Class

-- Markup --
<xc:CheckBox ID="chkIsActive" runat="server" CheckedOverride='<%#Bind("IsActive")%>' />
0
Steve Boulanger
Top achievements
Rank 1
answered on 04 Nov 2011, 08:29 PM
I had the same problem and solved it by adding a grid checkbox column to the columns collection.  Even though the column has Display="false" it somehow allows the checkbox on the edit form template (for a popup form) to work when in insert mode.

<

 

 

telerik:GridCheckBoxColumn DataField="MyCheckField" DataType="System.Boolean" DefaultInsertValue="False" Display="False"

 

 

 

FilterControlAltText="Filter column column" UniqueName="MyCheckField">

 

 

 

</telerik:GridCheckBoxColumn>

 

Tags
Grid
Asked by
Shehab
Top achievements
Rank 1
Answers by
Veli
Telerik team
Dustin
Top achievements
Rank 1
Cori
Top achievements
Rank 2
Steve Boulanger
Top achievements
Rank 1
Share this question
or