日历System.Globalization.GregorianCalendar中不支持字符串



我在12日之后的日期检索时遇到问题。例如:如果我从calander扩展程序中单击:2013年7月2日至2013年7日19日,is将抛出此错误:日历System.Globalization.GregorianCalendar.中不支持由字符串表示的DateTime

这是我的密码。

    var format = "MM/dd/yyyy";
    DateTime one = DateTime.ParseExact(startdate, format, CultureInfo.InvariantCulture);
    DateTime two = DateTime.ParseExact(enddate, format, CultureInfo.InvariantCulture);

    if (two >= one)
    {
        SqlConnection conn = new SqlConnection("Data Source=""catalog="";Integrated Security=True");
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT Name,CLass, NRIC, StallNo, AmountSpent ,TimeDate=convert(nvarchar,timedate,103)  FROM StudentTransactions WHERE TimeDate BETWEEN '" + one + "' AND '" + two + "'", conn);
        SqlDataReader reader = cmd.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataSourceID = null;
        GridView1.Visible = true;
        GridView1.DataBind(); 
        conn.Close();
   }

尝试这个

select CONVERT(varchar,<datecol>,103)      ---  and will return as dd/mm/yyyy
where convert(date,<datecol>) between '' and ''

如果要从CodeBehind更改GridView的列格式,请将RowDataBound添加到网格视图中。

然后,在GridView_RowDataBound(object sender, GridViewRowEventArgs e)方法中,您将能够访问e,这将使您能够访问该行的各个单元格,您可以在其中指定格式。

参考

引用2

首先确保TimeDate列的日期部分为所需格式,即"dd/mm/yyyy"。

var format = "dd/mm/yyyy";
DateTime one = DateTime.ParseExact(startdate, format, CultureInfo.InvariantCulture);
DateTime two = DateTime.ParseExact(enddate, format, CultureInfo.InvariantCulture);

编辑:要格式化输出,可以执行ff:假设你在GridView1:上的TimeDate上有一个Boundfield

<asp:BoundField DataField="TimeDate" HeaderText="TimeDate" DataFormatString="{0:dd/MM/yyyy}" />

您也可以使用DataFormatString="{0:d}"以当前区域性的短日期格式输出日期。

最新更新