protected void searchdata_Click(object sender, EventArgs e)
{
data();
byday2();
}
public void data()
{
if (Dayrange.Checked == true)
{
startdate = DateTime.ParseExact(txtStart1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
enddate = DateTime.ParseExact(txtEnd1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
//starttime = DateTime.ParseExact(txtStart1.Text, "HH:mm:ss", CultureInfo.InvariantCulture);
//endtime = DateTime.ParseExact(txtStart1.Text, "HH:mm:ss", CultureInfo.InvariantCulture);
Label1.Text = startdate.ToShortDateString();
Label2.Text = enddate.ToShortDateString();
}
if (Byday.Checked == true)
{
startdate = DateTime.ParseExact(txtStart1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
enddate = startdate.AddDays(1);
//starttime = DateTime.ParseExact(txtStart1.Text, "HH:mm:ss", CultureInfo.InvariantCulture);
//endtime = DateTime.ParseExact(txtStart1.Text, "HH:mm:ss", CultureInfo.InvariantCulture);
Label1.Text = startdate.ToShortDateString();
Label2.Text = enddate.ToShortDateString();
}
}
public void byday2()
{
if (Byday.Checked == true)
{
if (Maxdata.Checked == true)
{
//tablemax.hidden = true;
lblmaxdata.Visible = true;
lblmaxdata.Text = "Highest Data";
// ConnectionString to NorthWind Database.
string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\history\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";
// Create SQLDataSource.
SqlDataSource sqlDataSource = new SqlDataSource();
sqlDataSource.ID = "SqlDataSource123";
this.Page.Controls.Add(sqlDataSource);
// Bind ConnectionString to SQLDataSource.
sqlDataSource.ConnectionString = connectionString;
// Retrieve records with only 5 Columns from Employees table of NorthWind Database.
sqlDataSource.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + Convert.ToDateTime(startdate).ToString("dd/MM/yyyy") + "') AND ([date] < '" + Convert.ToDateTime(enddate).ToString("dd/MM/yyyy") + "')) ORDER BY [data] DESC, [date] DESC";
// Bind SQLDataSource to GridView after retrieving the records.
gridmaxdata.DataSource = sqlDataSource;
gridmaxdata.DataBind();
}
}
}
答案 0 :(得分:2)
最好使用参数
sqlDataSource.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= @startdate) AND ([date] < @enddate)) ORDER BY [data] DESC, [date] DESC";
sqlDataSource.SelectCommand.Parameters.AddwithValue("@startdate",startdate);
sqlDataSource.SelectCommand.Parameters.AddwithValue("@enddate",enddate);
答案 1 :(得分:0)
这是因为在2013年11月25日,25月份获得,11份获得日期,2013年获得年份。因此没有第25个月,那么它将抛出异常。
你可以尝试这种方法convert(varchar, startdate, 103)
了解更多信息http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/