473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Error while inserting data into table

I am developing an application which grab a comma delimited file and inserts them into a database. but while inserting it gives me an error. the code is below.

Do While Not EOF(iDrop)
Line Input #iDrop, iLine
iArray = Split(iLine, ";")
iArray(5) = Round(iArray(5) / 21.21, 0)
With rs
.AddNew
.Fields("ID") = crID
.Fields("NO") = iArray(0)
.Fields("SID") = iArray(1)
.Fields("CID") = iArray(2)
.Fields("CClass") = iArray(3)
.Fields("Cfier") = iArray(4)
.Fields("DB") = iArray(5)
.Update
End With
Loop
Close #iDate

Error:
unexpected error -2147217873 in subroutine dosomething.
[microsoft][odbc sql server driver][sql server] violation of primary key constrain PK_DBand. cannot insert duplicate key in object DB


here Dband is my table with primary key ID and DB is one of my column in the table Dband.

i am not sure why i am getting this error. It works for some records until i get this message.

in all i have about 1000 records and it does only 70-80 records and gives me this error.

any clues why this is happening. i am not sure the explaination helps or not please ask me question to understand more.
thanks
Jan 3 '07 #1
11 2662
Killer42
8,435 Expert 8TB
You are splitting on a semicolon (;) but you said the file is comma-delimited.

Oh, and a question - where is crID getting its value?
Jan 3 '07 #2
sorry its semicolon delimited...and also crID is query done on the another table and is a statis value from a table. crID i get it correctly and is not an issue.


You are splitting on a semicolon (;) but you said the file is comma-delimited.

Oh, and a question - where is crID getting its value?
Jan 3 '07 #3
Killer42
8,435 Expert 8TB
sorry its semicolon delimited...and also crID is query done on the another table and is a statis value from a table. crID i get it correctly and is not an issue.
So, does that mean we're looking at a cut-down version of the code? As it stands, it looks as though it should produce a duplicate-value error on your primary key for the second record, because of .Fields("ID") = crID.
Jan 4 '07 #4
Killer42
8,435 Expert 8TB
I wonder whether the 70-80 records might just reflect some buffer size, and the error occurs when (for instance) the buffer is filled and passed to the server.

Could you try chopping up the text file and loading the first couple of hundred records into 50-record chunks, to see whether it behaves the same?
Jan 4 '07 #5
Killer42
8,435 Expert 8TB
...
Do While Not EOF(iDrop)
Line Input #iDrop, iLine
...
Close #iDate
I doubt this is significant. But just to satisfy my curiosity, is #iDate a typo, or a different file to #iDrop, or an error in the code? Also, just for clarity, I'm going to post a copy of your code here with the appropriate COD tags around it. That way, the indenting will come through.
Expand|Select|Wrap|Line Numbers
  1. Do While Not EOF(iDrop)
  2.     Line Input #iDrop, iLine
  3.     iArray = Split(iLine, ";")
  4.     iArray(5) = Round(iArray(5) / 21.21, 0)
  5.     With rs
  6.         .AddNew
  7.         .Fields("ID") = crID
  8.         .Fields("NO") = iArray(0)
  9.         .Fields("SID") = iArray(1)
  10.         .Fields("CID") = iArray(2)
  11.         .Fields("CClass") = iArray(3)
  12.         .Fields("Cfier") = iArray(4)
  13.         .Fields("DB") = iArray(5)
  14.         .Update
  15.     End With
  16. Loop
  17. Close #iDate
Jan 4 '07 #6
I doubt this is significant. But just to satisfy my curiosity, is #iDate a typo, or a different file to #iDrop, or an error in the code? Also, just for clarity, I'm going to post a copy of your code here with the appropriate COD tags around it. That way, the indenting will come through.
Expand|Select|Wrap|Line Numbers
  1. Do While Not EOF(iDrop)
  2.     Line Input #iDrop, iLine
  3.     iArray = Split(iLine, ";")
  4.     iArray(5) = Round(iArray(5) / 21.21, 0)
  5.     With rs
  6.         .AddNew
  7.         .Fields("ID") = crID
  8.         .Fields("NO") = iArray(0)
  9.         .Fields("SID") = iArray(1)
  10.         .Fields("CID") = iArray(2)
  11.         .Fields("CClass") = iArray(3)
  12.         .Fields("Cfier") = iArray(4)
  13.         .Fields("DB") = iArray(5)
  14.         .Update
  15.     End With
  16. Loop
  17. Close #iDate


Sorry but #iDate is a different file name which i close.
Jan 5 '07 #7
Killer42
8,435 Expert 8TB
Sorry but #iDate is a different file name which i close.
Ok. So long as it is meant to be there, I'm happy. :) Thought it might have been an error.
Jan 5 '07 #8
So, does that mean we're looking at a cut-down version of the code? As it stands, it looks as though it should produce a duplicate-value error on your primary key for the second record, because of .Fields("ID") = crID.
Well my file is about 6 MB in size with 400,000 record in each line. Its comma delimited with 7 fields.

the way i am looking at the table is this way. i have a parent table say A with 3 fields, with ID as primary key. A child table B is the table i want to insert the data (thats what the code is doing trying to insert the data) with primary key as DateID which is a foreign key for ID in A. So each record with ID in A corresponds to more then 1 records in B (maybe 100,000 records). I do a query to pick newest record and then insert the whole 6MB file with same value in DateID for each ID which i query.

