错误将varchar数据类型转换为日期时间数据类型会导致超出范围的值

时间:2013-11-25 07:52:31

标签: c# sql sql-server datetime

是的,当我在文本框中插入数据时,我遇到了问题。问题是当我正在搜索数据10/10/2010时,它工作得很好但是当我尝试搜索其他日期时(例如25 / 11/2013),我会收到错误将varchar数据类型转换为日期时间数据类型会导致超出范围的值。如果我将日期更改为11/22/2013 ,我会得到错误,字符串未被识别为有效的DateTime。我的代码有问题吗?我会把错误发生在下面。

protected void searchdata_Click(object sender, EventArgs e)
{
    Label1.Text = locID.ToString();
    adddate();
    byday2();

}

public void adddate()
{
    today = DateTime.Parse(txtStartDate.Text);//String was not recognized as a valid DateTime
    DateTime answer = today.AddDays(1);
    adddate2 = answer;
}
public void byday2()
{
    Label9.Text = adddate2.ToShortDateString();
    Label10.Text = txtStartDate.Text;
    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\\cuba\\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 top 3 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + today + "') AND ([date] < '" + adddate2 + "')) ORDER BY [data] DESC, [date] DESC";

            // Bind SQLDataSource to GridView after retrieving the records.
            gridmaxdata.DataSource = sqlDataSource;
            gridmaxdata.DataBind(); //The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
        }
    }
}

0 个答案:

没有答案