抛出格式异常

时间:2014-05-22 08:06:20

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

我有gridview,它是根据我在文本框中提供的日期范围加载的。它可以处理罚款,但是当我输入数据库中没有记录的数据范围时,它会抛出FORMAT EXCEPTION ERROR。它在我已将Checkbox绑定到数据库列的行上引发错误。

代码:

<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

1 个答案:

答案 0 :(得分:2)

你的问题很可能来自这条线;

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

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

您可以向gridview添加行数据绑定事件,以便更好地控制数据转换。

<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" />