I am using a SqlDataReader and while trying to read the reader I am having rows.
When I check .HasRows, the rows are not available.
List<EmployeeTimings> empTimingsList = new List<EmployeeTimings>();
// Creates a SQL connection
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["WINPAKContext"].ToString()))
{
// Creates a SQL command
using (var command = new SqlCommand("SELECT * FROM SEED_VIEW WHERE empid is not null and DateTime > '" + dtlastpunch + "' order by datetime", connection))
{
connection.Open();
// Loads the query results into the table
var reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
EmployeeTimings empTime = new EmployeeTimings();
empTime.CardNumber = reader["CardNO"].ToString();
empTime.EMPId = reader["EMPID"].ToString();
empTime.FirstName = reader["FirstName"].ToString();
empTime.LastName = reader["LastName"].ToString();
empTime.Location = reader["Location"].ToString();
empTime.Trans_DateTime = Convert.ToDateTime(reader["DateTime"]);
empTimingsList.Add(empTime);
}
reader.Close();
}
command.ExecuteNonQuery();
}
connection.Close();
}
return empTimingsList;
Could you please let me know whats the issue?
What's happening after reader.HasRows() call, the redaer does not have any rows....
command.ExecuteNonQuery();call towards the end of your code?? Totally pointless - you've already executed the SQL statement and read in all the rows returned from it......