Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP.NET 2.0 Instant Results

.pdf
Скачиваний:
67
Добавлен:
17.08.2013
Размер:
11.03 Mб
Скачать

12

Modifying the Bug Base

The Bug Base application presented in this chapter, useful as it may be now, most likely won’t have all the functionality you require. Being a starter kit, it does not contain all the bells and whistles you may need in day-to-day bug tracking. Some of the enhancements you can make include:

E-mail Tracking: Get alerts by e-mail whenever the status of a bug changes.

My Bugs: A page that lists all the bugs you logged or are tracking through e-mail.

Bug Assignments: Allows a manager to assign a bug to a developer to fix it. Assigned bugs could show up on the My Bugs page, for instance.

An HTML Editor: You can deploy the FCKeditor you have seen in previous chapters to allow your users to enter formatted data.

Another enhancement that can be made to the Bug Base is to allow your users to upload files. This can be very useful if you want them to upload screen shots, error logs, and other related files. Having a screen shot of the error message or other relevant information about the application’s state at the time of the error can be critical in reproducing bugs. The next section of this chapter walks you through the changes you need to make to implement the file upload feature.

You should start by modifying the Bug table and the stored procedure that inserts the bug in the database. Then you need to change the Bug and the BugManagerDB classes. Finally, you should modify the AddEditBug.aspx page. To implement the changes, follow these steps:

1.In Visual Web Developer open the Database Explorer by choosing View Database Explorer. Locate the BugBase database under Data Connections. If the database is not listed, right click Data Connections, choose Add Connection, and browse to BugBase.mdf in the App_Data folder. Next, expand Tables, right click the Bug table, and choose Open Table Definition. This brings up the database table designer.

2.Add a new column called Attachment. Set its data type to nvarchar(255) and leave the Allow Nulls checkbox unchecked. You can save the changes and close the table designer.

3.Next, expand the Stored Procedures node and locate the procedure sprocBugInsert UpdateSingleItem. Add a parameter called @attachment with a type of nvarchar(255)

Chapter 12

and then change both the Insert and Update parts of the procedure so they correctly save the attachment in the database.

4.Open the stored procedure sprocBugSelectSingleItem and add support for the attachment by adding Bug.Attachment to the SELECT list.

5.Repeat step 4, but this time add the column to the SELECT list of the procedure sprocBug SelectList.

6.Open the Bug.vb file in the BusinessLogic folder and add a private field called _attachment, right below the field _status. Set its type to String and its initial value to String.Empty.

7.In the Public Properties region add a public property called Attachment, similar to the Title property. Your property should end up like this:

Public Property Attachment() As String

Get

Return _attachment

End Get

Set(ByVal value As String) _attachment = value

End Set

End Property

This is the only change you need to make in the business layer.

8.You’ll need to make three changes to the data access layer: two in the methods that retrieve the bug (GetBug and GetBugList) and one in the method that saves the bug in the database. Open the file BugManager.vb and locate the GetBug method. Right below the line that starts with theBug.Status add the following lines of code:

If Not myReader.IsDBNull(myReader.GetOrdinal(“Attachment”)) Then theBug.Attachment = myReader.GetString( _

myReader.GetOrdinal(“Attachment”))

End If

9.Repeat step 8, but this time add the line of code in the GetBugList method right below the line that starts with theBug.Status.

10.The final change you need to make is in the InsertUpdateBug method. Add an additional parameter to the SqlCommand when the Attachment property of the bug no longer contains an empty string:

myCommand.Parameters.AddWithValue(“@statusId”, theBug.Status.Value) If Not theBug.Attachment = String.Empty Then

myCommand.Parameters.AddWithValue(“@attachment”, theBug.Attachment) Else

myCommand.Parameters.AddWithValue(“@attachment”, DBNull.Value) End If

With these changes, the database, the data access layer, and the business layer now support an attachment for the bug. The final change you need to make is in the AddEditBug page.

1.Open the page AddEditBug.aspx from the Bugs folder, switch to Design View if necessary and add a new table row right above the Save and Cancel buttons.

58

Modifying the Bug Base

2.Inside the first cell of the table, type the text Attachment.

3.From the Toolbox, drag a FileUpload control, a CustomValidator, and a Hyperlink from the Toolbox into the second cell. Give the Hyperlink an ID of lnkAttachment, set its Visible property to False, its Text property to View Attachment, and its target to _blank. Set the

ErrorMessage of the CustomValidator to This is not a valid attachment, and its Display property to Dynamic.

4.Double-click the Save button to switch to the btnSave_Click method in the code-behind of the page.

5.Right before the line that starts with Page.Validate(), add code that validates the uploaded file:

If FileUpload1.HasFile Then

‘ Validate the extension

Dim fileName As String = FileUpload1.PostedFile.FileName.ToLower()

If Not fileName.EndsWith(“.jpg”) _

AndAlso Not fileName.EndsWith(“doc”) _

AndAlso Not fileName.EndsWith(“.gif”) Then

CustomValidator1.IsValid = False

End If

End If

Page.Validate()

6.Next, add the code that saves the uploaded file to disk right before the call to

InsertUpdateBug:

Try

‘ Now save the attachment, if any If FileUpload1.HasFile Then

Dim newFileName As String = “~/Uploads/” & Guid.NewGuid.ToString() & _ System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName)

FileUpload1.SaveAs(Server.MapPath(newFileName)) myBug.Attachment = newFileName

End If myBugManager.InsertUpdateBug(myBug)

7.This code tries to save the attachment in a folder called Uploads, so you’ll have to create that folder first. To do so, right click the project in the Solution Explorer and choose Add Folder New Folder. You may also need to set security permissions on this folder for the account used by the web server. Refer to Chapter 5 for instructions on how to do this.

8.Still in the code-behind for the AddEditBug.aspx page, add the following code to the end of the code on the LoadData method that loads an existing bug from the database:

Me.Title = “Edit Bug #” + bugId.ToString() btnSave.Text = “Update bug”

‘ Display the attachment when available: If Not myBug.Attachment = String.Empty Then

lnkAttachment.NavigateUrl = myBug.Attachment

lnkAttachment.Visible = True FileUpload1.Visible = False

End If Else

59

Chapter 12

Now when you file a new bug, you can upload a new attachment by clicking the Browse button. The attachment can be viewed on the AddEditBug.aspx page when you edit an existing bug. If you want, you can also add support for the attachment to the ViewBug page so it can be viewed from that page as well. The code required for that change is almost the same as the code shown in step 8.

You can also use the UploadHandler class that you saw in the Chapter 11 to make uploading the file and checking the extensions even easier.

60