Object of type 'System.Boolean' cannot be converted to type 'System.Int16'.

I have a Checkbox on a detailsview bound to a small int field in my database table.

It binds fine when showing the item template on the detailsview, but when I try do an insert/update it blows up with that error.
Surely I can bind a checkbox to an integer field, so that unchecked translates to 0, and checked translates to 1?

The reason I use a smallint, is its future-proof.  If we decided to switch to a status instead off/on in the future, there are less changes required.

 

0
swaino
10/8/2007 12:43:47 PM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 6 Replies
👁️‍🗨️ 1599 Views

As long as 0 is False and any other value is considered True you should be able to bind the value like this:

Checked='<%# Convert.ToBoolean(Eval("YourColumnNameHere")) %>'

-=JW=-
0
jamezw
10/8/2007 1:39:50 PM

Thanks! But the problem seems to exist not when displaying the data but pushing it back to the database.
So I guess, I need to change Eval to Bind, but when I did I got this error:

Name 'Bind' is not declared.

 

0
swaino
10/8/2007 1:59:23 PM
Can you show the update or insert code? You would need to explicitly send an int value back based on the checkbox's checked status.

-=JW=-
0
jamezw
10/8/2007 2:38:57 PM

 yes here it is:

 

<asp:MultiView ID="mvSiteLoc" runat="server">
            <asp:View ID="vwViewAll" runat="server">
                <asp:GridView ID="gvSiteLocs" runat="server" BackColor="White" BorderColor="#E7E7FF"
                    BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" DataSourceID="SqlAllSitesDataSource"
                    AutoGenerateColumns="False" SelectedIndex="0" Width="662px" DataKeyNames="locId" AllowPaging="True">
                    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Left" />
                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                    <AlternatingRowStyle BackColor="#F7F7F7" />
                    <Columns>
                        <asp:CommandField ShowSelectButton="True" />
                        <asp:BoundField DataField="locName" HeaderText="Site" SortExpression="locName" />
                        <asp:BoundField DataField="armAreaName" HeaderText="Area" SortExpression="armAreaName" />
                        <asp:BoundField DataField="rgmRegionName" HeaderText="Region" SortExpression="rgmRegionName" />
                        <asp:BoundField DataField="locEmail" HeaderText="Site Email" SortExpression="locEmail" />
                        <asp:BoundField DataField="locManager" HeaderText="Manager" SortExpression="locManager" />
                        <asp:BoundField DataField="locShortName" HeaderText="Short Name" SortExpression="locShortName" />
                        <asp:BoundField DataField="locVamosId" HeaderText="Vamos Id" SortExpression="locVamosId" />
                        <asp:BoundField DataField="locNotifySiteAdminSupp" HeaderText="Site Admin - Suppress notification"
                            SortExpression="locNotifySiteAdminSupp" />
                    Columns>
                asp:GridView>
                <asp:SqlDataSource ID="SqlAllSitesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:247Db %>"
                    SelectCommand="usp_Loc_SelectWithAreaRegions" SelectCommandType="StoredProcedure">
                asp:SqlDataSource>
                <br />
                <br />
                <asp:DetailsView ID="dvSiteLoc" runat="server" Height="50px" Width="369px"
                    AutoGenerateRows="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                    BorderWidth="1px" CellPadding="3" DataKeyNames="locId" DataSourceID="SiteLocDataSource"
                    GridLines="Horizontal">
                    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                    <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" Width="10px" />
                    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                    <Fields>
                        <asp:BoundField DataField="locId" HeaderText="Id" InsertVisible="False" ReadOnly="True"
                            SortExpression="locId" />
                        <asp:TemplateField HeaderText="Area" SortExpression="locAreaId">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddlArea" runat="server" AppendDataBoundItems="True"
                                    Width="141px" DataSourceID="AreaDataSource" DataTextField="armAreaName" DataValueField="armId" SelectedValue='<%# Bind("locAreaId") %>'>
                                    <asp:ListItem>asp:ListItem>
                                asp:DropDownList>
                                <asp:RequiredFieldValidator ID="rfvReqArea" runat="server" ControlToValidate="ddlArea"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                            EditItemTemplate>
                            <InsertItemTemplate><asp:DropDownList ID="ddlArea" runat="server" AppendDataBoundItems="True" DataSourceID="AreaDataSource"
                                    DataTextField="armAreaName" DataValueField="armId" SelectedValue='<%# Bind("locAreaId") %>'
                                    Width="141px">
                                <asp:ListItem Selected="True">asp:ListItem>
                            asp:DropDownList><asp:RequiredFieldValidator ID="rfvReqArea" runat="server" ControlToValidate="ddlArea"
                                ErrorMessage="*">asp:RequiredFieldValidator>
                            InsertItemTemplate>
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlArea" runat="server" AppendDataBoundItems="True" Enabled="False"
                                    Width="141px" DataSourceID="AreaDataSource" DataTextField="armAreaName" DataValueField="armId" SelectedValue='<%# Bind("locAreaId") %>'>
                                    <asp:ListItem>asp:ListItem>
                                asp:DropDownList> 
                            ItemTemplate>
                        asp:TemplateField>
                        <asp:TemplateField HeaderText="Map Zone" SortExpression="locMapZone">
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="MapZonesDataSource"
                                    DataTextField="mpzZoneName" DataValueField="mpzId" SelectedValue='<%# Bind("locMapZone") %>'
                                    Width="141px">
                                asp:DropDownList>
                                <asp:RequiredFieldValidator ID="rfvMapZone" runat="server" ControlToValidate="DropDownList1"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                            EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="MapZonesDataSource"
                                    DataTextField="mpzZoneName" DataValueField="mpzId" SelectedValue='<%# Bind("locMapZone") %>'
                                    Width="141px">
                                asp:DropDownList>
                                <asp:RequiredFieldValidator ID="rfvMapZone" runat="server" ControlToValidate="DropDownList1"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                            InsertItemTemplate>
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="MapZonesDataSource"
                                    DataTextField="mpzZoneName" DataValueField="mpzId" Enabled="False" SelectedValue='<%# Bind("locMapZone") %>'
                                    Width="141px">
                                asp:DropDownList>
                            ItemTemplate>
                        asp:TemplateField>
                        <asp:TemplateField HeaderText="Site Location" SortExpression="locName">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("locName") %>'>asp:TextBox><asp:RequiredFieldValidator
                                    ID="rfvSiteLoc" runat="server" ControlToValidate="TextBox2" ErrorMessage="*">*asp:RequiredFieldValidator>
                            EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("locName") %>' MaxLength="50">asp:TextBox>
                                <asp:RequiredFieldValidator ID="rfvSiteLoc" runat="server" ControlToValidate="TextBox1"
                                    ErrorMessage="RequiredFieldValidator">*asp:RequiredFieldValidator>
                            InsertItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("locName") %>'>asp:Label>
                            ItemTemplate>
                        asp:TemplateField>
                        <asp:TemplateField HeaderText="Email" SortExpression="locEmail">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" MaxLength="50" Text='<%# Bind("locEmail") %>'
                                    Width="200px">asp:TextBox>
                                <asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="TextBox1"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="TextBox1"
                                    ErrorMessage="* Enter valid email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">asp:RegularExpressionValidator>
                            EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("locEmail") %>' MaxLength="50" Width="200px">asp:TextBox>
                                <asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="TextBox2"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="TextBox2"
                                    ErrorMessage="* Enter valid email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">asp:RegularExpressionValidator>
                            InsertItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("locEmail") %>'>asp:Label>
                            ItemTemplate>
                        asp:TemplateField>
                        <asp:TemplateField HeaderText="Manager's Name" SortExpression="locManager">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox3"  MaxLength="50" runat="server" Text='<%# Bind("locManager") %>'>asp:TextBox>
                                <asp:RequiredFieldValidator ID="rfvManager" runat="server" ControlToValidate="TextBox3"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                            EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:TextBox ID="TextBox3" MaxLength="50"  runat="server" Text='<%# Bind("locManager") %>'>asp:TextBox>
                                <asp:RequiredFieldValidator ID="rfvManager" runat="server" ControlToValidate="TextBox3"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                            InsertItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("locManager") %>'>asp:Label>
                            ItemTemplate>
                        asp:TemplateField>
                        <asp:TemplateField HeaderText="Site Short Name" SortExpression="locShortName">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox4" runat="server" width="50px" Text='<%# Bind("locShortName") %>' MaxLength="2">asp:TextBox>
                                <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="TextBox4"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                            EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:TextBox ID="TextBox4" runat="server" width="50px" maxlength="2" Text='<%# Bind("locShortName") %>' CausesValidation="True">asp:TextBox>
                                <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="TextBox4"
                                    ErrorMessage="*">asp:RequiredFieldValidator>
                            InsertItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("locShortName") %>'>asp:Label>
                            ItemTemplate>
                        asp:TemplateField>
                        <asp:TemplateField HeaderText="Vamosoft ID" SortExpression="locVamosId">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("locVamosId") %>' MaxLength="6" Width="50px">asp:TextBox> 
                                <asp:RangeValidator ID="rgvVamosId" runat="server" ControlToValidate="TextBox5" ErrorMessage="* Enter value"
                                    MinimumValue="0" MaximumValue="999999" Type="Integer">asp:RangeValidator>
                            EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("locVamosId") %>' Width="50">asp:TextBox>
                           <asp:RangeValidator ID="rgvVamosId" runat="server" ControlToValidate="TextBox5" ErrorMessage="* Enter value"
                                    MinimumValue="0" MaximumValue="999999" Type="Integer">asp:RangeValidator>
                            InsertItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label5" runat="server" MaxLength="6" Text='<%# Bind("locVamosId") %>'>asp:Label>
                            ItemTemplate>
                        asp:TemplateField>
                        <asp:TemplateField HeaderText="Suppress Notifications to Site Admin" SortExpression="locNotifySiteAdminSupp">
                            <EditItemTemplate>
                                 <asp:CheckBox ID="Suppress" runat="server" Checked='<%# Bind("locNotifySiteAdminSupp") %>' />
                            EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:CheckBox ID="Suppress" runat="server" Checked='<%# Bind("locNotifySiteAdminSupp") %>' />
                            InsertItemTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("locNotifySiteAdminSupp")) %>' enabled="false"/>
                            ItemTemplate>
                        asp:TemplateField>
                        <asp:CommandField ButtonType="Image" CancelImageUrl="~/App_Themes/247Theme/images/rd_btns/sm2_cancel.gif"
                            DeleteImageUrl="~/App_Themes/247Theme/images/rd_btns/sm2_Delete.gif" EditImageUrl="~/App_Themes/247Theme/images/rd_btns/sm2_edit.gif"
                            InsertImageUrl="~/App_Themes/247Theme/images/rd_btns/sm2_insert.gif" NewImageUrl="~/App_Themes/247Theme/images/rd_btns/sm2_New.gif"
                            ShowEditButton="True" ShowInsertButton="True" UpdateImageUrl="~/App_Themes/247Theme/images/rd_btns/sm2_update.gif" ShowDeleteButton="True" />
                    Fields>
                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                    <AlternatingRowStyle BackColor="#F7F7F7" />
                    <EmptyDataTemplate>
                        <div id="graphicHeading">
                            <asp:Image ID="Image1" runat="server" ImageUrl="~/App_Themes/247Theme/images/msgicons/info_icon_md.png" />
                            <strong>No Locations currently exist.strong>
                            <br />
                        div>
                        Click <strong>Create Newstrong> to add one.<br />
                        <br />
                        <asp:Button ID="btnCreateNew" runat="server" OnClick="btnCreateNew_Click" Text="Create New" />
                    EmptyDataTemplate>
                    <FieldHeaderStyle HorizontalAlign="Left" />
                asp:DetailsView>
        <asp:ObjectDataSource ID="SiteLocDataSource" runat="server"
            SelectMethod="usp_Loc_SelectLoc" TypeName="MainAppDataSetTableAdapters.tfs_SiteLocTableAdapter"
            UpdateMethod="Update" DeleteMethod="Delete">
            <UpdateParameters>
                <asp:Parameter Name="locAreaId" Type="Int64" />
                <asp:Parameter Name="locName" Type="String" />
                <asp:Parameter Name="locId" Type="Int64" />
                <asp:Parameter Name="locMapZone" Type="Int64" />
                <asp:Parameter Name="locEmail" Type="String" />
                <asp:Parameter Name="locManager" Type="String" />
                <asp:Parameter Name="locShortName" Type="String" />
                <asp:Parameter Name="locVamosId" Type="Int64" />
                <asp:Parameter Name="locNotifySiteAdminSupp" Type="Int16" />
            UpdateParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="gvSiteLocs" Name="locId" PropertyName="SelectedValue"
                    Type="Int32" />
            SelectParameters>
            <DeleteParameters>
                <asp:Parameter Name="locId" Type="Int64" />
            DeleteParameters>
        asp:ObjectDataSource>
                <asp:ObjectDataSource ID="AreaDataSource" runat="server" DeleteMethod="Delete" InsertMethod="Insert" SelectMethod="GetData" TypeName="MainAppDataSetTableAdapters.tfs_AreaManagerTableAdapter"
                    UpdateMethod="Update">
                    <DeleteParameters>
                        <asp:Parameter Name="Original_armId" Type="Int64" />
                    DeleteParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="armUserId" Type="Object" />
                        <asp:Parameter Name="armAreaName" Type="String" />
                        <asp:Parameter Name="armRgmId" Type="Int64" />
                        <asp:Parameter Name="armFName" Type="String" />
                        <asp:Parameter Name="armSName" Type="String" />
                        <asp:Parameter Name="armEmail" Type="String" />
                        <asp:Parameter Name="armPhone" Type="String" />
                    UpdateParameters>
                    <InsertParameters>
                        <asp:Parameter Name="armUserId" Type="Object" />
                        <asp:Parameter Name="armAreaName" Type="String" />
                        <asp:Parameter Name="armRgmId" Type="Int64" />
                        <asp:Parameter Name="armFName" Type="String" />
                        <asp:Parameter Name="armSName" Type="String" />
                        <asp:Parameter Name="armEmail" Type="String" />
                        <asp:Parameter Name="armPhone" Type="String" />
                    InsertParameters>
                asp:ObjectDataSource>
                <asp:ObjectDataSource ID="MapZonesDataSource" runat="server" InsertMethod="Insert" SelectMethod="GetData" TypeName="MainAppDataSetTableAdapters.tfs_MapZonesTableAdapter">
                    <InsertParameters>
                        <asp:Parameter Name="mpzZoneName" Type="String" />
                        <asp:Parameter Name="mpzImgUrl" Type="String" />
                        <asp:Parameter Name="mpzNavUrl" Type="String" />
                        <asp:Parameter Name="mpzStatus" Type="Int16" />
                    InsertParameters>
                asp:ObjectDataSource>
            asp:View>
            <asp:View ID="vwError" runat="server">
                 <div class="warningbox">
                    <h1>
                        Could not carry out this operation!h1>
                    <p>
                        Unfortunately, this operation failed.<br />
                        <br />
                        Perhaps you are attempting to delete an existing location that is already in use
                        within a job role.p>
                    <div class="btns">
                        <asp:ImageButton ID="imbOk" runat="server" ImageUrl="~/App_Themes/247Theme/images/rd_btns/sm2_ok.gif" />
                    div>
                div>
            asp:View>
            <asp:View ID="vwNoData" runat="server">
                 <div class="warningbox">
                    <h1>
                        No Areas Set-uph1>
                    <p>
                        To add a new site location you must first create at least one new area manager for
                        the site location to belong to.p>
                    <div class="btns">
                        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/App_Themes/247Theme/images/rd_btns/sm2_ok.gif"
                            PostBackUrl="~/WelcomeRedirect.aspx" />
                    div>
                div>
            asp:View>
        asp:MultiView>
  
