0

I'm fetching records from sql database and storing in Dictionary using class properties. The issue is that the last row fetched changes or duplicates all the previous values in dictionary. I've used list before but same thing is happening. Below is my code.

public Dictionary<string,Models.Attendance> NumberofDaysPresent(string userid, int month, int year, System.DateTime firstday, System.DateTime lastday)
{
    firstday.ToString("yyyy/MM/dd").Trim().Replace("-", "/");
    lastday.ToString("yyyy/MM/dd").Trim().Replace("-", "/");
    SqlConnection conn;
    conn = Database.getInstance();
    conn.Open();

    Dictionary<string,Models.Attendance> lst = new Dictionary<string,Models.Attendance>();
    Models.Attendance att = new Models.Attendance();



    string query = "Select * from dbo.[Attendence] where UserId = @uid " +
        "AND CONVERT(VARCHAR(11),Date,111) >= @datefrom "+
        "AND CONVERT(VARCHAR(11),Date,111) <= @dateto Order by Date ASC";
    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.Parameters.AddWithValue("@uid", userid);
    //cmd.Parameters.AddWithValue("@month", month);
    //cmd.Parameters.AddWithValue("@year", year);
    cmd.Parameters.AddWithValue("@datefrom", firstday);
    cmd.Parameters.AddWithValue("@dateto", lastday);
    reader = cmd.ExecuteReader();

    while(reader.Read())
    {
        //if(reader["Status"].ToString() == "Full Day" || reader["Status"].ToString() == "Half Day" || reader["Status"].ToString() == "Late")
        //{
            att.strId = Guid.NewGuid().ToString();
        att.cEmployee = new User();
        att.cEmployee.StrUserID = reader["UserId"].ToString();
            att.strStatus = reader["Status"].ToString();
            att.tCheckIn = (TimeSpan)reader["CheckIn"];
            att.tCheckOut = (TimeSpan)reader["Checkout"];
            att.dtAttendanceDate = Convert.ToDateTime(reader["Date"]);
            lst.Add(att.strId,att);
      //  }
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Salar Muhammad
  • 334
  • 1
  • 8
  • 21
  • why are you storing data in a Dictionary ? just curious.. why not get the data and store it in a DataTable.. – MethodMan Nov 28 '17 at 06:18
  • This is because I have the records of Attendance that contains multiple status like Full Day, Half Day and Absent. So i need to extract these days individually to filter records further. – Salar Muhammad Nov 28 '17 at 06:20

1 Answers1

1

Try changing the while loop to this

Models.Attendance att;
while(reader.Read())
    {
        //if(reader["Status"].ToString() == "Full Day" || reader["Status"].ToString() == "Half Day" || reader["Status"].ToString() == "Late")
        //{
            att = new Models.Attendance();
            att.strId = Guid.NewGuid().ToString();
        att.cEmployee = new User();
        att.cEmployee.StrUserID = reader["UserId"].ToString();
            att.strStatus = reader["Status"].ToString();
            att.tCheckIn = (TimeSpan)reader["CheckIn"];
            att.tCheckOut = (TimeSpan)reader["Checkout"];
            att.dtAttendanceDate = Convert.ToDateTime(reader["Date"]);
            lst.Add(att.strId,att);
      //  }
    }

at the top of the while loop. Maybe by instantiating a new model each time, you won't have this issue.

rickjerrity
  • 804
  • 1
  • 9
  • 15