代码之家  ›  专栏  ›  技术社区  ›  Katerine459

使用带有布尔值的联合查询并将其设置为复选框asp时出错。网

  •  0
  • Katerine459  · 技术社区  · 8 年前

    我有一个ASP。NET页面,该页面具有带复选框控件的gridview,该控件绑定到查询(简化版本如下):

    <asp:SqlDataSource ID="gvPhonesDataSource" runat="server" OnInserted="gvPhonesDataSource_Inserted"
        ConnectionString="<%$ ConnectionStrings:ConnString %>" 
        SelectCommand="SELECT p.[CustomerPhoneID]
                    ,p.[IsActive]
                FROM [dbo].[CustomerPhones] p
                WHERE p.CustomerContactID = @CustomerContactID
                UNION SELECT -1 AS CustomerPhoneID, 0 AS [IsActive]"
        <SelectParameters>
            <asp:ControlParameter Name="CustomerContactID" Type="Int32" ControlID="contactid" PropertyName="Text" />
        </SelectParameters>
    </asp:SqlDataSource>
    
    <asp:gridview ID="gvPhones" runat="server" DataSourceID="gvPhonesDataSource"
        AutoGenerateColumns="False" DataKeyNames="CustomerPhoneID" 
        CssClass="searchresultsgrid" ShowFooter="True" OnRowCommand="gvPhones_RowCommand" AllowSorting="True"
        OnRowDataBound="gvPhones_RowDataBound">
        <Columns>
            <asp:BoundField DataField="CustomerPhoneID" InsertVisible="false" ReadOnly="true" Visible="False" />
    
            <asp:TemplateField HeaderText="Active?" SortExpression="IsActive">
                <FooterTemplate>
                    <asp:CheckBox runat="server" Checked='<%# Bind("IsActive") %>' ID="chkPhoneIsActive"></asp:CheckBox>
                </FooterTemplate>
                <EditItemTemplate>
                    <asp:CheckBox runat="server" Checked='<%# Bind("IsActive") %>' ID="chkPhoneIsActive"></asp:CheckBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" Text='<%# Bind("IsActive") %>' ID="lblPhoneIsActive"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
    
    
            <asp:TemplateField ShowHeader="False">
                <EditItemTemplate>
                    <asp:LinkButton runat="server" Text="Update" CommandName="Update" CausesValidation="True" ID="PhoneUpdate"></asp:LinkButton>&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" ID="PhoneEditCancel"></asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CausesValidation="False" ID="PhoneEdit"></asp:LinkButton>&nbsp;<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="False" ID="PhoneDelete"></asp:LinkButton>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:LinkButton runat="server" Text="Save New Phone" CommandName="FooterInsert" CausesValidation="True" ID="PhoneInsert"></asp:LinkButton>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:gridview>
    

    一切正常,SqlDataSource中的SELECT命令中没有UNION SELECT。但是,当我输入UNION SELECT时,gridview的IsActive列中的值不再列为“是”或“否”,而是开始列为“1”或“0”,当我尝试进入编辑(尝试将IsActive绑定到复选框)时,我得到以下错误:

    Exception Details: System.InvalidCastException: Specified cast is not valid.
    
    Source Error: 
    
    Line 143:                            </FooterTemplate>
    Line 144:                            <EditItemTemplate>
    Line 145:                                <asp:CheckBox runat="server" Checked='<%# Bind("IsActive") %>' ID="chkPhoneIsActive"></asp:CheckBox>
    Line 146:                            </EditItemTemplate>
    Line 147:                            <ItemTemplate>
    
    Source File: ...\ContactEdit.aspx    Line: 145 
    

    我怀疑问题在查询中。。。UNION导致绑定的IsActive列以字符串而不是布尔值的形式返回(无论如何,编译器都是这么认为的)。如何修复并集,使IsActive继续被视为布尔值?

    仅供参考,表(SQL Server 2014)的相关部分如下:

    CREATE TABLE [dbo].[CustomerPhones](
        [CustomerPhoneID] [int] IDENTITY(1,1) NOT NULL,
        [IsActive] [bit] NOT NULL CONSTRAINT [DF_CustomerPhones_IsActive]  DEFAULT ((1)),
     CONSTRAINT [PK_CustomerPhones] PRIMARY KEY CLUSTERED 
    (
        [CustomerPhoneID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    

    非常感谢!

    1 回复  |  直到 8 年前
        1
  •  1
  •   mjwills Myles McDonnell    8 年前

    你的

    0 AS [IsActive]
    

    表示“0为整数”,而不是“0为位”。

    您可以使用 CAST :

    CAST(0 AS BIT) AS [IsActive]