2016-05-27 71 views
0

我希望我的按鈕能夠在ReportViewer中搜索特定日期,並在其中顯示數據的特定日期。DateTimePicker作爲ReportViewer的參數

private void button1_Click(object sender, EventArgs e) 
    { 

     this.DataTable1TableAdapter.Fill(this.DataReport.DataTable1 + dateTimePicker2.Value()); 

     this.reportViewer1.RefreshReport(); 
    } 

這是我在DataAdapter中使用的查詢。

SELECT  Customer.CID, Customer.FirstName, Customer.LastName, Booking.VehicleNumber, Customer.MemberType, Customer.Points, Booking.[Time], Booking.[Date] 
FROM  (Booking INNER JOIN 
      Customer ON Booking.CID = Customer.CID) 
WHERE  Booking.[Date] = (dateTimePicker2) 

我上面的代碼,在按鈕下顯示「dateTimePicker2.Value」將不起作用。我試過用它作爲文本,但它仍然顯示錯誤。我應該把它轉換成什麼,或者我應該添加一個字符串?

+0

你已經厭倦了包裝dateTimePicker2.Value引號,所以它代表一個字符串? –

+0

讓我試着引用它。 – Minial

+0

'this.DataTable1TableAdapter.Fill(this.DataReport.DataTable1,「dateTimePicker2」.Value());'這樣的事情? – Minial

回答

0

在日期的DataAdapter中使用參數。這是假設正在使用System.Data.OleDb。

var cmd = new OleDbCommand(@" 
    SELECT Customer.CID, Customer.FirstName, Customer.LastName, Booking.VehicleNumber, Customer.MemberType, Customer.Points, Booking.[Time], Booking.[Date] 
    FROM (Booking INNER JOIN 
      Customer ON Booking.CID = Customer.CID) 
    WHERE Booking.[Date] = @booking_date"); 
cmd.Parameters.Add("@booking_date", OleDbType.DBDate).Value = dateTimePicker2.Value; 
DataTable1TableAdapter.SelectCommand = cmd; 
//{...} 
this.DataTable1TableAdapter.Fill(this.DataReport.DataTable1); 
this.reportViewer1.RefreshReport();