问题
I am trying to get data between two dates in a report viewer control in windows forms. Filtering report data, so I modified the dataset using parameters to
select SN,invoice_date,product_code........ where invoice_date >= @date1 and invoice_date <= @date2
I tried this also
select SN,invoice_date,product_code........ where invoice_date between @date1 and @date2
But the query is not returing any data, the datatype I used in SQL Server is date, and I changed the properties of the dataset @date1 and @date2 into date. So the problem is the datetimepicker am using is not working as it is adding time to the value. And I tried to validate the datetimepicker into something like
datetimepicker1.value.date.toshortdatestring() even tried datetimepicker1.value.tostring()
But the problem is, it will generate an error that system.datetime cannot be convert into string. I even change the datatype of the @date1 and @date2 into varchar in the dataset properties but still not loading.
So this is the code tried
this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value, ProductTo.Value, txtproductcode.Text);
this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value.Date, ProductTo.Value.Date, txtproductcode.Text);
What am I missing? I am using C# with SQL Server.
回答1:
Assuming that the signature of Sales_InvoiceTableAdapter.FillByget
is FillByget(string invoice, DateTime from, DateTime to, string productCode)
, then you should not need to convert to a string and you should check you're setting the time portion appropriately.
this.Sales_InvoiceTableAdapter.FillByget(this.ProductREP.Sales_Invoice, ProductFrom.Value.Date, ProductTo.Value.Date.AddDays(1), txtproductcode.Text);
Given a ProductFrom.Value
and ProductTo.Value
equal to 2020-04-28T10:55:00
, the above code will result in ProductFrom.Value = 2020-04-28T00:00:00
and ProductTo.Value = 2020-04-29T00:00:00
.
If you want to only get values on that specific day (excluding midnight), then subtract 1 second from your final to
value. e.g. ProductTo.Value.Date.AddDays(1).Subtract(TimeSpan.FromSeconds(1))
.
来源:https://stackoverflow.com/questions/61466722/way-to-load-data-between-dates-in-report-viewer-in-c-sharp