is there a better way to do this?
Jan 5 '07 #9
Killer42
8,435 Expert 8TB
Well my file is about 6 MB in size with 400,000 record in each line. Its comma delimited with 7 fields.

the way i am looking at the table is this way. i have a parent table say A with 3 fields, with ID as primary key. A child table B is the table i want to insert the data (thats what the code is doing trying to insert the data) with primary key as DateID which is a foreign key for ID in A. So each record with ID in A corresponds to more then 1 records in B (maybe 100,000 records). I do a query to pick newest record and then insert the whole 6MB file with same value in DateID for each ID which i query.

is there a better way to do this?
Oh, there's almost always a better way to do things. But I don't have a clue what it would be in this case. :)

However, things have been a bit confusing so far. You told us ID was your primary key, then listed code which inserts duplicate values into ID. What are we supposed to think? :) I may just be misreading the question, of course.

Can you please clarify exactly what is happening at the time you get the error? For a start, is the error happening on insert to table A or B?

I'll try to get another expert or two to have a look, as well. Access is not really my strong point.

P.S. I still think you need to check for bad data in the input file.
Jan 5 '07 #10
MMcCarthy
14,534 Expert Mod 8TB
I am developing an application which grab a comma delimited file and inserts them into a database. but while inserting it gives me an error. the code is below.

Do While Not EOF(iDrop)
Line Input #iDrop, iLine
iArray = Split(iLine, ";")
iArray(5) = Round(iArray(5) / 21.21, 0)
With rs
.AddNew
.Fields("ID") = crID
.Fields("NO") = iArray(0)
.Fields("SID") = iArray(1)
.Fields("CID") = iArray(2)
.Fields("CClass") = iArray(3)
.Fields("Cfier") = iArray(4)
.Fields("DB") = iArray(5)
.Update
End With
Loop
Close #iDate

Error:
unexpected error -2147217873 in subroutine dosomething.
[microsoft][odbc sql server driver][sql server] violation of primary key constrain PK_DBand. cannot insert duplicate key in object DB


here Dband is my table with primary key ID and DB is one of my column in the table Dband.

i am not sure why i am getting this error. It works for some records until i get this message.

in all i have about 1000 records and it does only 70-80 records and gives me this error.

any clues why this is happening. i am not sure the explaination helps or not please ask me question to understand more.
thanks
Please post all the declarations of the variables used here. The array, etc.

Also how are you looping through crID

Mary
Jan 5 '07 #11
NeoPa
32,556 Expert Mod 16PB
Points I would make :
1. It is a good idea not to include the code for your whole database, but you must select the code to leave out based on an understanding of what is required. It would appear that you have missed out some code that is helpful for others to understand what's going on.
2. You should really post the data structure (What tables do you have involved in this process? What are the relevant fields involved within those tables? What are their types? Which ones are keyed Primary and Foreign?)
Example :
Posting Table/Dataset MetaData
Expand|Select|Wrap|Line Numbers
  1. Table Name=tblStudent
  2. StudentID; Autonumber; PK
  3. Family; String; FK
  4. Name; String
  5. University; String; FK
  6. MaxMark; Numeric
  7. MinMark; Numeric
More specifically :
1. As mentioned earlier, Closing #iDate in code which deals with the file #iDrop is unhelpful. Especially as you don't close (or even open) #iDrop within this code.
2. When posting code, it is always important to include variable definitions and Procedure definitions as they contain a lot of very important information.

If you are using an Access database, you could use the Text File import instead to do the import part for you. Would this not be a better solution?


NB NB NB NB NB NB NB NB NB NB NB NB NB NB NB NB NB NB NB
Please respond to all points made (by all experts kind enough to get involved with your request for help).
It's very difficult for people to help you if they don't have enough information with which to work.
Jan 5 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Charles Wilt | last post by:
I have a IBM iSeries (aka AS-400) running v5r3 of OS/400 that I access via a linked server from SQL Server 2000. The following select works fine: select * from...
1
by: suslikovich | last post by:
Hi all, I am getting this error when insert values from one table to another in the first table the values are varchar (10). In the second they are datetime. The format of the data is mm/dd/yyyy...
1
by: ing42 | last post by:
I have a problem with inserting records into table when an indexed view is based on it. Table has text field (without it there is no problem, but I need it). Here is a sample code: USE test GO...
10
by: Phil Latio | last post by:
I am inserting data into user table which contains 5 fields, sounds simple enough normally but 2 of the fields are designated as UNIQUE. If someone does enter a value which already exists, how do I...
1
by: madhuxml82 | last post by:
Dear Forum Members, I have generated an XML Schema and a Table of XMLType referencing the XML Schema. Now When I am Inserting the Data into the Table. I am getting the Error 0RA-30937: Error is...
2
ak1dnar
by: ak1dnar | last post by:
Hi, I am getting some sql errors when trying to insert a record to mysql table using asp.net application. Let me explain the scenario for your reference. This asp.net web form is executing...
0
by: mbenedict | last post by:
I am rather new at this code and am attempting to modify existing code to use clob datatypes, which I have never used before. The database tables have been set up for clob data. When trying to use...
2
by: hakkatil | last post by:
Hi to all, I have a page that inserts excel sheet to access database. I am using asp. What I want to do is to check the inserting record if it is in the database. Basicly checking the dublicate...
4
by: ghjk | last post by:
I wan to add some data using php page to mysql db. But I got the error saying "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.