1

I have a problem. I have a table named "RollNoSlip" which has a column named "AttendanceStatus". I have list variable named "getAttendance" of type "List<string>". I want to assign each values to each record of "RollNoSLip". Attendance column can contain "P" or "A". Problem is its showing only P in all records. Why?

var getAttendance = (from r in db.RollNoSlips
                                 from a in db.Attendances
                                 where r.RollNo == a.RollNo
                                 select a.AttendanceStatus).ToList();
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["IQTSConnectionString"].ConnectionString))
            {
                con.Open();
                using (var cmd = new SqlCommand("update RollNoSlip set AttendanceStatus=@Attendance where RollNo between  10001 and 10045", con))
                {
                    cmd.Parameters.Add("@Attendance", SqlDbType.VarChar);
                    for (int i = 0; i < getAttendance.Count; i++)
                    {
                        cmd.Parameters["@Attendance"].Value = getAttendance[i];
                        cmd.ExecuteNonQuery();                                                                           
                    }
                }
            }
2
  • You are updating all the records (where rollno between 10001 and 10045) each execution. So they will all have the same AttendanceStatus as the final iteration. Commented Mar 6, 2018 at 14:16
  • the for loop has no meaning in your code. You just keep overwriting the same parameter and keep updating the same records between 10001 and 10045. So in the end they will all have the same value as the last value from your list variable Commented Mar 6, 2018 at 14:17

2 Answers 2

2

The reason you get all 'P's is that the last record in getAttendance list happens to have a 'P'. Had it been an 'A', all your records would be set to an 'A'.

You need to process items separately, passing their IDs to SQL:

// Include RollNo in getAttendance list
var getAttendance = (from r in db.RollNoSlips
                     from a in db.Attendances
                     where r.RollNo == a.RollNo
                     select new {a.AttendanceStatus, a.RollNo}
).ToList();
...
// Updates getAttendance rows in the database, one row at a time
using (var cmd = new SqlCommand("update RollNoSlip set AttendanceStatus=@Attendance where RollNo=@RollNo", con))
{
    cmd.Parameters.Add("@Attendance", SqlDbType.VarChar);
    cmd.Parameters.Add("@Attendance", SqlDbType.Int);
    for (int i = 0; i < getAttendance.Count; i++)
    {
        cmd.Parameters["@Attendance"].Value = getAttendance[i].AttendanceStatus;
        cmd.Parameters["@RollNo"].Value = getAttendance[i].RollNo;
        cmd.ExecuteNonQuery();                                                                           
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is asuming there is a RollNo column, and there are as much records in the table as there are in getAttendance...
@GuidoG a.RollNo is definitely there, because OP shows it. The only assumption here is that his RollNos match the hard-coded values from the update query.
0

Assume you use SQL Server, then use the query below to update AttendanceStatus.

UPDATE RollNoSlip SET
  AttendanceStatus = A.AttendanceStatus
FROM RollNoSlip R
INNER JOIN Attendance A ON R.RollNo = A.RollNo
WHERE R.RollNo BETWEEN 10001 AND 10045

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.