Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 45 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
45
Dung lượng
567,47 KB
Nội dung
Dim conn As New SqlConnection(CONNSTR) Dim sSQL As String ' Make sure we are using a valid date. If calStart.SelectedDate() < Now Then lblResult.ForeColor = System.Drawing.Color.Red lblResult.Text = "You must select a date equal to or " & _ "later than today." Exit Sub End If ' Prepare our INSERT statement that will save the request. sSQL = "INSERT INTO VacationRequest (EmpID, StartDate, " & _ "Hours, Approved) " & _ "VALUES (" & eID & ", '" & _ calStart.SelectedDate.ToString() & "', " & _ CInt(tbHours.Text) & ", 0)" ' Attemp the INSERT. Dim cmd As New SqlCommand(sSQL, conn) Try conn.Open() cmd.ExecuteNonQuery() conn.Close() lblResult.ForeColor = System.Drawing.Color.RoyalBlue lblResult.Text = "Vacation request was successfully " & _ "submitted (" & calStart.SelectedDate() & ", " & _ "tbHours.Text & " hours)." Catch ex As SqlException conn.Close() lblResult.ForeColor = System.Drawing.Color.Red lblResult.Text = "There was a database error: " & ex.Message() End Try End Sub ' If the user changes the selection on the calendar, we want ' to make sure it is today or later. If not, tell the user. Private Sub calStart_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calStart.SelectionChanged ' Make sure we are using a valid date. If calStart.SelectedDate() < Now.Date Then lblResult.ForeColor = System.Drawing.Color.Red lblResult.Text = "You must select a date equal to or " & _ "later than today." 446 Project 10 Else lblResult.Text = "" End If End Sub End Class We define our connection string, as usual, but this time we actually have some class data to store. All the class variables are related to saving the various vacation statistics, as well as the employee ID once we load it from the database. Then we handle the page load event. We start it out by retrieving some employee information from the database, including the employee ID, the first and last names, and the amount of vacation the employee is allotted for the year. We’ll use these shortly because we need them before we can get any work done. We use standard SQL and ADO.NET to get the information back. Once we get the data back, we fill in the user name and total vacation hours on the form. Next we must load all the vacation request records for the user for several reasons. First, we need to iterate through them all, totaling the amount of vacation used this year. Second, we will hook the list of records up to the DataGrid so that users can see the history of all their requests. We retrieve the vacation date, hours used, and approval state for all the records that match the employee ID. Once all the records are loaded, we iterate through them using a For Each loop total- ing the amount of vacation used. This is stored in the iUsed class variable. After the loop completes, we calculate the amount of vacation remaining and store that. Those values are then filled into the form. The amount of vacation used is calculated in this application based only on approved hours. Any stored requests that are not approved yet do not count toward the used total. This may not suit your needs, and it is very easy to change. Simply remove the condition in the For Each loop that checks the approval status. If the total amount of vacation used or remaining looks wrong, it’s probably because the unapproved requests were not counted. The last thing we do in the page load handler is to configure the validator controls. We wait until now because we don’t know the maximum value that should be allowed until we calculate the amount of vacation remaining for the employee. Once we do, we can set the MaximumValue property and the error messages, all of which reference the total remaining vacation hours. Our next function handles the Submit Request button that indicates that the user is saving a new vacation request. We first check to make sure that the date the user has selected in the calendar is equal to or later than today’s date. It does not make sense to retroactively request vacation. If the date is invalid, we tell the user and exit the Procedure. Employee Intranet with .NET 447 TEAMFLY Team-Fly ® We handle errors in this application in one of two ways. You’ve already seen the validator controls that check input as the user enters it. However, what do you do if you have an error in the code? You can’t display a message box because you are running on the server. I decided to create a label control called lblResult that initially has no text value and is thus invisible. If we encounter an error in the code during a Try Catch construct, we set the text of the label to describe the error and change its color to red. We also use the same label to report that a requested operation succeeded. If everything goes well, we set the text to indicate this and change the color to a friendlier blue. You can use the same technique anywhere, and we do for each form in the application. As long as the start date is okay and we have a valid number of hours requested, we go ahead and attempt to INSERT the request data into the database. If all goes well, we tell the user. If something goes wrong, we also tell the user. And that’s it for saving the request. Try It Out You should be able to run the application now and access the vacation planning page. Figure 10.10 shows the page running in the browser. Run around the form and try a few things. First, you can verify that the calculations are being done correctly. Then you can attempt to submit a vacation request. Select an old date and see that you get an error reported properly, as shown in Figure 10.11. Save the request and make sure it gets put into the database. Figure 10.10 The vacation planning page in the browser. 448 Project 10 Figure 10.11 The vacation planning page, with error. The Employee Information Form Most of your employees will typically want to make sure you have the right informa- tion or occasionally to edit the information. It can save time when employees need to verify their salary or emergency contact if they can do it themselves without having to bother your other staff to get the information. So we created this page to allow the user to do just that. Start by creating a new Web form and naming it prj10empinfo.aspx. Enter the fol- lowing HTML to create the form: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="prj10empinfo.aspx.vb" Inherits="prj10.prj10empinfo"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title></title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR"> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> Employee Intranet with .NET 449 <LINK href="http://localhost/prj10/Styles.css" type="text/css" rel="stylesheet"> </HEAD> <body style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"> <form id="frmStatus" method="post" runat="server"> <table cellSpacing="5" cellPadding="0" width="700" border="0"> <tr> <td align="left" colSpan="4"> <asp:image id="Image1" runat="server" ImageUrl="images/EmployeeInfoHdr.jpg"> </asp:image> </td> </tr> <tr> <td class="TitleContent"> Review the information that company has on file about you. You can make a few changes as well, should you need to. </td> </tr> <tr> <td class="DescTitle" style="PADDING-LEFT: 10px"> Information Summary </td> </tr> <tr> <td> <table style="PADDING-LEFT: 10px" cellSpacing="0" cellPadding="10" width="700" border="0"> <tr height="40"> <td class="Content" vAlign="middle" align="right" width="25%"> First Name: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblFirstName" runat="server" Font-Bold="True"></asp:label> </td> <td class="Content" vAlign="middle" align="right" width="25%"> Annual Salary: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblSalary" runat="server" Font-Bold="True"></asp:label> </td> </tr> <tr height="40"> 450 Project 10 <td class="Content" vAlign="middle" align="right" width="25%"> Last Name: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblLastName" runat="server" Font-Bold="True"></asp:label> </td> <td class="Content" vAlign="middle" align="right" width="25%"> Vacation Hours: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblHours" runat="server" Font-Bold="True"></asp:label> </td> </tr> <tr height="40"> <td class="Content" vAlign="middle" align="right" width="25%"> Title: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblTitle" runat="server" Font-Bold="True"></asp:label> </td> <td class="Content" vAlign="middle" align="right" width="25%"> Emergency Contact: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:TextBox id="tbContact" runat="server" Font-Bold="True"></asp:TextBox> </td> </tr> <tr height="40"> <td class="Content" vAlign="middle" align="right" width="25%"> Department: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblDept" runat="server" Font-Bold="True"></asp:label> </td> <td class="Content" vAlign="middle" align="right" width="25%"> Employee Intranet with .NET 451 Login ID: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblLogin" runat="server" Font-Bold="True"></asp:label> </td> </tr> <tr height="40"> <td class="Content" vAlign="middle" align="right" width="25%"> Gender: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblGender" runat="server" Font-Bold="True"></asp:label> </td> <td class="Content" vAlign="middle" align="right" width="25%"> Street 1: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblStreet1" runat="server" Font-Bold="True"></asp:label> </td> </tr> <tr height="40"> <td class="Content" vAlign="middle" align="right" width="25%"> Home Phone: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:TextBox id="tbHomePhone" runat="server" Font-Bold="True"></asp:TextBox> </td> <td class="Content" vAlign="middle" align="right" width="25%"> Street 2: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:Label id="lblStreet2" runat="server" Font-Bold="True"></asp:Label> </td> </tr> <tr height="40"> <td class="Content" vAlign="middle" align="right" width="25%"> 452 Project 10 Extension: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblExt" runat="server" Font- Bold="True"></asp:label> </td> <td class="Content" vAlign="middle" align="right" width="25%"> City, State, ZIP: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblCityStateZip" runat="server" Font-Bold="True"></asp:label> </td> </tr> <tr height="40"> <td class="Content" vAlign="middle" align="right" width="25%"> </td> <td class="Content" vAlign="middle" align="left" width="25%"> </td> <td class="Content" vAlign="middle" align="right" width="25%"> Password: </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:textbox id="tbPwd" runat="server" TextMode="Password" Font-Bold="True"></asp:textbox> </td> </tr> <tr height="40"> <td class="Content" vAlign="middle" align="left" width="25%"> <IMG src="images/bullet1.gif"><A href="default.aspx"> Back to Main Page</A> </td> <td class="Content" vAlign="middle" align="left" width="25%"> </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:label id="lblResult" runat="server" ForeColor="Red"></asp:label> </td> <td class="Content" vAlign="middle" align="left" width="25%"> <asp:Button id="btnSubmit" runat="server" Employee Intranet with .NET 453 Text="Save Changes"></asp:Button> </td> </tr> </table> </td> </tr> </table> </form> </body> </HTML> That was our biggest listing yet, primarily due to the number of controls on the page. They are detailed in Table 10.4, and Figure 10.12 shows what the form should look like in the Designer. Most of these are label controls used to display data and field names. There are three edit fields and a button to break up the monotony a little. Table 10.4 The Employee Information Controls CONTROL NAME PROPERTIES Button btnSubmit Text=”Save Changes” Image Image1 ImageURL=”images/EmployeeInfoHdr.jpg” Label lblFirstName Text=””, Font-Bold=True Label lblLastName Text=””, Font-Bold=True Label lblSalary Text=””, Font-Bold=True Label lblTitle Text=””, Font-Bold=True Label lblDept Text=””, Font-Bold=True Label lblLogin Text=””, Font-Bold=True Label lblGender Text=””, Font-Bold=True Label lblStreet1 Text=””, Font-Bold=True Label lblStreet2 Text=””, Font-Bold=True Label lblExt Text=””, Font-Bold=True Label lblCityStateZip Text=””, Font-Bold=True Label lblResult Text=””, Font-Bold=True Label lblHours Text=””, Font-Bold=True TextBox tbPwd Text=””, Font-Bold=True, TextMode=Password TextBox tbContact Text=””, Font-Bold=True TextBox tbHomePhone Text=””, Font-Bold=True 454 Project 10 Figure 10.12 The employee information form in the Designer. We begin the code in our usual fashion, with a database connection string and any class data we might need. In this case, we need something to keep track of the employee ID and the password, once we pull them out of the database: #Region "Class Data and Types " Private Const CONNSTR As String = "PERSIST SECURITY INFO=False; " & _ "DATA SOURCE=tony; INITIAL CATALOG=prj10; UID=sa; PWD=;" Private eID As Int32 = 0 Private sPwd As String = "" #End Region The Page Load event takes care of loading the employee information and filling it into the labels on the form. Here’s the code: Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim conn As SqlConnection = New SqlConnection(CONNSTR) Dim sSQL As String Dim s As String Employee Intranet with .NET 455 [...]... status reports Like vacation requests, status reports must be reviewed by managers Create a page that would allow managers to look at the status reports for all the employees associated with them You could optionally create a special page that managers use to create their status reports It would show all the reports of the employees associated with them so that they could review their department status... tools, 47–55 DataBindings collection, Binding object, 55–56 DataForm wizard, 47–49 Data Generator DataAdapter object, 321 described, 315 distributor’s database layout, 316–317 Imports statement, 319 Main form, 316–318 setup code, 322–325 SQLSELECT statement, 321 Try.Catch construct, 321 Data Grid, 218–219 DataList control, 224–227 data navigation, 69–71 Data Reader Controls, 327, 336 database, 326 described,... data manipulation methods, 43–46 Rows collection, 42–43 sending results to database, 45–46 Tables collection, 42–43 DataSets, Web service return values, 97–98 DataTables, 42 Data Transformation Services (DTS) wizard, 92–95 data types, 5, 297–298, 300–302 DateFormat enumeration, 263–264 debug code, standards, 5 debugging, 115–116, 417 declarations ASP.NET server control tag, 202 BoundColumn, 252 Data... 328–335 sValidateErrs string, 328 UI updates, 334 validation, 328–329 ValidationCallBack function, 331 Write Data button, 332–333 DataReader control, Web portal, 226 DataReader object, 39 data readers, 227 data source, Web services definition, 96 data structures, 300–303 DataRelation object, 39, 46–47 DataSet object accepting/rejecting changes, 45–46 adding multiple result sets, 41 ADO.NET, 38 Columns... 255 INSERT statement, 257–258 TableCell object, 256–257 Index UpdateCommand event, 255–256 Department News BindDataList routine, 264–265 BindPriority function implementation, 271 calendar declarations, 269–270 calendar labels, 269–270 CommandArgument attribute, 261 CommandName attribute, 261 DataList declaration, 261 DataListItem object, 266 date range selections, 269 DateFormat enumeration, 263–264... Figure 10. 13 The employee information form in the browser 459 460 Project 10 Figure 10. 14 The status report form in the Designer The Status Report Form The status report form is probably the simplest in the application It has a couple text fields to accept and display status report information, and a button to save it That’s it! Take a look at Figure 10. 14 to see what it looks like sitting in the Designer... object ADO.NET, 56 data navigation, 69–71 Binding object, 55–56 Boolean properties, 14 BoundColumn declarations, 252 bug reporting client forms, 103 –115 debugging, 115–116 frmBug form, 104 109 frmMain form, 112–114 frmSeeBugs form, 109 –112 Web service responsibilities, 91 bug tracking ADO.NET architecture, 38–40 ADO.NET development history, 35 ADO vs ADO.NET, 37–38 data access setup, 40 database design,... routine, 247–258 custom actions application installers, 186–187 installation component, 162 Custom Actions Editor, 164, 170 custom styles, main form, 427–428 customized UI information, 292 D data access, ADO.NET setup, 40 DataAdapter object accepting/rejecting changes, 45–46 ADO.NET, 38 data specifications, 41 473 474 Index DataAdapter wizard, Visual Studio NET, 49–52 databases accepting/rejecting changes,... methods, 40 Data Generator, 315 Data Reader, 326 data retrieval, 41 function name listing, 136 intranet, 422424 Remoting, 130–131 retrieving requested rows, 226 sending user data to, 99 101 table relationships, 46–47 table row addition/ deletion, 45 updating, 45–46 user login, 349 Web portal, 211–212 Web services, 91–95 data binding Mobile Internet Toolkit, 386–390 Visual Basic NET methods, 55–56 Visual. .. not complicated; it just looks that way due to the amount of data on the page Try It Out Run the application and access the employee information page Once the page is displayed, verify that the correct data is being loaded for the particular user you are impersonating Try out the update for a field or two to make sure it works The form is shown executing in the browser in Figure 10. 13 Figure 10. 13 The . gets put into the database. Figure 10. 10 The vacation planning page in the browser. 448 Project 10 Figure 10. 11 The vacation planning page, with error. The Employee Information Form Most of your. 10. 13. Figure 10. 13 The employee information form in the browser. Employee Intranet with .NET 459 Figure 10. 14 The status report form in the Designer. The Status Report Form The status report form. verify that the calculations are being done correctly. Then you can attempt to submit a vacation request. Select an old date and see that you get an error reported properly, as shown in Figure 10. 11.