Without seeing your inputs, I would suggest you use parameterized SQL in your C# or create a stored procedure to accept DATETIME
parameters. You shouldn't use hardcoded SQL queries - in short: they are prone to attack and not optmized in SQL.
A really easy way to do this would be to use the Dapper.NET object mapper.
In SQL, you could do:
CREATE PROCEDURE return_led_for_dates
@startdate DATETIME,
@enddate DATETIME
AS
BEGIN
SELECT
[LedId],
[LedName]
FROM
[Ledger]
WHERE
Date BETWEEN @Startdate AND @Enddate
END
And with Dapper, your C# could then be:
DateTime startdate = Convert.ToDateTime(metroLabel8.Text);
DateTime enddate = Convert.ToDateTime(metroLabel9.Text);
var LED = this.Connection.Query<LED>(
"return_led_for_dates",
new {
StartDate = startdate,
EndDate = enddate
},
commandType: CommandType.StoredProcedure);
You would need an LED class too:
public class LED
{
int LedId {get; set;},
string LedName {get; set;}
}
Finally, this assumes that there is no issue with your text field conversions, you should use DateTime.TryParse
. You can then bullet proof your code, and ensure the field is corretly parsed. Like:
DateTime startDate;
DateTime endDate;
if (DateTime.TryParse(Convert.ToDateTime(metroLabel8.Text), out startDate) && DateTime.TryParse(Convert.ToDateTime(metroLabel9.Text), out endDate))
{
// Your data code.
}