【问题标题】:Format exception thrown抛出格式异常
【发布时间】:2014-07-11 03:43:57
【问题描述】:

我有根据我在文本框中提供的日期范围加载的 gridview。它工作正常,但是当我输入数据库中不存在记录的数据范围时,它会引发格式异常错误。它在我将复选框绑定到数据库列的行上引发错误。

代码:

<asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="4" runat="server" AutoGenerateColumns="False" CssClass="mGrid" BorderColor="#333333"    Width="550px" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" >
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="ID" /> 
        <asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />    
        <%-- <asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" /> --%>
        <asp:BoundField HeaderText="FromMobileNo" DataField="FromMobileNo" /> 
        <asp:BoundField HeaderText="Message" DataField="Message" >     
            <ItemStyle Wrap="True" />
            </asp:BoundField>
        <asp:TemplateField HeaderText="IsComplaint">
            <ItemTemplate>
                <asp:CheckBox ID="ckboxIsComplaint" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' />
            </ItemTemplate>
        </asp:TemplateField>
     </Columns>
</asp:GridView>

SP:

ALTER PROCEDURE [dbo].[SearchSmsComplaintsByDate_SP]
    @DateFrom Datetime = null,
    @DateTo DateTime = null
AS
BEGIN
    Begin Try
        IF(@DateFrom is null AND @DateTo is null)
        Begin
            Set @DateFrom = Convert(date,(Select min(ReceivedMessages.ReceivedDateTime) from ReceivedMessages))
            Set @DateTo = Convert(date,(Select max(ReceivedMessages.ReceivedDateTime) from ReceivedMessages))

            SELECT [ID]
            ,REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 106), ' ','/') as RecievingDate
            ,[FromMobileNo]
            ,[Message]
            ,[IsComplaint]
            FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
            where Convert(date,ReceivedDateTime)>= @DateFrom AND Convert(date,ReceivedDateTime)<= @DateTo
            AND IsComplaint!=1

        End
        Else IF(@DateFrom is not null AND @DateTo is not null)
        Begin
            SELECT [ID]
            ,REPLACE(convert(varchar, ReceivedMessages.ReceivedDateTime, 106), ' ','/') as RecievingDate
            ,[FromMobileNo]
            ,[Message]
            ,[IsComplaint]
            FROM [CmsSMSDb].[dbo].[ReceivedMessages] 
            where Convert(date,ReceivedDateTime)>= @DateFrom AND Convert(date,ReceivedDateTime)<= @DateTo 
            AND IsComplaint!=1
        End
    End Try
    Begin Catch
         Select ERROR_MESSAGE() as ErrorMsg
    End Catch
END

【问题讨论】:

    标签: asp.net c#-4.0 gridview checkbox ado.net


    【解决方案1】:

    您的问题可能源于此行;

    <asp:CheckBox ID="ckboxIsComplaint" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsComplaint").ToString()) %>' />
    

    如果IsComplaint 字段的数据为空/空,则转换为布尔值将失败并出现格式异常。

    您可以将行数据绑定事件添加到您的网格视图,以便更好地控制数据转换。

    <asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="4"
        runat="server" AutoGenerateColumns="False" CssClass="mGrid"
        BorderColor="#333333" Width="550px"
        OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging"
        OnRowDataBound="GridViewSmsComplaints_RowDataBound" >
    

    ...并在您的代码隐藏文件中包含类似于以下内容的内容

    protected void GridViewSmsComplaints_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            CheckBox ckboxIsComplaint = e.Row.FindControl("ckboxIsComplaint") as CheckBox;
    
            if (ckboxIsComplaint != null) {
                bool isComplaint;
                string dataItem = DataBinder.Eval(e.Row.DataItem, "IsComplaint") as string;
    
                if (!String.IsNullOrEmpty(dataItem)) {
                    if (Boolean.TryParse(dataItem, out isComplaint)) {
                        ckboxIsComplaint.Checked = isComplaint;
                    }
                }
            }
        }
    }
    

    然后,您可以从复选框组件中删除内联数据绑定(因为这现在在代码隐藏中处理)。所以它就变成了一个普通的复选框控件;

    <asp:CheckBox ID="ckboxIsComplaint" runat="server" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2017-03-14
      • 1970-01-01
      相关资源
      最近更新 更多