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?

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?

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 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(); } Make bookid an auto-incrementing number
ALTER TABLE book_master ALTER COLUMN bookid INT NOT NULL IDENTITY