Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

Solved SQL insert resulting in duplicate records

Posted on 2010-11-11
Medium Priority
Last Modified: 2012-05-10
I am working on a vb application that grabs PLC data and inserts it in batches into a sql table. Everything seems to work however i am getting duplicate records. Can anyone tell me why? I have attached the code that does the SQL insert.

Private Sub InsertSQL()
        Dim sqlstring As String, dr As DataRow
        sqlstring = ""
            For Each drw As DataRow In DsetSQL.Tables(0).Rows
                sqlstring = sqlstring + "INSERT INTO [PNJ_Data].[dbo].[L1Pack_Weights] ([Manuf_Time],[Filler_Num],[Setpoint],[Weight],[Container],[Preact],[Bulk_Pulses],[Trim_Pulses],[ID]) VALUES ("
                sqlstring = sqlstring + "'" + drw(0).ToString + "',"
                sqlstring = sqlstring + drw(1).ToString + ","
                sqlstring = sqlstring + drw(2).ToString + ","
                sqlstring = sqlstring + drw(3).ToString + ","
                sqlstring = sqlstring + drw(4).ToString + ","
                sqlstring = sqlstring + drw(5).ToString + ","
                sqlstring = sqlstring + drw(6).ToString + ","
                sqlstring = sqlstring + drw(7).ToString + ","
                sqlstring = sqlstring + drw(8).ToString + "); "

                '    MsgBox(sqlstring)
                dr = DsetLog.Tables(0).NewRow
                dr(0) = DateTime.Now
                dr(1) = sqlstring 'drw(8).ToString
                da.InsertCommand = New SqlCommand(sqlstring, myConnection)
            Catch ex As Exception
                dr = DsetLog.Tables(0).NewRow
                dr(0) = DateTime.Now
                dr(1) = "Error Inserting Row: " + ex.Message
                Exit Sub
            End Try

            dr = DsetLog.Tables(0).NewRow
            dr(0) = DateTime.Now
            dr(1) = "Batch insert complete"

        Catch ex As Exception
            dr = DsetLog.Tables(0).NewRow
            dr(0) = DateTime.Now
            dr(1) = "Error Inserting Batch Data: " + ex.Message
            SQLFailed = True
            Exit Sub
        End Try
    End Sub

Open in new window

Question by:michael_krell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 23

Accepted Solution

Racim BOUDJAKDJI earned 2000 total points
ID: 34115011
A suggestion: If you implement a unicity constraint on the [L1Pack_Weights]  table, the database will reject your insert and probably tell you why.   That is why constraints are used in the first place:
> Prevent dupplicates to be inserted
> Tell you when and how the dupplicate breaks the unicity.

Hope this helps...

Author Comment

ID: 34115696
i will do that, but it still doesn't explain why i have duplicates. And it just seems random.

Is there anyway that i can run a query that shows me transactions to help me troubleshoot this?
LVL 23

Expert Comment

ID: 34116374
There are two questions here:  the cause of duplicates being inserted and how to detect the ones already inserted.  I suggest you separate the two questions.

For the causes...
<<i will do that, but it still doesn't explain why i have duplicates. >>
As I said, the reason why you have difficulty knowing is because you are trying to implement unicity on the client, when the only effective way to do that is on the server side, using constraints.

For detecting the duplicates...
<<Is there anyway that i can run a query that shows me transactions to help me troubleshoot this?>>
Can you list the columns on L1Pack_Weights table make you consider a row to be a duplicate of another when these column values are the same for two different rows.  Without this information we can not help you detect the duplicates.


Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question