You are viewing our Forum Archives. To view or take place in current topics click here.
Getting a unique constraint error when inserting to Database
Posted:

Getting a unique constraint error when inserting to DatabasePosted:

Gavin-
  • Winter 2020
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
So I am using visual studio and I am adding movies to a list box and when i click rent i want them all to insert at the same time.

First off here is the error its a unique constraint error:

[ Register or Signin to view external links. ]

So this is how it works ,,,

I have a rentals table and rental items table.

[ Register or Signin to view external links. ]

An example of what it should look like when i rent 3 movies ,,

Rental Table Rental Items Table
RentalID : 1 Rental ID : 1 DVID : 3
CustomerID : 1 RentalID : 1 DVDID :2;
DateRented : ETC RentalID : 1 DVDID : 1
DueDate : ETC

Pretty much it will insert one rental but the rental will have 3 rental items.


Here is the code i am using to loop over the list box and insert its working properly only that its inserting only 1 dvd into rental items when i am actually renting 3.

[ Register or Signin to view external links. ]



Here is my insert Statement ::

[ Register or Signin to view external links. ]

gets error when executing the statement.
#2. Posted:
ProJimmyRustler
  • E3 2016
Status: Offline
Joined: Jul 14, 20149Year Member
Posts: 1,720
Reputation Power: 71
Status: Offline
Joined: Jul 14, 20149Year Member
Posts: 1,720
Reputation Power: 71
You are probably getting an error because you have plus signs with in your values statement.

I'm for certain that you don't need those.

Remove those and try that.
The reason I say that is because you have a numeric datatype and you as far as the SQL INSERT is concerned, you are try to put two plus signs into it.
#3. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Unique constraint means you have a duplicate key so check your data
#4. Posted:
tortuga
  • Blind Luck
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
It's hard to figure out what's wrong from only snippets of code, but try adding some log statements into the printDVD() method to see what values it's trying to insert into the RentalItems table. My guess is that your loops for iterating over the list box are not properly setting the DvdId field of your object.
#5. Posted:
Gavin-
  • Fairy Master
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
tortuga wrote It's hard to figure out what's wrong from only snippets of code, but try adding some log statements into the printDVD() method to see what values it's trying to insert into the RentalItems table. My guess is that your loops for iterating over the list box are not properly setting the DvdId field of your object.



Pretty much its only inserting one movie into rental items when it should be inserting 3 , I am assuming the unique constraint error is from that.
#6. Posted:
tortuga
  • Fairy Master
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Gavin- wrote
tortuga wrote It's hard to figure out what's wrong from only snippets of code, but try adding some log statements into the printDVD() method to see what values it's trying to insert into the RentalItems table. My guess is that your loops for iterating over the list box are not properly setting the DvdId field of your object.

Pretty much its only inserting one movie into rental items when it should be inserting 3 , I am assuming the unique constraint error is from that.

Right - it inserts the first record, and then fails at inserting the following records.

Since you've got a composite primary key for the RentalItems, either the RentalId field or DvdId field (or both) are not changing on subsequent insertions. That's why I'm suggesting to add some print statements to figure out what you're actually trying to insert into your RentalItems table. Then, modify your code or setters appropriately.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.