2. Table of Contents
• Library System Overview
• Design Highlights
• Windows Form Sample
• ASP.NET Web Form Sample
• ADO.NET Code Sample
• Stored Procedure Sample
3. Library System Overview
• Introduction:
– I designed and implemented a library system to handle day-to-day
operations performed by librarians.
• Audience:
– Librarians will be the primary users of the applications.
• Project Goals:
– Design and develop Windows and Web based applications that allow
librarians to add adult and juvenile members, check in and check out
books, and maintain an inventory of the library’s books.
– Develop code that is easily maintainable.
– Provide validation for all required fields.
– Provide error handling
– Produce a user interface that is intuitive, requiring minimal training for users
while minimizing resource utilization
4. Design Highlights
• 3 Tiered Architecture
• User-friendly interface developed using MDI forms with
menu strip navigation and ASP.NET Web forms using Tree
View navigation
• Business ruled and validations encapsulated within the
Business Tier
• ADO.NET Data Access called Stored Procedures in the SQL
Server Database
• Data transported between the layers using Entities and
Strongly-Typed Data Sets
5. Windows Form Sample
Data entry screen
Flags input format errors
Requires a valid Adult Member Sponsor
6. Windows Form Sample
Lists books on loan in a Data Grid View
Allows check out of books
Non-modal confirmation message
7. ASP.NET Web Form Sample
Data entry screen
Flags input format errors
Requires a valid Adult Member Sponsor
Prompts to renew Adult Sponsor if expired membership
8. ASP.NET Web Form Sample
Lists books on loan
Allows check out of books
Non-modal confirmation message
Prompts to renew Membership if expired
9. ADO.NET Code Sample - Parameters
// Create DB connection with connection string
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.LibraryConnectionString))
{
// Define the SQL Command
using (SqlCommand command = new SqlCommand(quot;checkoutquot;))
{
// SQL Command is a stored Procedure
command.CommandType = CommandType.StoredProcedure;
command.Connection = connection;
// Load the Parameters
command.Parameters.AddWithValue(quot;@Member_Noquot;, memberID);
command.Parameters.AddWithValue(quot;@ISBNquot;, isbn);
command.Parameters.AddWithValue(quot;@Copy_Noquot;, copyNumber);
// Open connection
connection.Open();
// Execute the Stored Procedure
command.ExecuteNonQuery();
}
}
10. ADO.NET Code Sample
Use of Ordinal Column values for efficiency:
// Define Common Ordinal Column IDs
int titleColumn = reader.GetOrdinal(quot;titlequot;);
int authorColumn = reader.GetOrdinal(quot;authorquot;);
int memberNumberColumn = reader.GetOrdinal(quot;member_noquot;);
int outDateColumn = reader.GetOrdinal(quot;out_datequot;);
int dueDateColumn = reader.GetOrdinal(quot;due_Datequot;);
// Create object to read into
object[] allFields = new object[reader.FieldCount];
reader.Read();
Use of Typed Dataset Table Adapters:
// Create Typed Dataset Table adapter to connect to database
ItemsTableAdapter itemsTableAdapter = new ItemsTableAdapter();
// The stored proc has one input parameter(@Member_No)
int returnValue = itemsTableAdapter.Fill(ids.Items, memberID);
11. Stored Procedure Sample
/* ADDNEWBOOK
…
Adds abook to the library
error checking ensures that valid parameters ALTER proc [dbo].[addnewbook]
are supplied for all non nullable fields. @ISBN int = NULL,
@Title varchar(63) = NULL,
@Author varchar(31) = NULL,
Input Parameters:
@Translation char(8) = NULL,
@ISBN smallint = NULL,
@Cover char(8) = NULL,
@Title varchar(63) = NULL, @Loanable char(1) = NULL,
@Author varchar(31) = NULL, @Copy_No int Output
@Translation char(8) = NULL,
as
@Cover char(8) = NULL,
@Loanable char(1) = NULL
begin try
execute addmessages -- Set up error messages
Output Parameters:
declare @New_Copy_No int
@Copy_No int = Output
declare @New_Title_No int
Error Codes: Error_State 12
if @ISBN is NULL
50018 - Invalid ISBN Begin
50019 - @ISBN must be between 1 and print 'Invalid ISBN‘
2,147,483,647 raiserror (50018,11,12) -- Invalid ISBN
return
50032 - No more Copies can be added to Library
End
50111 - SQL Error Inserting New Title
…
…
12. Stored Procedure Sample
...
…
end try
begin catch -- raise an error to pass up the...
begin try -- Insert a new Copy DECLARE @ErrorMessage NVARCHAR(4000);
insert copy (ISBN, Copy_No, Title_No, On_Loan) DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
values( @ISBN, @New_Copy_No,
SELECT
@New_Title_No, 'N')
@ErrorMessage = ERROR_MESSAGE(),
end try
@ErrorSeverity = ERROR_SEVERITY(),
begin catch -- If an error occurs during insert @ErrorState = ERROR_STATE()
print 'SQL Error Inserting New Copy‘
RAISERROR (@ErrorMessage, -- Message text.
rollback tran
@ErrorSeverity, -- Severity.
raiserror (50113,16,12) -- Error Inserting New...
@ErrorState -- State.
Return
)
end catch end catch
set @Copy_No = @New_Copy_No
commit tran -- Add Item was successful
return
…