[英]C# Conversion failed when converting date and/or time from character string
I have googled for the last few hours and none of those questions can be apply to my case, so hope you guys can help. 我已经搜索了过去几个小时,但这些问题都无法解决我的问题,因此希望你们能提供帮助。 I have a report application which user can pick a date from the DateTimePicker and the item which has the largest quantity value would be displayed. 我有一个报表应用程序,用户可以从DateTimePicker中选择一个日期,并显示数量最大的项目。 I use the report viewer control to display the information. 我使用报表查看器控件来显示信息。
Here is the SQL query in my table adapter: 这是我的表适配器中的SQL查询:
SELECT TOP (1)
Drink_Name,
SUM(Quantity) AS NoDrink,
Bill_No,
Staff_No,
Bill_Date,
Bill_Value,
Customer_No,
Unit_Price,
Quantity
FROM
(SELECT
Bill.Bill_No,
Bill.Staff_No,
Bill.Bill_Date,
Bill.Bill_Value,
Bill.Customer_No,
Bill_Detail.Drink_Name,
Bill_Detail.Bill_No AS Expr1,
Bill_Detail.Unit_Price,
Bill_Detail.Quantity
FROM
Bill INNER JOIN
Bill_Detail ON Bill.Bill_No = Bill_Detail.Bill_No
WHERE
(Bill.Bill_Date = @Bill_Date)
) AS Temp
GROUP BY Drink_Name,
Bill_No,
Staff_No,
Bill_Date,
Bill_Value,
Customer_No,
Unit_Price,
Quantity
ORDER BY NoDrink DESC
And here is the code for generate and load data into report dataset 这是用于生成数据并将数据加载到报表数据集中的代码
CoffeeShopDataSetTableAdapters.DataTable1TableAdapter adapter =
new CoffeeShopDataSetTableAdapters.DataTable1TableAdapter();
CoffeeShopDataSet.DataTable1DataTable table =
new CoffeeShopDataSet.DataTable1DataTable();
adapter.FillByMax(table, dateTimePicker1.Value.ToShortDateString());
ReportDataSource maxdatasource =
new ReportDataSource("DataSet1form3", (DataTable)table);
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(maxdatasource);
this.reportViewer1.LocalReport.Refresh();
this.reportViewer1.RefreshReport();
The problem is I always get this error: 问题是我总是会收到此错误:
"An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll “ System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理的异常
Additional information: Conversion failed when converting date and/or time from character string." 其他信息:从字符串转换日期和/或时间时转换失败。”
When I try the SQL query with the SQL server management 2008, it works just fine, so i think this is the VS problem. 当我尝试使用SQL Server Management 2008进行SQL查询时,它工作得很好,所以我认为这是VS问题。 Thanks all in advance. 提前谢谢大家。 Sorry for the bad English 对不起英语不好
I would be explicit in expressing your Dates so there is no confusion between Date Time formats. 我会明确表示您的日期,因此在日期时间格式之间不会造成混淆。
1) Change the line converting the date to string with explicit format 1)更改将日期转换为具有显式格式的字符串的行
dateTimePicker1.Value.ToString("yyyyMMdd");
2) Change the SQL (Assuming that Bill.Bill_Date column is a Date 2)更改SQL(假设Bill.Bill_Date列为Date)
(Bill.Bill_Date = CONVERT(DATE, @Bill_Date, 112)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.