I have this block of code in one form of mine,
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConString))
{
query = "select SUM(TotalMinutes) as TotalMin, SUM(OTMinutes) as OTMin from EmpLog where EmpID = @EmpID and LogDate >= @empdate1 and LogDate <= @empdate2";
conn.Open();
using (SqlCommand cmd2 = new SqlCommand(query, conn))
{
cmd2.Parameters.AddWithValue("@empdate1", dtp_from.Value.ToString("yyyy-MM-dd"));
cmd2.Parameters.AddWithValue("@empdate2", dtp_to.Value.ToString("yyyy-MM-dd"));
cmd2.Parameters.Add(new SqlParameter() { ParameterName = "@EmpID", Value = listBox1.SelectedValue.ToString() });
var da = new SqlDataAdapter(cmd2);
DataTable dt = new DataTable();
da.Fill(dt);
decimal decminutes = Convert.ToDecimal(dt.Rows[0]["TotalMin]"].ToString());
decimal decOTminutes = Convert.ToDecimal(dt.Rows[0]["OTMin"].ToString());
I have made a similar code work on different forms but somehow this won't work. I've tried SqlDataReader but it won't even go inside a while reader.read loop.
also upon trying out the query via ssms, it works as expected. but if i try other methods such as the sqldatareader, it goes into null exception.
EDIT: I've tried removing the WHERE clause from my query, and i'm able to return the rows.
for some odd reason, the datetimepicker values isn't properly being matched with the values in the database. even if when I casted it to string, it exactly matches, but then again, its still returning dbnull.