部署时日期时间格式错误

时间:2016-01-10 21:19:47

标签: c# sql-server

我有一个程序让我计算重复记录,并通过SQL数据源将其放入文本框“SQL数据源从另一个gridview中选择记录填充它的参数 一切正常,在visual studio上没有错误但是,当我部署它时,它给了我以下错误:

  

“字符串未被识别为有效的DateTime。”

SP

USE [ZainJTA]
GO
/****** Object:  StoredProcedure [dbo].[SP_Dupplicated_Ticket]    Script Date: 01/10/2016 23:08:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_Dupplicated_Ticket]
(
       @Justification_Cisco int
      ,@Justification_Date_Of_Case datetime
      ,@Justification_Justification varchar(60))

AS
BEGIN

    SET NOCOUNT ON;


SELECT [Justification_Cisco] AS Cisco
, [Justification_Date_Of_Case] AS DateOfCase
, [Justification_Justification] AS Justification
, Count([Justification_Cisco]) AS NumberOfDups

FROM dbo.TBL_Justification
GROUP BY 
 Justification_Cisco
, [Justification_Date_Of_Case]
, Justification_Justification
HAVING ([Justification_Cisco]=@Justification_Cisco
AND [Justification_Date_Of_Case]=@Justification_Date_Of_Case
AND [Justification_Justification]=@Justification_Justification
AND Count([Justification_Cisco])>1
AND Count([Justification_Justification])>1)

END

SQL数据

        <asp:SqlDataSource ID="SqlDataSource_Duplicated" runat="server" ConnectionString="<%$ ConnectionStrings:ZainJTAConnectionString_96 %>" SelectCommand="SP_Dupplicated_Ticket" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:SessionParameter DefaultValue="0000" Name="Justification_Cisco" SessionField="SelectedGridCisco" Type="Int32" />
                <asp:SessionParameter DefaultValue="2009-07-22" Name="Justification_Date_Of_Case" SessionField="SelectedGridDateOfCase" Type="DateTime" />
                <asp:SessionParameter DefaultValue="0000" Name="Justification_Justification" SessionField="SelectedGridJustification" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

分配参数

 protected void ValidatorGridView_SelectedIndexChanged(object sender, EventArgs e)
 {
     Session["SelectedGridID"] = ValidatorGridView.SelectedRow.Cells[1].Text.Trim();
     Session["SelectedGridCisco"] = ValidatorGridView.SelectedRow.Cells[2].Text.Trim();
     Session["SelectedGridDateOfCase"] = ValidatorGridView.SelectedRow.Cells[4].Text.Trim();
     Session["SelectedGridJustification"] = ValidatorGridView.SelectedRow.Cells[3].Text.Trim();
     Response.Redirect("Validator_Details.aspx");

 }

通过SQL数据源获取重复计数

Int32 NumOfDup;
                DataView dvSql = (DataView)SqlDataSource_Duplicated.Select(DataSourceSelectArguments.Empty);
                foreach (DataRowView drvSql in dvSql)
                {
                    NumOfDup=System.Convert.ToInt32(drvSql["NumberOfDups"].ToString());
                    if (NumOfDup >= 2)
                    {
                        Label_Dupplicate.Text = "There are " + NumOfDup.ToString() + " Dupplicated Tickets";
                    }
                    else
                    {
                        Label_Dupplicate.Text = "";
                    }

                }

还有其他简单方法或我该如何摆脱日期时间错误

1 个答案:

答案 0 :(得分:0)

我认为,错误可能发生在这里:

Session["SelectedGridDateOfCase"] = ValidatorGridView.SelectedRow.Cells[4].Text.Trim();

如果有人输入网格单元格的文字不能解析为日期(或为空),那么您将收到转换问题...

我建议尝试类似

的内容
var d DateTime;
if(DateTime.TryParse(ValidatorGridView.SelectedRow.Cells[4].Text.Trim(), out d))
    Session["SelectedGridDateOfCase"] = d;
else
    //assign default or handle error (user message, what ever...)