Results 1 to 10 of 10

Thread: [RESOLVED] Prevent getting duplicate records

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Resolved [RESOLVED] Prevent getting duplicate records

    Hello, I have a database and if I call GetData() I'm getting duplicate records on DataGridView. Here's my code:

    VB.NET Code:
    1. private void GetData()
    2. {
    3.     // Retrieve the data.
    4.  
    5.     sqliteDataAdapter.Fill(dataTable);
    6.  
    7.     // The table can be used here to display and edit the data.
    8.     // That will most likely involve data-binding but that is not a data access issue.
    9. }
    10.  
    11. private void SaveData()
    12. {
    13.     // Save the data.
    14.     sqliteDataAdapter.Update(dataTable);
    15. }

    If I add the code below to all places where I declare datatable (and set id column to unique), I'm getting many errors:

    VB.NET Code:
    1. try
    2. {
    3.     sqliteDataAdapter.Fill(dataTable);
    4. }
    5. catch (ConstraintException)
    6. {
    7.     DataRow[] rowErrors = dataTable.GetErrors();
    8.  
    9.     System.Diagnostics.Debug.WriteLine("YourDataTable Errors:"
    10.         + rowErrors.Length);
    11.  
    12.     for (int i = 0; i < rowErrors.Length; i++)
    13.     {
    14.         System.Diagnostics.Debug.WriteLine(rowErrors[i].RowError);
    15.  
    16.         foreach (DataColumn col in rowErrors[i].GetColumnsInError())
    17.         {
    18.             System.Diagnostics.Debug.WriteLine(col.ColumnName
    19.                 + ":" + rowErrors[i].GetColumnError(col));
    20.         }
    21.     }
    22. }
    Last edited by nikel; Aug 25th, 2017 at 09:33 AM. Reason: I deleted SaveData()

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Prevent getting duplicate records

    The problem isn't the code... the problem is the data... there are different ways to get duplicate data. One is the table in the database itself has the data duplicated. The other is the result of a query.

    The first is easy enough to explain... it means the data is duplicated in the table. If your IDs are set "correctly" then you'd be getting different IDs on the rows... doesn't sound like that's the issue, so I'm going to guess you've got two tables you're selecting data from and you've got a join condition that ties them together. The problem then is that for each row you have in one table, you've got multiple related rows in the second table resulting in "duplicate" data.

    Consider this:
    table: People
    -------
    ID, Name, Address

    Table: Cars
    -------
    ID, Make, Model, Color

    Table: CarsPeopleOwn
    -------
    ID, PeopleID, CarID


    So the People table is full of, well, People... and the Cars is full of Cars and CarsPeopleOwn links the two... now people can own more than one... I own two... so I have one record in the People table, two records in the CarsPeopleOwn table... and since the cars I own are different makes and models, there's multiple rows in there...

    If I'm the only one in the table and I select from P:eople, I get one record... but as soon as I join to the CarsPeopleOwn table, I get two records... resulting in "duplicate" data.

    Hopefully all this makes sense.

    Long story short, trying to fix it in the code is too late...the horse has already left the barn. What you need to be looking at is the data and the SQL that created the datatable you're trying to fill... the solution lies there.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Prevent getting duplicate records

    thank you for your reply.
    I'm not a man of too many faces
    The mask I wear is one

  4. #4
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Prevent getting duplicate records

    Hopefully all this makes sense.
    it makes sense...in a way
    but only if that design is implemented in a wrong way
    if it is implemented as having a 1 to 1 relationship between Cars and CarsPeopleOwn
    then there can not be duplicate records in CarsPeopleOwn

    a nice idea would be to make CarID the primary key of CarsPeopleOwn
    (and that would have a very interesting side effect)

    of cource that is assuming that a car can only be owned by 1 person
    (what, according to my spouse is pure nonsence)
    do not put off till tomorrow what you can put off forever

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Prevent getting duplicate records

    Quote Originally Posted by IkkeEnGij View Post
    it makes sense...in a way
    but only if that design is implemented in a wrong way
    if it is implemented as having a 1 to 1 relationship between Cars and CarsPeopleOwn
    then there can not be duplicate records in CarsPeopleOwn

    a nice idea would be to make CarID the primary key of CarsPeopleOwn
    (and that would have a very interesting side effect)

    of cource that is assuming that a car can only be owned by 1 person
    (what, according to my spouse is pure nonsence)
    It depends on what other data you attach to it and where... I have a Ford F150 ... so do a million other people... In the example I was laying out, the Cars table is the generic Ford F150... the same one that a million people own... so Cars and CarsPeopleOwn would need a one to many relationship. At least that's how I saw it in my head. Sounds like you're thinking the Cars table was the actual instance of each million cars out there. And that's what makes this so much fun. :P If that were the case, then I would have just put PeopleID directly on the Cars table and not bothered with the middle lookup.
    But it's all moot. The point I was trying to demonstrate is how it's possible to get duplicate data when it's not really duplicate. It just LOOKs like duplicate data. The reality is if I were designing a set of tables to track cars and ownership, there would be a mass amount of tables... as there would be all kinds of things to track: make, model, colors, types, lic #, & the vehicle ID would be the VIN

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Prevent getting duplicate records

    These duplicates occur one time. If I restart my program it doesn't have any duplicates, I get them when I save new record. Another strange thing is a few records aren't duplicated. Can anyone help?
    Last edited by nikel; Aug 30th, 2017 at 03:11 PM.
    I'm not a man of too many faces
    The mask I wear is one

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Prevent getting duplicate records

    Here's my new record function:

    VB.NET Code:
    1. private void addNew_tsmi_Click(object sender, EventArgs e)
    2. {
    3.     info_lbl.Text = "yeni program ekleniyor";
    4.  
    5.     List<string> list = new List<string>();
    6.     int number;
    7.  
    8.     using (InsertData insertData = new InsertData())
    9.     {
    10.         insertData.ShowDialog();
    11.         list = insertData.GetValues();
    12.     }
    13.  
    14.     m_dbConnection.Open();
    15.     sqliteDataAdapter = new SQLiteDataAdapter();
    16.     SQLiteCommand insert = new SQLiteCommand(
    17.                             "INSERT INTO tbl_programs " +
    18.                             "(int_isSelected, txt_programName, int_is86, int_is64, " +
    19.                             "txt_preCommand, txt_preSwitch, txt_executable, " +
    20.                             "txt_switches, int_timeout, txt_md5, txt_version, " +
    21.                             "txt_description, txt_category, dt_insertDate) " +
    22.                             "VALUES(" +
    23.                             ":isSelected, :programName, :is86, :is64, :preCommand, " +
    24.                             ":preSwitch, :executable, :switches, :timeout, " +
    25.                             ":md5, :version, :description, :category, :insertDate);",
    26.                             m_dbConnection);
    27.     insert.Parameters.AddWithValue("isSelected", 0);
    28.     insert.Parameters.AddWithValue("programName", list[0]);
    29.     insert.Parameters.AddWithValue("is86", 1);
    30.     insert.Parameters.AddWithValue("is64", 1);
    31.     insert.Parameters.AddWithValue("preCommand", list[3]);
    32.     insert.Parameters.AddWithValue("preSwitch", list[4]);
    33.     insert.Parameters.AddWithValue("executable", list[5]);
    34.     insert.Parameters.AddWithValue("switches", list[6]);
    35.     insert.Parameters.AddWithValue("timeout", 30);
    36.     insert.Parameters.AddWithValue("md5", list[8]);
    37.     insert.Parameters.AddWithValue("version", list[9]);
    38.     insert.Parameters.AddWithValue("description", list[10]);
    39.     insert.Parameters.AddWithValue("category", list[11]);
    40.     insert.Parameters.AddWithValue("insertDate", Convert.ToDateTime(list[12]));
    41.     sqliteDataAdapter.InsertCommand = insert;
    42.     sqliteDataAdapter.InsertCommand.ExecuteNonQuery();
    43.     sqliteDataAdapter.SelectCommand = new SQLiteCommand(
    44.                             "SELECT DISTINCT " +
    45.                             "ID, " +
    46.                             "int_isSelected, txt_programName, int_is86, int_is64, " +
    47.                             "txt_preCommand, txt_preSwitch, txt_executable, " +
    48.                             "txt_switches, int_timeout, txt_md5, txt_version, " +
    49.                             "txt_description, txt_category, dt_insertDate " +
    50.                             "FROM tbl_programs WHERE ID <> NULL;",
    51.                             m_dbConnection);
    52.     sqliteDataAdapter.SelectCommand.ExecuteScalar();
    53.     m_dbConnection.Close();
    54.     programs_dgv.Sort(programs_dgv.Columns["txt_programName"], ListSortDirection.Ascending);
    55.     GetData();
    56.  
    57.     info_lbl.Text = "hazır";
    58. }
    Last edited by nikel; Aug 30th, 2017 at 03:11 PM. Reason: I fixed syntax highlighting
    I'm not a man of too many faces
    The mask I wear is one

  8. #8
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Prevent getting duplicate records

    Sounds like you're thinking the Cars table was the actual instance of each million cars out there. And that's what makes this so much fun. :P If that were the case, then I would have just put PeopleID directly on the Cars table and not bothered with the middle lookup.
    congrats, so you did pay attention when i said:
    a nice idea would be to make CarID the primary key of CarsPeopleOwn
    (and that would have a very interesting side effect)
    I have a Ford F150 ... so do a million other people... In the example I was laying out, the Cars table is the generic Ford F150
    that is in compleet contradiction with your Cars table
    In the example you were laying out, in the Cars table was the generic Ford F150
    it would just have 2 fields: a make and a model: 'Ford' and 'F150'

    so you own 3 fords F150
    2 yellow and a red
    how are you going to put in CarsPeopleOwn that you own 3 fords F150 ? (without having duplicate data ?)

    i know, that is not duplicate data, it just seems so, right ?

    to sum up:
    to prove a point (and it was a just point, that you were right to make)
    you made up a completely contrived example
    and all i did was turn that completely contrived example
    in a workable system
    do not put off till tomorrow what you can put off forever

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Prevent getting duplicate records

    bump
    I'm not a man of too many faces
    The mask I wear is one

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Prevent getting duplicate records

    OK I fixed it. When the program first loaded, there was a command builder and I got rid of it. Instead I used selectcommand. Thanks for the helps.
    I'm not a man of too many faces
    The mask I wear is one

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width