我在 12 点之后的日期之后检索日期时遇到问题。例如:如果我从日历扩展器中单击:2/7/2013 到 19/july/2013,会抛出这个错误:DateTime日历 System.Globalization.GregorianCalendar 不支持由字符串表示的。
这是我的代码。
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();
}