.NET Code Examples


Published on

  • 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

.NET Code Examples

  1. 1. <ul><li>.NET Portfolio </li></ul><ul><li>Gary Brenner </li></ul><ul><li>[email_address] </li></ul><ul><li>Phone (281) 300-5830 </li></ul>
  2. 2. Table of Contents <ul><li>Windows Front-End Application (Library Project phase 1) </li></ul><ul><li>Replace Data Access and Entity Layers(Library Project phase 2), with SQL Stored Procedures and our own Entities. </li></ul><ul><li>Create Web-Application to replace Windows Front-End(Library Project phase 3) </li></ul>
  3. 3. (Windows Front-End Application) <ul><li>Objective </li></ul><ul><li>A database has been created to support the principal functions of a lending library’s day-to-day operations: adding new members (adult and juvenile) and checking books in and out. An assembly has been created that contains classes and interfaces that provide access to the database for these functions. What is needed is a Windows Forms-based front-end application that will provide a librarian with a visual interface through which he or she may perform the desired functions. Creating that application will be your task. </li></ul><ul><li>Requirements </li></ul><ul><li>Design and develop a front end application that satisfies the four basic functionalities: Add Adult, Add Juvenile, Check In a book, Check Out a book. </li></ul><ul><li>Develop code that is easily maintainable. </li></ul><ul><li>Provide validation for all required fields (see details below). </li></ul><ul><li>Provide adequate error handling. </li></ul><ul><li>Produce a user interface that is intuitive, requiring minimal training for users while minimizing resource utilization. </li></ul>
  4. 4. Windows Front-End Add Adult Member Screen
  5. 5. (Code Sample for Add Adult Screen) <ul><li>public NewMemberForm() </li></ul><ul><li>{ </li></ul><ul><li>InitializeComponent(); </li></ul><ul><li>// set up validation regular expressions </li></ul><ul><li>// note: PatternAndMessage is a user-defined class </li></ul><ul><li>this.mTextBoxFirstName.Tag = new PatternAndMessage(@&quot;^[A-Z][a-z]{0,14}$&quot;, </li></ul><ul><li>&quot;First Name must be all letters, first uppercase, all others lowercase, length limited to 15 letters&quot;); </li></ul><ul><li>this.mTextBoxLastName.Tag = new PatternAndMessage(@&quot;^[A-Z][a-z]{0,14}$&quot;, </li></ul><ul><li>&quot;Last Name must be all letters, first uppercase, all others lowercase, length limited to 15 letters&quot;); </li></ul><ul><li>this.mTextBoxMiddleInitial.Tag = new PatternAndMessage(@&quot;^[A-Z]$&quot;, </li></ul><ul><li>&quot;Middle Initial is optional, if entered must be only 1 uppercase letter&quot;); </li></ul><ul><li>this.mTextBoxStreetAddress.Tag = new PatternAndMessage(@&quot;^.{1,15}$&quot;, </li></ul><ul><li>&quot;Street Address must be 1 to 15 characters long&quot;); </li></ul><ul><li>this.mTextBoxCity.Tag = new PatternAndMessage(@&quot;^.{1,15}$&quot;, </li></ul><ul><li>&quot;City must be 1 to 15 characters long&quot;); </li></ul><ul><li>} </li></ul>
  6. 6. (Windows Business & Data Access Replacements) <ul><li>Requirements </li></ul><ul><li>Design the Business and Data Access tiers </li></ul><ul><li>Develop code that is easily maintainable. </li></ul><ul><li>Provide adequate error handling. </li></ul><ul><li>Use database-programming techniques that provide maximum programming flexibility and </li></ul><ul><li>control while minimizing resource utilization. </li></ul><ul><li>Work with your instructor who will act as the project DBA for any required SQL help. </li></ul>
  7. 7. (Code Sample for SQL stored procedure : CheckInItem) <ul><li>/****** Object: StoredProcedure [dbo].[CheckInItem] Script Date: 08/21/2009 04:31:27 ******/ </li></ul><ul><li>SET ANSI_NULLS ON </li></ul><ul><li>GO </li></ul><ul><li>SET QUOTED_IDENTIFIER ON </li></ul><ul><li>GO </li></ul><ul><li>--stored proc CheckInItem </li></ul><ul><li>-- will add the item to the loanhist table and remove it from the load table if successful </li></ul><ul><li>CREATE Proc [dbo].[CheckInItem] </li></ul><ul><li>@ISBN int , </li></ul><ul><li>@Copy_No smallint </li></ul><ul><li>--@Title_No int, </li></ul><ul><li>--@On_Loan nchar(1) </li></ul><ul><li>As </li></ul><ul><li>Declare @Temp_Title_No int </li></ul><ul><li>Declare @Temp_Member_No smallint </li></ul><ul><li>Declare @Temp_Out_Date datetime </li></ul><ul><li>Declare @Temp_Due_Date datetime </li></ul><ul><li>Set @Temp_Title_No = NULL </li></ul><ul><li>Set @Temp_Member_No = NULL </li></ul><ul><li>Set NoCount ON </li></ul><ul><li>Begin Try </li></ul><ul><li>Begin Transaction </li></ul><ul><li>select @Temp_Title_No = title_no from copy where ISBN = @ISBN and </li></ul><ul><li>copy_no = @Copy_No and on_loan = 'Y' </li></ul><ul><li>if (@Temp_Title_No IS NULL) </li></ul><ul><li>Begin </li></ul><ul><li>Raiserror ('X108',13,1) -- item is not on loan </li></ul><ul><li>End </li></ul><ul><li>else </li></ul><ul><li>Begin </li></ul><ul><li>******************* Code is continued on next slide ***************************** </li></ul>
  8. 8. (Code Sample for SQL stored procedure : CheckInItem ) <ul><li>******************* Code is continued on below ***************************** </li></ul><ul><li>update copy set on_loan = 'N' where on_loan = 'Y' and ISBN = @ISBN and copy_no = @Copy_No and title_no = @Temp_Title_No </li></ul><ul><li>select @Temp_Member_No = member_no,@Temp_Out_Date = out_date, </li></ul><ul><li>@Temp_Due_Date = due_date from loan where isbn = @ISBN and copy_no = @Copy_No and title_no = @Temp_Title_No </li></ul><ul><li>if (@Temp_Member_No IS NULL) </li></ul><ul><li>Begin </li></ul><ul><li>Raiserror ('X107',13,1) -- item not found </li></ul><ul><li>End </li></ul><ul><li>else </li></ul><ul><li>Begin </li></ul><ul><li>delete from loan where member_no = @Temp_Member_No and out_date = @Temp_Out_Date </li></ul><ul><li>and due_date = @Temp_Due_Date and isbn = @ISBN and copy_no = @Copy_No and title_no = @Temp_Title_No </li></ul><ul><li>insert into loanhist(isbn,copy_no,out_date,title_no,member_no,due_date, </li></ul><ul><li>in_date,fine_assessed,fine_paid,fine_waived,remarks) </li></ul><ul><li>values(@isbn,@copy_no,@Temp_Out_Date,@Temp_Title_No,@Temp_Member_No,@Temp_Due_Date, </li></ul><ul><li>GetDate(),NULL,NULL,NULL,NULL) </li></ul><ul><li>End </li></ul><ul><li>End </li></ul><ul><li>if @@error <> 0 </li></ul><ul><li>Begin </li></ul><ul><li>Raiserror('X103',13,1) -- check in failed </li></ul><ul><li>End </li></ul><ul><li>COMMIT </li></ul><ul><li>End Try </li></ul><ul><li>BEGIN CATCH </li></ul><ul><li>-- there was an error </li></ul><ul><li>IF @@TRANCOUNT > 0 </li></ul><ul><li>ROLLBACK </li></ul><ul><li>-- Raise an error with the details of the exception </li></ul><ul><li>DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int --, @ErrNumber int </li></ul><ul><li>SELECT @ErrMsg = ERROR_MESSAGE(), </li></ul><ul><li>@ErrSeverity = ERROR_SEVERITY() </li></ul><ul><li>RAISERROR( @ErrMsg, @ErrSeverity, 1) </li></ul><ul><li>END CATCH </li></ul><ul><li>GO </li></ul>
  9. 9. (Web Application to replace Windows Front-End requirements) <ul><li>When displaying an adult’s information, the application should detect if the card is expired and give the librarian a chance to renew the card. Librarian must be able to choose whether or not to renew the card. The renewal date is today plus one year. Members can not check out books if the card is expired. </li></ul><ul><li>When dealing with juveniles, the application should detect if the juvenile is 18 years old or older and convert the member to an adult (row deleted in the juvenile table, row added to the adult table). This operation is not at the discretion of the librarian; i.e. the upgrade must take place automatically and the librarian must be notified that the updgrade has taken place. </li></ul><ul><li>Overdue books, shown in any display, must be highlighted. </li></ul><ul><li>The librarian must be able to enter a new book into the database. If the ISBN already exists in the database, all that is needed is to add a record for a new copy number. If the ISBN does not yet exist in the database, you must add all necessary records for the new ISBN and a new copy number 1 for that ISBN. </li></ul><ul><li>Use of hyperlinks to navigate between pages. </li></ul><ul><li>Project locations and requirements: </li></ul><ul><li>Create your web application project as a File System project. Your solution folder must contain all your project folders so that when you submit the solution folder all necessary parts of your application are present. Your solution must include the source for all your projects; i.e. not just the DLLs for your business tier and data access tier projects. </li></ul>
  10. 10. (Add Adult Code Sample ASPX) <ul><li>public partial class AddAdult : System.Web.UI.Page </li></ul><ul><li>{ </li></ul><ul><li>protected void Page_Load(object sender, EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>protected void btnAddAdult_Click(object sender, EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>AdultMember newAdultMember = new AdultMember(); </li></ul><ul><li>newAdultMember.FirstName = txtFirstName.Text; </li></ul><ul><li>newAdultMember.MiddleInitial = txtMiddleInitial.Text; </li></ul><ul><li>newAdultMember.LastName = txtLastName.Text; </li></ul><ul><li>newAdultMember.Street = txtStreet.Text; </li></ul><ul><li>newAdultMember.City = txtCity.Text; </li></ul><ul><li>newAdultMember.State = statesDropDown.Text; </li></ul><ul><li>newAdultMember.ZipCode = txtZipCode.Text; </li></ul><ul><li>newAdultMember.PhoneNumber = txtPhoneNumber.Text; </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>BusinessLayer bl = new BusinessLayer(); </li></ul><ul><li>bl.AddAdult(newAdultMember); </li></ul><ul><li>// MessageBox.Show(&quot;Added &quot;, newAdultMember.MemberID.ToString()); </li></ul><ul><li>LitMemberInfo.Text = (&quot;Added adult &quot; + newAdultMember.MemberID.ToString()); </li></ul><ul><li>} </li></ul><ul><li>catch (LibException lex) </li></ul><ul><li>{ </li></ul><ul><li>switch (lex.LibErrorCode) </li></ul>
  11. 11. (Add Adult Code Sample ASPX) <ul><li>{ </li></ul><ul><li>case LibErrorCode.GenericException: </li></ul><ul><li>LitMemberInfo.Text = &quot;Some sort of generic error need to see.&quot;; </li></ul><ul><li>break; </li></ul><ul><li>case LibErrorCode.AddAdultFailed: </li></ul><ul><li>LitMemberInfo.Text = &quot;This adult member was not added.&quot;; </li></ul><ul><li>break; </li></ul><ul><li>case LibErrorCode.None: </li></ul><ul><li>LitMemberInfo.Text = &quot;No error code was provided, sorry.&quot;; </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>LitMemberInfo.Text = &quot;What happened?&quot;; </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>protected void btnCancel_Click(object sender, EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>txtFirstName.Text = &quot;&quot;; </li></ul><ul><li>txtMiddleInitial.Text = &quot;&quot;; </li></ul><ul><li>txtLastName.Text = &quot;&quot;; </li></ul><ul><li>txtStreet.Text = &quot;&quot;; </li></ul><ul><li>txtCity.Text = &quot;&quot;; </li></ul><ul><li>txtZipCode.Text = &quot;&quot;; </li></ul><ul><li>txtPhoneNumber.Text = &quot;&quot;; </li></ul><ul><li>} </li></ul>