My Portfolio


Published on

Sample code

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

My Portfolio

  1. 1. <ul><li>Library Phase II – Windows Business & Data Access TiersLibrary Phase III -Web Based Front-endIntroduction: In phase II the task was to create a three tier system to retrieve information from a T-SQL database. The system has a windows front-end, business tier and data tier. I needed to create the stored procedures to communicate with the data access layer. The stored procedures checked for invalid or missing data. The data layer handled any errors thrown by the T-SQL and passing these to the business layer. The business layer converted the errors into an enumeration that was handled by the windows front-end. Phase III was to replace the windows front-end with a web-based front-end. Keeping the business layer and data access layer. The web-based front-end needed to provide security and meet all the other criteria in phase II. Phase III required the ability to add books and renew membership (membership lasted for 1 year).Audience:Phase II and III was developed for the library personnel to handled the day-to-day activities. The project allowed the librarians to add members, check in and out books, and view members’ information. In both phases the screens were designed to make data entry as easy and flawless as possible.Project Goals:Phase II was to create the stored procedures, data access layer and entities. These were then added to windows font end and business layer I created in phase I. All error codes were handled all errors and all code would be easy to maintain. Members consisted of two types: Adults and Juveniles (less than 18 years old). The juvenile required an adult member to sponsor them.Project Goals: Phase IIReplace given data access layer (from phase I), using stored proceduresDevelop a business layer to handle all business rulesCreate the stored procedures required by the data access layerCreate the entities classes for the projectPhase IIIUse the business access and data layers created in phase II and replace the windows front-end with a web front-endAdd security to the web pages so that only those in the Librarian role are given access to the pagesAdd a page to allow the librarian to add books to the libraryGive the librarian the ability to renew membership when it expiresConvert a juvenile member to an adult member when their record is viewed if past their 18th birthday</li></ul>Adding Adult to the LibraryFrom the windows project (phase II), button click method: The user is enters the new adult member’s information and presses the button to accept the information to be added. The form will ask the user to confirm the addition and then call the business layer’s method to add the adult.// Add clided, validate then add memberprivate void btnAdd_Click(object sender, EventArgs e){ // validate all controls on the form (this is known as thorough validation) if (!this.ValidateChildren(ValidationConstraints.Visible)) { // keep the dialog open if controls do not validate this.DialogResult = DialogResult.None; } else { string message = string.Format(" Add {0} {1} {2} as a member?" , txtFirstName.Text, txtMI.Text, txtLastName.Text); DialogResult myResult = MessageBox.Show(this, message, " Add adult member" , MessageBoxButtons.YesNo); if (myResult == DialogResult.Yes) { // Everything has been validated ErrorCode errCode; BusinessLayer bl = new BusinessLayer(); AdultMember myMember = new AdultMember(); // Assign member information myMember.FirstName = txtFirstName.Text; myMember.MiddleInitial = txtMI.Text; myMember.LastName = txtLastName.Text; myMember.Street = txtStreet.Text; myMember.City = txtCity.Text; myMember.State = cboState.SelectedValue.ToString(); myMember.ZipCode = txtZipCode.Text; myMember.PhoneNumber = txtPhone.Text; // Add adult member errCode = bl.AddNewMember(myMember); if (errCode == ErrorCode.AddAdultFailed) { toolStripStatusLabel1.Text = " Could not add this Adult record" ; return; } // Pass and create new Information form frmMemberInfo myForm = new frmMemberInfo(myMember); myForm.MdiParent = this.MdiParent; myForm.Show(); this.Close(); } } else toolStripStatusLabel1.Text = " User canceled adding adult member" ; }}<br />ADO.NET: Adding Juvenile to databaseAdd member method, overridden to handle either an Adult or Juvenile member. This code displays how I handled the addition of the juvenile member. The method is passed an object of type JuvenileMember./// <summary>/// Add Member to Library/// </summary>/// <param name=" aMember" >JuvenileMember</param>public void AddMember(JuvenileMember aMember){ // Set-up the connection using (SqlConnection cnn = new SqlConnection(Properties.Settings.Default.LibraryConnection)) { // Create the command using (SqlCommand cmd = new SqlCommand(" dbo.AddJuvenile" , cnn)) { try { // Assign the parameters cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(" @FirstName" , aMember.FirstName); cmd.Parameters.AddWithValue(" @MiddleInitial" , aMember.MiddleInitial); cmd.Parameters.AddWithValue(" @LastName" , aMember.LastName); cmd.Parameters.AddWithValue(" @Adult_Member_No" , aMember.AdultMemberID); cmd.Parameters.AddWithValue(" @Birth_Date" , aMember.BirthDate); cmd.Parameters.Add(" @MemberNo" , SqlDbType.SmallInt, 2).Direction = ParameterDirection.Output; cmd.Parameters.Add(" @Return" , SqlDbType.Int, 4).Direction = ParameterDirection.ReturnValue; // Open the connection cnn.Open(); cmd.ExecuteNonQuery(); cnn.Close(); aMember.MemberID = Convert.ToInt16(cmd.Parameters[" @MemberNo" ].Value); aMember = (JuvenileMember)this.GetMember(aMember.MemberID); } // Catch any SQL Errors catch (SqlException sqlex) { cnn.Close(); int sqlErr = Convert.ToInt32(cmd.Parameters[" @Return" ].Value); if (sqlex.Number == 50000) { switch (sqlex.State) { case 13: // Invalid member number throw new ArgumentOutOfRangeException(sqlex.Message); case 10: // Adult sponsor not found throw new LibraryException(ErrorCode.MissingAdultMember); case 11: // Add Juvenile member failed throw new LibraryException(ErrorCode.AddJuvenileFailed); default: throw new LibraryException(ErrorCode.GenericException, sqlex.Message, sqlex);<br />ADO.NET: Adding Juvenile to databaseContinued } } else { if (sqlex.Number == 8114) // ArgumentOutOfRange { throw new ArgumentOutOfRangeException(sqlex.Message); } else { throw new LibraryException(ErrorCode.GenericException, sqlex.Message, sqlex); } } } catch (Exception ex) { throw new LibraryException(ErrorCode.GenericException, ex.Message, ex); } } }}<br />Stored Procedure to Add Book (Phase III)The stored procedure checks if the ISBN is already in the database. If the ISBN exists then it will just add another copy to the Copy table. If the ISBN does not exist, then the title and author are added to the Title table first to get the Title_No and then the Title_No and ISBN are added to Item table. The last thing added is Copy_No to the Copy table.ALTER PROC [dbo].[AddBook]/************************************** * Add Book to Library * Add a new book if ISBN is not * found. If found it adds a new * copy number only * Parameters: * @ISBN - Number of item * @Title - Title of item * @Author - Author of item * Return: * @CopyNo - Copy number * * Author: John Elmer * Date: 06/26/2009 * *************************************/@ISBN int = 0,@Title varchar(63) = NULL,@Author varchar(31) = NULL,@CopyNo smallint OUTPUTASDECLARE @TitleNo int-- Check that ISBN is between 1 and 2,147,483,647IF @ISBN <= 0 OR @ISBN > 2147483647BEGINRAISERROR ('Invalid ISBN, must be between 1 and 2,147,483,647',14,13)RETURNEND-- Check if ISBN exists in ItemIF EXISTS (SELECT * FROM dbo.Item WHERE ISBN = @ISBN)BEGINBEGIN TRANDECLARE @TempCopyNo smallintSET @TempCopyNo = (SELECT MAX(Copy_No) FROMdbo.COPY WHERE ISBN = @ISBN) + 1SET @TitleNo = (SELECT I.Title_No FROM ITEM I WHEREI.ISBN = @ISBN)-- Add to Copy onlyINSERT dbo.CopyVALUES (@ISBN, @TempCopyNo, @TitleNo, 'N')IF @@ERROR <> 0BEGINROLLBACKRAISERROR ('Could not add Item', 14, 2)RETURN @@ERROREND<br />Stored Procedure to Add Book (Phase III)ContinuedSET @CopyNo = @TempCopyNoCOMMIT TRANEND-- ISBN does not existELSEBEGINIF @Title IS NULL OR @Author IS NULLBEGINRAISERROR ('Title and Author must be provided',14,2)RETURNENDBEGIN TRAN-- Insert into Title first to get Title_noINSERT dbo.TitleVALUES (@Title, @Author, NULL)IF @@ERROR <> 0BEGINROLLBACKRAISERROR ('Could not add Item', 14, 2)RETURN @@ERRORENDSET @TitleNo = @@IDENTITY-- Add to ItemINSERT dbo.ItemVALUES (@ISBN, @TitleNo, NULL, NULL, 'Y')IF @@ERROR <> 0BEGINROLLBACKRAISERROR ('Could not add Item', 14, 2)RETURN @@ERROREND-- Add to CopyINSERT dbo.CopyVALUES (@ISBN, 1, @TitleNo, 'N')IF @@ERROR <> 0BEGINROLLBACKRAISERROR ('Could not add Item', 14, 2)RETURN @@ERRORENDSET @CopyNo = 1COMMIT TRANEND<br />Member Information Screen (Windows)<br />Member Information Screen (Web)<br />