Possible Duplicate:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value可能重复:将char数据类型转换为日期时间数据类型会导致日期时间值超出范围
my task is to display all name and birth date. my condition is between basic but at time of i selected date and enter in button i see exception,
my code is
我的任务是显示所有姓名和出生日期。我的条件介于基本但是在我选择的日期和输入按钮时我看到异常,我的代码是
SqlConnection con = new SqlConnection("my connection ");
SqlDataAdapter da = new SqlDataAdapter("select name,date from date where date between'"+ Convert.ToDateTime( datePicker2.SelectedDate)+"' and '"+ Convert.ToDateTime( datePicker3.SelectedDate)+"'",con);
DataSet ds = new DataSet();
da.Fill(ds, "entry");
da.Dispose();
dataGrid1.DataContext = ds.Tables["entry"].DefaultView;
plz help me solve code error and write correct code . this is a wpf appliction
plz帮我解决代码错误并编写正确的代码。这是一个wpf应用程序
2 个解决方案
#1
0
From your above code, the part of SQL Statement is - "select name,b_date from date where date between"
从上面的代码中,SQL语句的部分是 - “选择名称,b_date从日期之间的日期”
Can you please make sure the following:
你能否确认以下内容:
-
Is "date" column there in the "date" table? should it be "b_date" instead of "date" or it is correct?
“日期”列中是否有“日期”列?应该是“b_date”而不是“date”还是正确的?
-
Can you please post what exception/error you are getting?
您能否发布您遇到的异常/错误?
Thanks
#2
0
DateTime should be formatted to be valid in sql, the format can be like yyyyMMdd,that is:
DateTime应格式化为在sql中有效,格式可以像yyyyMMdd,即:
SqlConnection con = new SqlConnection("my connection ");
SqlDataAdapter da = new SqlDataAdapter("select name,date from date where date between'"+ Convert.ToDateTime( datePicker2.SelectedDate.ToString("yyyyMMdd")+"' and '"+ Convert.ToDateTime( datePicker3.SelectedDate.ToString("yyyyMMdd"))+"'",con);
DataSet ds = new DataSet();
da.Fill(ds, "entry");
da.Dispose();
dataGrid1.DataContext = ds.Tables["entry"].DefaultView;
#1
0
From your above code, the part of SQL Statement is - "select name,b_date from date where date between"
从上面的代码中,SQL语句的部分是 - “选择名称,b_date从日期之间的日期”
Can you please make sure the following:
你能否确认以下内容:
-
Is "date" column there in the "date" table? should it be "b_date" instead of "date" or it is correct?
“日期”列中是否有“日期”列?应该是“b_date”而不是“date”还是正确的?
-
Can you please post what exception/error you are getting?
您能否发布您遇到的异常/错误?
Thanks
#2
0
DateTime should be formatted to be valid in sql, the format can be like yyyyMMdd,that is:
DateTime应格式化为在sql中有效,格式可以像yyyyMMdd,即:
SqlConnection con = new SqlConnection("my connection ");
SqlDataAdapter da = new SqlDataAdapter("select name,date from date where date between'"+ Convert.ToDateTime( datePicker2.SelectedDate.ToString("yyyyMMdd")+"' and '"+ Convert.ToDateTime( datePicker3.SelectedDate.ToString("yyyyMMdd"))+"'",con);
DataSet ds = new DataSet();
da.Fill(ds, "entry");
da.Dispose();
dataGrid1.DataContext = ds.Tables["entry"].DefaultView;