0
swaino
10/8/2007 2:42:29 PM

I did a simple test. I bound a smallint field to a checkbox in a DetailsView and the update worked fine. The conversion was done automatically by the framework. I did notice from looking at your code this though: on your SiteLocDataSource object you show the UpdateMethod as:

UpdateMethod="Update"

Shouldn't this attribute contain the name of your stored procedure, similar to your SelectMethod which is:

SelectMethod="usp_Loc_SelectLoc"

-=JW=-
0
jamezw
10/8/2007 3:10:02 PM

The basic 4 commands; select, update, insert & delete are auto-generated by the table adapter in studio and also auto-generated into sprocs.  On the table adapter,  these commands including 'update' resolve to the stored procedures (e.g. Update --> usp_Loc_SelectLoc) and don't explicitly appear as separate functions on the table adapter.  If I add a specific stored procedure not included in those 4, then I would use the sproc name on the adapter.

Anyway, back to the point. I've realised what the problem was.  On the table adapter, I manually added each column and I missed specifying the source for each new column on the table adapter (including the on/off checkbox column).  Even though the datatype was set correctly it didn't like the fact it wasn't mapped to a field in the table.  Strange that it flagged that it couldn't convert it to an integer if it didn't know which column it came from, although maybe this was because the data type had been specified.

thanks for your help. 

 


 


0
swaino
10/8/2007 3:58:14 PM