Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,I have a project in winforms where i need to make attendance for students.I have a textbox that takes the value inserted and based on that it inserts data into the table.I have 2 flags that takes evidence of the attendance,mainly i have one for arrival and one for departure.When the student inserts data,the first flag(Attending) will set true,meaning the checkbox will tick.My problem is that when it inserts data the second time,the table won't update with Departed flag,meaning that it will tick,but it will create the same row as before.I don't understand why is that happening.If i leave the Attending flag 0 on inserting data,the departed one works.This is my AttendanceList:
CREATE TABLE [dbo].[AttendanceList](
	[sNr] [int] IDENTITY(1,1) NOT NULL,
	[SN] [char](10) NOT NULL,
	[fName] [nvarchar](max) NOT NULL,
	[lName] [nvarchar](max) NOT NULL,
	[dateArrival] [datetime] NOT NULL,
	[dateDeparture] [datetime] NULL,
	[Attending] [bit] NULL,
	[CourseID] [nvarchar](50) NULL,
	[Departed] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
	[sNr] ASC

And this is the method for saving and updating the table in datagridview:
private void btnSave_Click(object sender, EventArgs e)
     {
         var result = checkCourse();
         if(result==true)
         {
             using (var cn = new SqlConnection(connstr))
             {
                 SqlCommand checkData = new SqlCommand("SELECT COUNT(dateArrival) FROM AttendanceList WHERE SN = @Id and Attending=0", cn);
                 checkData.Parameters.AddWithValue("@Id", txtStudentId.Text);
                 cn.Open();
                 int Exist = (int)checkData.ExecuteScalar();
                 cn.Close();
                 if (Exist % 2 == 0)
                 {
                     try
                     {
                         using (var cnn = new SqlConnection(connstr))
                         {

                             var query =
                                 "Insert into AttendanceList(SN,fName,lName,dateArrival,Attending,Departed,CourseId)Values(@SN,@fName,@lName,@dateArrival,@Attending,@Departed,@ClassId)";
                             using (var cmd = new SqlCommand(query, cnn))
                             {



                                 try
                                 {
                                     string Studentquery = "select  fName,lName from RegisterStudent  WHERE (SN = @SN)";
                                     using (var Student = new SqlCommand(Studentquery, cnn))
                                     {


                                         Student.Parameters.AddWithValue("@SN", txtStudentId.Text);
                                         cnn.Open();
                                         cn.Open();
                                         dr = Student.ExecuteReader();
                                         if (dr.HasRows == true)
                                         {

                                             while (dr.Read())
                                             {
                                                 if (dr.HasRows == true)
                                                 {

                                                     cmd.Parameters.AddWithValue("@fName", dr["fName"].ToString());
                                                     cmd.Parameters.AddWithValue("@lName", dr["lName"].ToString());

                                                 }

                                             }

                                         }


                                     }
                                 }
                                 catch (Exception ex)
                                 {
                                     // write exception info to log or anything else
                                     MessageBox.Show(ex.Message);
                                     cmd.Parameters.AddWithValue("@fName", "");
                                     cmd.Parameters.AddWithValue("@lName", "");

                                 }
                                 cmd.Parameters.AddWithValue("@SN", txtStudentId.Text);
                                 cmd.Parameters.AddWithValue("@dateArrival", dtArrival.Text);
                                 cmd.Parameters.AddWithValue("@ClassId", cmbClassId.SelectedValue.ToString());
                                 cmd.Parameters.AddWithValue("@Departed", 0);
                                 cmd.Parameters.AddWithValue("@Attending", 1); //here i set the attending flag to 1 when student registers into the attedancelist
                                 dr.Close();
                                 cmd.ExecuteNonQuery();
                                 cn.Close();
                                 cnn.Close();
                                 dg.Update();
                                 dg.Refresh();

                                 LoadData();
                                 Clr();
                             }
                         }
                     }
                     catch (Exception ex)
                     {
                         // write exception info to log or anything else
                         MessageBox.Show(ex.Message);

                     }
                 }

                 else
                 {
                     using (var cnn = new SqlConnection(connstr))
                     {

                         checkData = new SqlCommand(
                             "update AttendanceList set Departed=@Departed, dateDeparture=@dateDeparture where sNr=(SELECT MAX (sNr) FROM AttendanceList) and SN =@Id", cn);
                         cn.Open();

                         checkData.Parameters.AddWithValue("@Id", txtStudentId.Text);
                         checkData.Parameters.AddWithValue("@dateDeparture", dtArrival.Text);
                         checkData.Parameters.AddWithValue("@Departed", 1);//here i set it when the student inserts his data the second time and the departed flag is set to 1.
                         checkData.ExecuteNonQuery();
                         cn.Close();
                         Clr();
                         LoadData();

                         Clr();



                     }
                 }

             }
         }


What I have tried:

I have tried to set the Attending flag to 0 ,and then it works for some reason,but when the student first inserts data,it won't set to 1(when the student inserts the first time his data,it will set the Attending to 1 and when he inserts the data the second time-which is the same data,basically a serial nr,it will update the Departed flag) but when i insert data the second time,the departed flag will be on.
Posted
Updated 15-Jun-18 0:39am

1 solution

I don't understand the logic using a COUNT query on the dateArrival field and creating a new dataset when that returns an even value. That would also return zero when the WHERE clause matches but the dateArrival field is NULL.

You should also catch errors for your update command.

I guess your problem is sourced by your UPDATE command. What is the purpose of the
SQL
sNr=(SELECT MAX (sNr) FROM AttendanceList
WHERE clause?
That would only match the recently added record.

If the data has been updated can be simply checked with the return value of the checkData.ExecuteNonQuery() call which I expect to be zero.

Why not let the initial query return the sNR and optionally some other fields looking only for a matching SN?
If there is no match, create a new record. Otherwise use the other fields to decide if the data should be updated which can be then done with the unique sNR.
 
Share this answer
 
Comments
Eliza Maria 15-Jun-18 6:48am    
Thank you for your response.I have am RFID card reader,so every time the student swipes the NFC tag on the card reader,it stores the data into the table based on the serial nr on the card reader.When it identifies the same value the second time,it will automatically update,checking the flag Departed.As you told me it is much better.Thank you sir!
Jochen Arndt 15-Jun-18 6:56am    
So the SN is unique?
Then there would be no need to use the sNR and the SN can be the primary key.
Eliza Maria 15-Jun-18 7:41am    
Yes,the SN is unique.Ok,I will try to do it this way.Thank you and I will let you know what i have done!Best regards!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900