我有一个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> <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> <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中SelectCommand中的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列以字符串形式返回,而不是布尔值(无论如何,这是编译器认为的)。如何修复UNION以便继续将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]
非常感谢!
答案 0 :(得分:1)