.hi guys! good day to all, i am currently developing a library system as one of my school projects using Visual Studio C# 2008 and SQL Server 2008. however there are several problems i have encountered.

1st: How to retrieve the value of an auto-incremented field.
2nd: How to use a checkedListBox in inputting multiple authors on my Book_Author Database.

to make things clear here's the scenario:
i have three databases namely Books, Author and Book_Author. the primary keys of Books and Author databases must become foreign keys of the Book_Author database.

The primary key of Books is Book_Number and for Author the primary key is AuthorID, both of them are auto-incremented fields.
The Book_Author database contains three fields namely Book_AuthorID(Primary Key,Auto Increment), Book_Number(Foreign Key) and AuthorID(Foreign Key).

My professor wants my system to insert data on Book_Author by retrieving the value of the auto-incremented field Book_Number, and get the value of AuthorID per items checked in my CheckedListBox populated by the Author Database.

.I'm hoping anyone can help me with this as i am currently stuck on developing my project.

.More powers to all the programmers! God Bless! :)

Recommended Answers

All 2 Replies

In a stored procedure, the value of @@IDENTITY is the last indentity field generated. So I'd create a procedure to do the insert and return that value. Then you can use it to update your linking table (and they are tables, not databases :))

You should generally use Scope_Identity over @@Identity as @@Identity is connection based and may not reflect the last added row.
Just so that i have it clear in my head, this is how i understand your order of execution:

-Populate CheckedList with Authors
-User enters details for a book
-Add book to Books Table -> retrieve newly created ID
-Link Book to selected Author using ID's from CheckedList and returned BookID

Is this what you are aiming for? If not, let me know whats different before we start suggesting approaches.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.