1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual Basic .NET at Work Building 10 Enterprise Projects phần 9 ppt

52 217 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Preparing Your Development Environment We’re getting close to the project, so we’d better make sure that your development environment is ready to go. There are a few things you’ll need to get set up before any actual coding takes place. You’ll also need to get the SDK installed and your emulator running. You might actually want to try things out on a real mobile device, too. Install the SDK This is pretty simple, but it has to happen before you can do any mobile Web develop- ment. Download the Mobile Internet Toolkit SDK from Microsoft. It’s actually fairly compact, given what it does for you. You must have Visual Studio .NET installed prior to installing the SDK. When you have downloaded and installed the SDK, you will have access to the language extensions, the object libraries, the mobile Web controls, and the documentation. Install the Emulator Testing your application on a real mobile device is not only cumbersome and difficult to debug, but it can also be expensive, given the current mobile Internet access rates. Therefore, most of the mini-browser manufacturers, including Microsoft, have created emulators that run on your PC and act just like the device. The easiest and most generic one to use is the MME. You can download this from Microsoft at no cost. You have already seen it in the illustrations used for this project. Simply install it and Visual Studio will list it in the available browsers in the Browse With context menu in the Solution Explorer. The emulator is easy to use, and it integrates very nicely with Visual Studio .NET. It works with the debugging system directly, and you can even dock it into your Visual Studio environment. The MME was used for all the testing and execution of all the code for this project. If you are targeting a specific device, or would like to test your application on some specific devices, you might try looking at manufacturers’ Web sites. Many manufac- turers of cell phones or browsers provide emulators for their own hardware or soft- ware. You can usually download them at no cost. For example, Phone.com makes one of the more popular cell phone browsers and has an SDK and emulator of its own. You can even download skins for the emulator that look exactly like specific cell phone models. Using a Real Mobile Device Eventually you will want to make sure that your mobile Web application actually works on a real device. When you create a mobile Web application, Visual Studio cre- ates the project directly in IIS. If the machine on which you develop the application can be connected to the Internet as a server, you can simply add a bookmark to it in your cell phone that points to your machine’s IP address and the project directory. If not, 394 Project 9 you’ll have to post your application to a server that has IIS 5.0 and the Mobile Internet Toolkit SDK installed and also has an Internet connection. Let’s Start the Project Finally we arrive at the project that we’ll be building using the Mobile Internet Toolkit SDK and Visual Basic. You will be making use of a fair amount of what you have learned so far when we created our Employee Information Application. It is targeted toward cell phones but should also work well with a PDA. Here are the features we will implement: Weather alert. If other companies are like mine, they are occasionally closed due to inclement weather. I normally have to call a phone number, a number that I can never remember, to check whether or not I should come in to work. There- fore I have provided a feature that anyone with a Web-enabled cell phone can access. It simply displays the company status for today, open or closed, with any message you would like to attach. Announcements. Companies make many announcements to employees, and it is important that the employees have access to them. This feature simply displays those announcements on their cell phone. Company Events. Companies have all kinds of events, from holiday parties to massive layoffs. Whatever the case, employees need to know about them. This feature allows users to select a date from a Calendar control, and all events for the month in which their selected dates falls are displayed. Next holiday. This feature is likely to be extremely popular. It looks at today’s date and displays the next paid company holiday. Phone lookup. This feature is an online employee phone book. Users can enter an employee’s last name, or the first part of a last name, and it will return any matches in a list. They can then see the entire record, including first and last name, phone number, and extension (if any). The best part, though, is that users can highlight the desired person and directly call the number without dialing it. Design of the Application It would be fairly easy to build this application in one big ASP.NET page with embed- ded code. That is not an ideal application design because it would be large, resulting in long download times. In addition, it would not be terribly reusable. So instead I have opted to break out the business functionality, largely database access routines, into a business component that we will compile as a DLL from a Class Library. UI code will go into the ASP page, which will contain all our forms on one page. The way I’ve split the business functionality out, you could easily use the same component for the back end of a normal Web site, such as your company’s intranet site. We need to create a database as well. It’s a simple database with five tables and no relationships. The design of the tables is illustrated in Figure 9.11. Employee Information System Using the Mobile Toolkit 395 Figure 9.11 The database design. Our application was written for Microsoft SQL Server. There is a Microsoft Access database on the accompanying CD-ROM that you can either import into SQL Server or change the code (particularly the connection string and the data access objects) so that you can read the Access database instead. Part 1: The Business Component The business component we’re building has five methods in it, all of which perform essentially the same function: data retrieval. Some of the functions require special logic, most of which has been implemented in the SQL statements. I’ll point it out when I use it. For now, let’s just look at all the code at once, and then I’ll discuss a few of the more interesting features. Create a new project in Visual Studio, a standard Class Library. I called mine prj09logic. Rename the default class module MobileSvcs. Now we’re ready to add code. The following listing is the complete content of the class: Option Strict On Option Explicit On Imports System.Data.SqlClient Public Class MobileSvcs #Region " Class Data and Types " ' Database connection string. We're using SQL Server. Change this if ' you want to use something else, like the Access db included with ' the book. Private Const CONNSTR As String = "PERSIST SECURITY INFO=False; " & _ 396 Project 9 "DATA SOURCE=tony; INITIAL CATALOG=prj09; UID=sa; PWD=;" #End Region #Region " User Code " ' This method retrieves the weather alert record from the database. Public Function GetWeatherAlert() As DataSet Dim conn As SqlConnection = New SqlConnection(CONNSTR) Dim sSQL As String ' SQL to retrieve alert rows that are current. By design, ' only one row is ever intended to be in there, reflecting ' the current state of the company. sSQL = "SELECT * FROM WeatherAlert WHERE AlertDate <= '" & _ Now & "'" ' Get our data objects ready for retrieval. Dim da As SqlDataAdapter = New SqlDataAdapter(sSQL, conn) Dim ds As DataSet = New DataSet() ' Load the data. Try conn.Open() da.Fill(ds, "Alerts") conn.Close() GetWeatherAlert = ds Catch ex As SqlException GetWeatherAlert = Nothing End Try End Function ' This method loads announcement data from the database. Public Function GetAnnouncements() As DataSet Dim conn As SqlConnection = New SqlConnection(CONNSTR) Dim sSQL As String ' In this case, we simply retrieve all rows. It is assumed that ' the data will be properly maintained, with old records being ' removed on a regular basis. ' We added the DESC clause to bring the events back with the ' most recent events coming in first. sSQL = "SELECT * FROM Announcement ORDER BY AnnounceDate DESC" ' Get our data objects ready for retrieval. Dim da As SqlDataAdapter = New SqlDataAdapter(sSQL, conn) Employee Information System Using the Mobile Toolkit 397 TEAMFLY Team-Fly ® Dim ds As DataSet = New DataSet() ' Load the data. Try conn.Open() da.Fill(ds, "Announcements") conn.Close() GetAnnouncements = ds Catch ex As SqlException GetAnnouncements = Nothing End Try End Function ' This method loads a list of matching employee phone ' records from the database. The sName parameter is the ' user-entered employee last name. Public Function PhoneLookup(ByVal sName As String) As DataSet Dim conn As SqlConnection = New SqlConnection(CONNSTR) Dim sSQL As String ' The SQL loads data based on a complete or partial match of ' the first part of the last name. Ex: If the user enters a ' value of "POW", the SQL will return "Powers" and "Powell". sSQL = "SELECT EmpLastName, EmpFirstName, Phone, Extension " & _ "FROM Phone " & _ "WHERE EmpLastName LIKE '" & sName & "%' " & _ "ORDER BY EmpLastName" ' Get our data objects ready for retrieval. Dim da As SqlDataAdapter = New SqlDataAdapter(sSQL, conn) Dim ds As DataSet = New DataSet() ' Load the data. Try conn.Open() da.Fill(ds, "PhoneNums") conn.Close() PhoneLookup = ds Catch ex As SqlException PhoneLookup = Nothing End Try End Function ' This method loads the next available holiday from ' the database. Public Function GetHoliday() As DataSet Dim conn As SqlConnection = New SqlConnection(CONNSTR) 398 Project 9 Dim sSQL As String ' This SQL will get the first record from a list of holidays ' that have a date following the current date. sSQL = "SELECT TOP 1 HolidayDate, Holiday " & _ "FROM Holiday WHERE HolidayDate >= '" & _ Now & "' ORDER BY HolidayDate" ' Get our data objects ready to load. Dim da As SqlDataAdapter = New SqlDataAdapter(sSQL, conn) Dim ds As DataSet = New DataSet() ' Load the data. Try conn.Open() da.Fill(ds, "Holiday") conn.Close() GetHoliday = ds Catch ex As SqlException GetHoliday = Nothing End Try End Function ' This method loads event data from the database. The parameter ' is the user-selected date to match. Public Function GetEvents(ByVal sDate As String) As DataSet Dim conn As SqlConnection = New SqlConnection(CONNSTR) Dim sSQL As String ' This SQL loads all event records whose month and year match ' the month and year of the date the user selected. The effect ' is that the users will get all the events for the month ' of the date that they entered. sSQL = "SELECT EventDate, EventName, EventDesc " & _ "FROM Event " & _ "WHERE (MONTH(EventDate)=MONTH('" & sDate & "')) " & _ "AND (YEAR(EventDate)=YEAR('" & sDate & "')) " & _ "ORDER BY EventDate" ' Get our data objects ready to load. Dim da As SqlDataAdapter = New SqlDataAdapter(sSQL, conn) Dim ds As DataSet = New DataSet() ' Load the data. Try conn.Open() Employee Information System Using the Mobile Toolkit 399 da.Fill(ds, "Events") conn.Close() GetEvents = ds Catch ex As SqlException GetEvents = Nothing End Try End Function #End Region End Class That’s quite a listing. I promise we won’t be going over every excruciating detail. We’re only going to touch on a few of the more interesting parts. The basic idea of each of the five methods is to define a SQL statement to retrieve the data requested, fill a DataSet, and then return it to the caller. The first method, GetWeatherAlert, is pretty simple. It assumes that there is only one record in the database table it reads, representing the current state of the company. You can change that single row as required to reflect the actual state of your company. The method GetAnnouncements is also fairly straightforward. The only interesting part is the DESC at the end of the SQL statement. It returns announcements so that the most recent come in first. The PhoneLookup method is slightly more interesting. The functionality returns a list of rows whose associated last name matches a string that the user has entered. However, keeping with the practice of reducing the amount of data the user has to input, we look up matches based on the first part of the last name. For example, enter- ing POW will return both Powell and Powers. We do this using the LIKE clause and the wildcard character, like this: "WHERE EmpLastName LIKE '" & sName & "%' All the results from the query are stuffed into a DataSet and returned to the caller. The GetHoliday method is supposed to return the next paid holiday that the com- pany has. It’s easy to create a SQL statement to retrieve all the holidays after a certain date. We could write some code to then pull out the most recent one from the returned rows. However, we can do all the work in the SQL like this: sSQL = "SELECT TOP 1 HolidayDate, Holiday " & _ "FROM Holiday WHERE HolidayDate >= '" & _ Now & "' ORDER BY HolidayDate" The Top 1 clause returns the first row in the result set. As long as we have the ORDER BY clause at the end, which makes sure that the holidays come in the right order, everything works properly. We then return the top row in a DataSet. Lastly, we have GetEvents. This method takes a date that the user has entered and retrieves all event rows from the database that fall in the same month as the passed-in date. The SQL looks a little more complex, but it really isn’t: 400 Project 9 sSQL = "SELECT EventDate, EventName, EventDesc " & _ "FROM Event " & _ "WHERE (MONTH(EventDate)=MONTH('" & sDate & "')) " & _ "AND (YEAR(EventDate)=YEAR('" & sDate & "')) " & _ "ORDER BY EventDate" All we’re doing here is extracting the MONTH part of the EventDate column and the passed-in date and making sure they’re the same. We do the same thing with the YEAR, in case there are old or future rows in the database. And that’s about it for the business component. You can see how it would be very easy to use the exact same component to present the same information using a different venue, such as the company’s intranet site. When you’ve got all this done (or loaded it from the CD-ROM), build the DLL and note where it is so that we can reference it shortly. Part 2: The Mobile Web Application Start this part of the project by creating a new mobile Web project. I called it prj09. Once the project is created, you’ll be looking at a default form, ready for your ministrations. Rename the form default.aspx so that it will load automatically. You can create all the forms yourself or load the project from the accompanying CD-ROM. Figures 9.12 and 9.13 show what all the forms look like and the controls you’ll want to drop on them. Table 9.5 also lists each control and any relevant properties that need to be set. Figure 9.12 The mobile Web application forms, part 1. Employee Information System Using the Mobile Toolkit 401 Figure 9.13 The mobile Web application forms, part 2. Table 9.5 The Controls on the Forms FORM CONTROLS PROPERTIES frmMain Image ID=Image1, ImageURL=images/mobile_logo.gif, Alignment=Center List ID=lstMenu frmWeather Image ID=Image2, ImageURL=images/mobile_logo.gif, Alignment=Center Label ID=Label1, Text=”Company Status”, StyleReference=Title Label ID=lbllblDate Label ID=lblAlertTitle Label ID=lblStatus Command ID=cmdHome, Text=”Home” 402 Project 9 FORM CONTROLS PROPERTIES frmAnnounce Form Paginate=True Image ID=Image3, ImageURL=images/mobile_logo.gif, Alignment=Center Label ID=Label4, Text=”Announcements”, StyleReference=Title TextView ID=tvAnnounce Command ID=cmdHome2, Text=”Home” frmEvents Image ID=Image4, ImageURL=images/mobile_logo.gif, Alignment=Center Label ID=Label8, Text=”Select event date:” Calendar ID=calEvents, Alignment=Center Command ID=cmdHome3, Text=”Home” frmEvents2 Image ID=Image5, ImageURL=images/mobile_logo.gif, Alignment=Center Label ID=Label5, Text=”Events”, StyleReference=Title TextView ID=tvEvents Command ID=cmdHome4, Text=”Home” frmPhone Image ID=Image5, ImageURL=images/mobile_logo.gif, Alignment=Center Label ID=Label6, Text=”Phone List”, StyleReference=Title TextBox ID=txtName, Alignment=Left, Size=10, MaxLength=20 Command ID=cmdLookup, Text=”Lookup” Command ID=cmdHome5, Text=”Home” frmPhone2 Image ID=Image7, ImageURL=images/mobile_logo.gif, Alignment=Center Label ID=Label7, Text=”Phone List”, StyleReference=Title Label ID=lblPhoneErr, StyleReference=Error, Visible=False ObjectList ID=lstPhoneNums, Alignment=Left Command ID=cmdHome6, Text=”Home” continues Employee Information System Using the Mobile Toolkit 403 [...]... we created works for any application, and you could easily display the useful data it supplies on a corporate intranet Add a status or information page that shows it to everyone all the time Create a content management Web site This mobile Web application depends on current data to be useful, but the people in a company are not going to maintain the data if it isn’t easy In particular, the weather... the user Our code is as follows: ' This method retrieves data about the weather status of the company ' and displays it for the user Private Sub ShowWeatherAlert() Dim o As New prj09logic.MobileSvcs() Dim ds As DataSet Dim theRow As DataRow ' Our business component ' Holds returned data ' Holds a single row ' Get data about the weather alert status from our ' business component There will be by design... 9. 14 shows what the main menu looks like running in the emulator 405 406 Project 9 Figure 9. 14 The main menu seen in the emulator The Weather Alert This feature is one of the simpler bits of functionality in this application, but it bears some scrutiny because we’ve patterned all our data retrieval methods after this one We will use the business object we created earlier to access the data we need and... Microsoft NET Framework and Visual Basic NET Each project looked at a specific technique This project will bring several of them together to create a solution that just about any company could use, given some adaptation It will make it easier for individuals to deal with overhead tasks that almost every company has 4 19 420 Project 10 THE PROBLEM: Most companies have overhead work that employees must... TextView control to present the data to the user because we can format the text a little more than a label allows It’s possible that this DataSet could be large, so we make sure that the automatic pagination is turned on, through the TextView properties, for the TextView control The code for the Announcement retrieval is similar to that of the weather alert feature We load some data and present it to the... is returned, we use data binding to fill the data retrieved into the ObjectList We use an object list because we want to attach complete data rows to the list The DataSource is set to the DataSet that we got back from the business object, and the DataMember property is set to the particular table in the DataSet that we want to use (in this case it is the only table) Then we call DataBind and everything... to see how much vacation time they have left this year and the past history of their vacation and to create new vacation requests 6 The employee information form Users will be able to see the basic information that the company keeps on them, review it, and change some of it if they need to 7 The status reports form Most companies require employees to create weekly (or otherwise) status reports This... 424 Project 10 Table 10. 1 The Database Tables TABLE DESCRIPTION Employee The heart of the database Most of the data is stored here, and all other tables relate to it It also contains the employee’s login and password information for use in this application, which makes it easy for use to access that information OfTheDay This table stores our repository of jokes, quotes, and fortunes that will be displayed... of the form We place everything into a Try Catch construct to make sure that we handle any errors that might crop up Once the data is filled in, we make the form active and we’re done Figure 9. 15 shows the Weather Alert screen Figure 9. 15 The Weather Alert feature Team-Fly® 407 408 Project 9 The Announcements The Announcements feature retrieves a complete list of all rows in the Announcement table and... call ds = o.GetWeatherAlert() ' If we got data back, then extract it from the ' DataSet and stuff it into our controls Employee Information System Using the Mobile Toolkit Try theRow = ds.Tables(0).Rows(0) lblDate.Text = CStr(theRow("AlertDate")) lblAlertTitle.Text = CStr(theRow("AlertTitle")) & " -" lblStatus.Text = CStr(theRow("AlertText")) Catch ex As Exception lblDate.Text = "No data available." . arrive at the project that we’ll be building using the Mobile Internet Toolkit SDK and Visual Basic. You will be making use of a fair amount of what you have learned so far when we created our. intranet site. We need to create a database as well. It’s a simple database with five tables and no relationships. The design of the tables is illustrated in Figure 9. 11. Employee Information. errors that might crop up. Once the data is filled in, we make the form active and we’re done. Figure 9. 15 shows the Weather Alert screen. Figure 9. 15 The Weather Alert feature. Employee Information

Ngày đăng: 14/08/2014, 01:20

Xem thêm: Visual Basic .NET at Work Building 10 Enterprise Projects phần 9 ppt

TỪ KHÓA LIÊN QUAN