You are viewing our Forum Archives. To view or take place in current topics click here.

Getting a unique constraint error when inserting to DatabasePosted:

Gavin-
  • Ninja
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,307
Reputation Power: 827
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
  • V5 Launch
Status: Offline
Joined: Jul 14, 20144 Year Member
Posts: 1,722
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
  • 2 Million
Status: Offline
Joined: Oct 09, 20107 Year Member
Posts: 2,320
Reputation Power: 104
Unique constraint means you have a duplicate key so check your data
#4. Posted:
tortuga
  • Wizard
Status: Offline
Joined: Dec 25, 20098 Year Member
Posts: 2,312
Reputation Power: 530
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-
  • Ninja
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,307
Reputation Power: 827
tortuga wroteIt'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
  • Wizard
Status: Offline
Joined: Dec 25, 20098 Year Member
Posts: 2,312
Reputation Power: 530
Gavin- wrote
tortuga wroteIt'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.