How to retrieve Distinct strings from comparing two Arrays?

Jan 22 2018 6:06 AM
I want to retrieve Absent student names from sql database. I have a Listbox in which i have Present student Rfid data (Something like 42 39 A0 11), I have stored the student details along with Rfid in database as nvarchar(1500) datatype.
 
Using the present stud id in list box i want to retrieve absent students name in an array.
 
I used NOT IN condition in where but i was not able to retrieve
 
Then i thought of using for loops to match the get the students who's id was not in the Listbox
 
But still I am losing the values and not getting proper absent students Id.
  1. private void checkabst_Click(object sender, EventArgs e)  
  2. {  
  3. string[] present = new string[3];  
  4. string[] absent = new string[3];  
  5. string[] allstd = new string[3];  
  6. List<string> total = new List<string>();  
  7. using (SqlConnection con = new SqlConnection(cs))  
  8. {  
  9. SqlCommand cmd = new SqlCommand("Select Rfid_Uid From Studetails", con);  
  10. con.Open();  
  11. SqlDataReader sdr = cmd.ExecuteReader();  
  12. while (sdr.Read())  
  13. {  
  14. total.add(sdr[0].ToString());  
  15. }  
  16. allstd=total.toArray();  
  17. for(int i=0;i  
  18. {  
  19. present[i] = listbox_present.Items[i].ToString();  
  20. }  
  21. bool isfound = false;  
  22. for (int i = 0; i < 3; i++)  
  23. {  
  24. isfound = false;  
  25. for (int j = 0; j < present.Length; j++)  
  26. {  
  27. if (allstd[i] == present[j])  
  28. {  
  29. isfound = true;  
  30. }  
  31. }  
  32. if (!isfound)  
  33. {  
  34. MessageBox.Show(allstd[i]);  
  35. }  
  36. }  
Stuck here from past few days.
I am getting Present as well as the absent student Ids in messagebox.

Answers (3)