Re: Primary key error

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Eric,

Your notes below do not make it clear to me what you are doing with each column of data.

I would suggest that you use SQL Server Profiler to trace a problem case and capture the SQL statements that are issued. I see that you are building a string of SQL code for the update, so if you create the INSERT the same way the profiler trace should show all the parameters that you are inserting.

Looking at that might help you.

To help somebody else duplicate your problem, follow the guidelines at:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

RLF

"Eric" <Eric@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:D1637D50-833A-4E34-A917-38C2B48010C4@xxxxxxxxxxxxxxxx
Hi,

In my VB.NET program the user can update a bunch of information.
Just before the update is put in the database, I first change one field by
putting _bak behind the text with:

mySQL = "UPDATE " + Project.GeneralLog + " SET " + GeneralLog.YPcode + " =
" + GeneralLog.YPcode + "+ '_BAK' "
mySQL += "WHERE " + GeneralLog.YPcode + " = '" + ypcode + "'"

myCMD = New SqlCommand(mySQL, conn)
Try
myCMD.ExecuteNonQuery()
Catch ae As SqlException
conn.Close()
Return False
End Try

After that I use INSERT TO to put the the changed data in.
In most tables it goes ok because the primary key is the field that gets
changed, but in the tables that have a seperate ID field to be the primary
key (because the field that is primary in the other tables can have double
entries in this table).

For these tables I get an error:

{"Violation of PRIMARY KEY constraint 'PK__OverallS__3214EC2775435199'.
Cannot insert duplicate key in object 'dbo.OverallStatus'. The statement has
been terminated."}

The table is created with:

mySQL = "CREATE TABLE " + Project.OverallStatus + " (ID BIGINT
IDENTITY(1,1) PRIMARY KEY, " + OverallStatus.YPcode + " VARCHAR(15), "
mySQL += OverallStatus.Datum + " DATETIME, "
mySQL += OverallStatus.Description + " VARCHAR(5000))"
myCMD = New SqlCommand(mySQL, conn)
Try
myCMD.ExecuteNonQuery()
Catch ae As SqlException
nOK = True
End Try

The INSTERT INTO command I use, is the same one as for putting the data in
the table in the first time, so that is working properly.

What am I doing wrong?

rg,
Eric


.



Relevant Pages

  • Re: Primary key error
    ... myCMD = New SqlCommand(mySQL, conn) ... Catch ae As SqlException ... After that I use INSERT TO to put the the changed data in. ...
    (microsoft.public.sqlserver.server)
  • RE: SQL Server Error
    ... SQL 2000 Server ... It is an Access 97 database ... Set conn = New ADODB.Connection ...
    (microsoft.public.sqlserver.server)
  • Re: Prüfen auf is Nothing?
    ... > Connection zulassen (um ggf. ... > Public Sub OpenDyna(R As ADODB.Recordset, SQL As String, Optional Conn ...
    (microsoft.public.de.access.clientserver)
  • RE: sql criteria change
    ... you don't really need to use MS Query. ... Dim SQL As String ... Dim conn As ADODB.Connection ...
    (microsoft.public.excel.programming)
  • Re: Data Access done simply ...
    ... System.Data.SqlClient.SqlConnection conn; ... System.Data.DataSet dsNames; ... "Jon Rowlan" wrote: ... I want to connect to an SQL database, execute a SQL command to return a ...
    (microsoft.public.vstudio.development)