0

I want to add book to my Book_Master table. But I've got an error of Violating Primary Key. I really don't understand of the duplicate value because I am generating bookid (primary key) through code then from where the duplication occur?

Here's the pic

5
  • Please post your table definition so we can see the constraint. You shouldn't need to insert the identity into a table in SQL Server through your code, it can be set up to automatically increment. Commented Apr 26, 2016 at 21:09
  • You're trying to insert data with a value of 9 into that field but you've already got a 9 in there. It's a unique field so you can't have two instances of the same value. Commented Apr 26, 2016 at 21:21
  • For emphasis, I'm echoing what Ron is implying and Rich Benner noted below. That code may be vulnerable to a Sql Server Injection attack, something that is commonly exploited. blogs.msdn.microsoft.com/sqlphp/2008/09/30/… Commented Apr 26, 2016 at 21:32
  • Try using auto generated primary keys Commented Apr 27, 2016 at 2:35
  • 1
    Please paste in your code. We can't copy/paste a screen shot, which makes it hard to offer further corrections. Commented Apr 30, 2016 at 20:14

3 Answers 3

2

Try replacing:

INSERT INTO Book_Master (bookid, booktitle.... 

with:

INSERT INTO Book_Master (booktitle.... 

Generally speaking, when inserting into a table with an identity column, you cannot pass your own.

Also please do consider the ramifications of a user entering a book title of:

';drop table Book_master 
Sign up to request clarification or add additional context in comments.

Comments

1

You've got the SqlCommand execution in a for( int i = 0; i < counter ; i++) loop...but not the generation of the bookid. Google sql injection for why that's a really bad way to build a SQL statement.

Edit:

So, the basic problem is, you need to add records to the book_detail for as many copies as you have...but just once for book_master.

There are a couple of issues here. First, just do the insert into book_master just once like you have it...by taking the line:

for (int i=0; i < counter; i++ ){ 

...and the matching...

} 

...out.

Then, right after cmdSave.ExecuteNonQuery(), and using the same open connection, edit the cmd.Command text to insert into the book_detail. We don't know what fields go into book_detail...but this is the statement that will go inside your loop.

Generally, you want to set up your statements so that you can reuse the statement multiple times...with different parameters. This is how you prevent the sql injection problems people have been trying to warn you about. It looks like this:

cmdSave.CommandText = "insert sometable(a,b) values (@a,@b)"; cmdSave.Parameters.Add( "@a", SqlDbType.String, 4 ); // for example cmdSave.Parameters.Add( "@b", SqlDbType.Int ); // for example for( int i=0;i<counter;i++){ cmdSave.Parameters["@a"].Value = GetNextValueA(i); // for example cmdSave.Parameters["@b"].Value = GetNextValueB(i); // for example cmdSave.ExecuteNonQuery(); } 

7 Comments

I just want to add that many copies of books that are put in run time in quantity text box through loop.... I am not using for-loop for generating bookid
Right - but you can't put multiple records into book_master if the book_id is the primary key.
Your insert statement is inside the loop...move it to after the loop
That's where i am stuck ... I don't have an idea how to save book copies into Book_Detail table in Database .... I want to save book data in Book_Master table and copies of that book in to Book_Detail... @Clay
@khaafi, I get it...will edit my answer to outline what to do
|
0

Make bookid an auto-incrementing number

ALTER TABLE book_master ALTER COLUMN bookid INT NOT NULL IDENTITY 

5 Comments

I don't want to auto increment bookid .. i want to increment through max query
Then you are going to need to exclusively lock the table in a serialized transaction. But that opens up it's own set of problems that you aren't ready for, so just use the database in the way it was intended.
Thanks man ... Actually I am new to programming ... That's why ... Bundle of thanks for your precious answer
I don't intend to be mean-spirited about this, but I've been doing this for nearly 20 years and even I screw-up half the time when trying to simulate auto-numbering outside of the database.
@Jonathen Thanks for your time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.