生成gridview时出错

时间:2013-12-02 04:13:46

标签: c# asp.net sql-server

是的,当我在文本框中插入数据时,我遇到了问题。问题是当我正在搜索数据10/10/2010时,它工作得很好但是当我尝试搜索其他日期时(例如25 / 11/2013),我会得到错误将varchar数据类型转换为日期时间数据类型会导致超出范围的值。错误发生在“gridmaxdata.DataBind();”

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();
        }
    }
}

2 个答案:

答案 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/