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

Conversion from type 'DBNull' to type 'Boolean' is not valid.

3 Answers 675 Views
Button
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 12 Jan 2014, 02:30 PM

I'm getting an error "Conversion from type 'DBNull' to type 'Boolean' is not valid." upon displaying an insert form inside a RadGrid that has two RadButton controls with the ToggleType="Checkbox" that have the Checked property being bound from a database.

<telerik:RadButton ID="RXRequired" runat="server" ForeColor="White"

   
AutoPostBack="false"
ButtonType="ToggleButton" Checked='<%# Bind("RXRequired")%>'

    ToggleType="CheckBox" EnableViewState="true" >

    <ToggleStates>

         <telerik:RadButtonToggleState PrimaryIconCssClass="rbRemove" />

         <telerik:RadButtonToggleState PrimaryIconCssClass="rbOk" Selected="true" />

    </ToggleStates>

</telerik:RadButton>

I have seen numerous posts all over the web regarding DBNull values and checkboxes upon initiating a form insert with the Bind statement. This apparently has been going on for many, many years. This is truly amazing that this is still an issue today, many years later! When the hell is Telerik going to fix this issue with their controls? Come on guys, fix this already!!! This is a real simple thing to do, why should this still be an issue?


This is insane already. FIX IT!!!!!!!!!!!!!!!!!!!!!!

3 Answers, 1 is accepted

Sort by
0
Steve
Top achievements
Rank 1
answered on 12 Jan 2014, 03:50 PM
Some additional information.

I tried some of the "workarounds" found on the net, but so far none are working.

If I try the following code,

checked='<%# IIf(Eval("RXRequired") Is DBNull.Value, False, Eval("RXRequired")) %>'

The form will load fine, but when I try to insert, I get an error "Procedure or function VendorProductInsert has too many arguments specified."

If I do the following code,

    Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
        Select Case e.CommandName
            Case RadGrid.InitInsertCommandName
                e.Canceled = True
                Dim newValue As New System.Collections.Specialized.ListDictionary()
                newValue("RXRequired") = False
                e.Item.OwnerTableView.InsertItem(newValue)
        End Select
    End Sub

The form will load fine, but when I try to insert, I get an error "Procedure or function VendorProductInsert has too many arguments specified."


So, how do I work around this issue? I'm using SQLDataSource in the form. I really don't want to have to change the whole thing around to do everything in the code behind.
0
Danail Vasilev
Telerik team
answered on 16 Jan 2014, 03:19 PM
Hi Steve,

The checked property of the RadButton accepts only boolean values, so that empty values cannot be set to it. The same is with a standard ASP Button. Therefore this is not an issue with the control and I can suggest that you set an if statement there. For example:
ASPX:
<telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false">
    <MasterTableView>
        <Columns>
            <telerik:GridTemplateColumn DataField="ID" UniqueName="name1"
                HeaderText="header column">
                <ItemTemplate>
                    <asp:CheckBox ID="Checkbox1" Text="text" runat="server" Checked='<%# Eval("RXRequired") == DBNull.Value ? false : Eval("RXRequired") %>' />
                    <telerik:RadButton ID="RXRequired" runat="server" ForeColor="White"
                        AutoPostBack="false" ButtonType="ToggleButton" Checked='<%# Eval("RXRequired") == DBNull.Value ? false : Eval("RXRequired") %>'
                        ToggleType="CheckBox" EnableViewState="true">
                        <ToggleStates>
                            <telerik:RadButtonToggleState PrimaryIconCssClass="rbRemove" />
                            <telerik:RadButtonToggleState PrimaryIconCssClass="rbOk" Selected="true" />
                        </ToggleStates>
                    </telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
C#:
protected void Page_Load(object sender, EventArgs e)
{
    RadGrid1.DataSource = GetData();
    RadGrid1.DataBind();
}
 
protected DataTable GetData()
{
    DataTable dt = new DataTable();
 
    dt.Columns.Add("ID");
    dt.Columns.Add("RXRequired", typeof(bool));
 
    dt.Rows.Add(1, true);
    dt.Rows.Add(2, DBNull.Value);
    dt.Rows.Add(3, false);
    dt.Rows.Add(4, true);
 
    return dt;
}

The above code works properly on my side.

You can also find the full VS example in the attached archive.

As for the InsertItemTemplate you can handle the server-side ItemCommand event handler where the checked property of the RadButton can be obtained and stored in the database. You can find a VS example in the other attached archive that illustrates that approach with a DataTable data source, stored in the ViewState.

Regards,
Danail Vasilev
Telerik
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 the blog feed now.
0
Steve
Top achievements
Rank 1
answered on 16 Jan 2014, 11:40 PM
I got a suggestion to do the below code, which solved my issue.

Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
        
        If e.CommandName = RadGrid.InitInsertCommandName
                If RadGrid1.EditIndexes.Count > 0 Then  'Already in edit mode.
                    e.Canceled = True
                    Exit Sub
                End If
                e.Canceled = True
                Dim newValue As New System.Collections.Specialized.ListDictionary()
                newValue("RXRequired") = False
                newValue("ReorderToMax") = False
                newValue("Active") = True
 
                e.Item.OwnerTableView.InsertItem(newValue)
         End if
    End Sub
Tags
Button
Asked by
Steve
Top achievements
Rank 1
Answers by
Steve
Top achievements
Rank 1
Danail Vasilev
Telerik team
Share this question
or