Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a question regarding my datagridview.For my datagridview I'm using a datatable and a dataset.I have 2 sql queries that are relevant for filling the dg.One is "
string query = " select * from RegisterStudent p inner join StudentCourses c on p.SN = c.StudentId where c.CourseId = '" + cmbClassId.SelectedValue.ToString() + "'";
whre cmbClassId is the courseID of the student.This query takes all the students from RegisterStudent table specific to that course and it displays it in dg.Now,this I am doing because i would like to have a list displayed with all the students for that course.My problem comes when I have to insert these values plus others in another table called AttendanceList.I'm doing it like this:"
var query =
                                  "Insert into AttendanceList(SN,sNr,fName,lName,dateArrival,Attending,Departed,CourseId)Values(@SN,@sNr,@fName,@lName,@dateArrival,@Attending,@Departed,@ClassId) ";
For both of them it works perfectly,but my question is:How can I delete the duplicate rows in the dg on inserting the values in AttendanceList?Should i do it through a query or relating the dg?And if it is related to the dg,how can i do it so that it will remove duplicate rows?I mention I have tried all the possible choices for this but without any luck.Thank you in advance!

What I have tried:

How to remove duplicate row from datagridview in c#? - Stack Overflow[^]
https://www.c-sharpcorner.com/forums/remove-duplicates-from-datagridview-c-sharp
Remove duplicated rows in a datagridview[^]
https://www.codeproject.com/Articles/36697/Eliminate-Duplicate-Values-from-the-Grid-View
https://www.aspsnippets.com/Articles/Remove-Delete-Duplicate-Rows-Records-from-DataTable-using-C-and-VBNet.aspx
And I also have tried this:
int  rowIndex = 0;
            if (!this.dg.Rows[rowIndex].IsNewRow)
            {
                this.dg.Rows.RemoveAt(rowIndex);
            }

when I have rowIndex=0 it will delete the row that is at index 0 if it is alike to the one from the select statement.In returen i would like it to delete the one from select.If I changed to rowIndex=dg.Rows.Count it will give me an index out of range exception.
Posted
Updated 24-Jun-18 23:21pm
v2
Comments
Richard Deeming 26-Jun-18 12:18pm    
string query = " select * from RegisterStudent p inner join StudentCourses c on p.SN = c.StudentId where c.CourseId = '" + cmbClassId.SelectedValue.ToString() + "'";


Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

1 solution

do one thing when you binding data to datatable in that same time do or remove duplicate rows following example please check.



Remove duplicate records from a table[^]
 
Share this answer
 
v2
Comments
Eliza Maria 25-Jun-18 4:47am    
Thank you for your response.I have also tried this one.Is not working.I don't need a hash table in my code nor an Arraylist.I have a datatable and a dataset.I am connected to the database thorugh mysql server without any binding of any kind to the datagridview
amit kalshetti 25-Jun-18 5:13am    
us distinct in mysql query like follow

https://stackoverflow.com/questions/2068515/select-distinct-records-on-a-join
Eliza Maria 25-Jun-18 5:21am    
Thank you Amit for your help but i think you don't understand what i want exactly.I have a select statement which takes the values from one table (RegisterStudent) and displays it in a datagridview.The table has SN,sNr,fName and lName.The insert statement for the other table,AttendanceList has also the same values + dateArrival,dateDeparture,Departed,Attending and CourseID.When the insert statement is executed,the row for the select statement that had the same values (SN,sNr,fName,lName) will dissapear once the other record is inserted into the datagridview.I don't need a distinct record.This is only based on the view.
amit kalshetti 25-Jun-18 5:44am    
on insert you want to delete row from datagridview means after you inserted data in gridview you want to delete it or before inserting row you want to check and insert into the gridview ?

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