I am having a problem with chapter 9 of Build Your Own ASP.NET site
I have completed the chapter and I can’t get the data to write to the tables for HelpDesk.aspx, I am continuing to get the catch block error message from Figure 9.11 I have also tried using the code from the code archive and nothing is happening.
I must be doing something wrong?
Any help would be appreciated, thanks.
HelpDesk.aspx
<%@ Page Language="VB" MasterPageFile="~/Dorknozzle.master" AutoEventWireup="false" CodeFile="HelpDesk.aspx.vb" Inherits="HelpDesk" title="Dorknozzle Help Desk" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h1>Employee Help Desk Request</h1>
<asp:Label ID="dbErrorMessage" ForeColor="Red" runat="server" />
<p>
Station Number:<br />
<asp:TextBox id="stationTextBox" runat="server"
CssClass="textbox" />
<asp:RequiredFieldValidator id="stationNumReq" runat="server"
ControlToValidate="stationTextBox"
ErrorMessage="<br />You must enter a station number!"
Display="Dynamic" />
<asp:CompareValidator id="stationNumCheck" runat="server"
ControlToValidate="stationTextBox"
Operator="DataTypeCheck" Type="Integer"
ErrorMessage="<br />The value must be a number!"
Display="Dynamic" />
<asp:RangeValidator id="stationNumRangeCheck" runat="server"
ControlToValidate="stationTextBox"
MinimumValue="1" MaximumValue="50" Type="Integer"
ErrorMessage="<br />Number must be between 1 and 50."
Display="Dynamic" />
</p>
<p>
Problem Category:<br />
<asp:DropDownList id="categoryList" runat="server"
CssClass="dropdownmenu" />
</p>
<p>
Problem Subject:<br />
<asp:DropDownList id="subjectList" runat="server"
CssClass="dropdownmenu" />
</p>
<p>
Problem Description:<br />
<asp:TextBox id="descriptionTextBox" runat="server"
CssClass="textbox" Columns="40" Rows="4"
TextMode="MultiLine" />
<asp:RequiredFieldValidator id="descriptionReq" runat="server"
ControlToValidate="descriptionTextBox"
ErrorMessage="<br />You must enter a description!"
Display="Dynamic" />
</p>
<p>
<asp:Button id="submitButton" runat="server"
CssClass="button" Text="Submit Request" /></p>
</asp:Content>
HelpDesk.aspx.vb
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class HelpDesk
Inherits System.Web.UI.Page
Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitButton.Click
If Page.IsValid Then
' Define data objects
Dim conn As SqlConnection
Dim comm As SqlCommand
' Read the connection string from Web.config
Dim connectionString As String = _
ConfigurationManager.ConnectionStrings( _
"Dorknozzle").ConnectionString
' Initialize connection
conn = New SqlConnection(connectionString)
' Create command
comm = New SqlCommand("InsertHelpDesk", conn)
' Specify we're calling a stored procedure
comm.CommandType = System.Data.CommandType.StoredProcedure
' Add command parameters
comm.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int)
comm.Parameters("@EmployeeID").Value = 5
comm.Parameters.Add("@StationNumber", _
System.Data.SqlDbType.Int)
comm.Parameters("@StationNumber").Value = stationTextBox.Text
comm.Parameters.Add("@CategoryID", System.Data.SqlDbType.Int)
comm.Parameters("@CategoryID").Value = _
categoryList.SelectedItem.Value
comm.Parameters.Add("@SubjectID", System.Data.SqlDbType.Int)
comm.Parameters("@SubjectID").Value = _
subjectList.SelectedItem.Value
comm.Parameters.Add("@Description", _
System.Data.SqlDbType.NVarChar, 50)
comm.Parameters("@Description").Value = _
descriptionTextBox.Text
comm.Parameters.Add("@StatusID", System.Data.SqlDbType.Int)
comm.Parameters("@StatusID").Value = 1
' Enclose database code in Try-Catch-Finally
Try
' Open the connection
conn.Open()
' Execute the command
comm.ExecuteNonQuery()
' Reload page if the query executed successfully
Response.Redirect("HelpDesk.aspx")
Catch
' Display error message
dbErrorMessage.Text = _
"Error submitting the help desk request! Please " & _
"try again later, and/or change the entered data!"
Finally
' Close the connection
conn.Close()
End Try
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Define data objects
Dim conn As SqlConnection
Dim categoryComm As SqlCommand
Dim subjectComm As SqlCommand
Dim reader As SqlDataReader
' Read the connection string from Web.config
Dim connectionString As String = _
ConfigurationManager.ConnectionStrings( _
"Dorknozzle").ConnectionString
' Initialize connection
conn = New SqlConnection(connectionString)
' Create command to read the help desk categories
categoryComm = New SqlCommand( _
"SELECT CategoryID, Category FROM HelpDeskCategories", _
conn)
' Create command to read the help desk subjects
subjectComm = New SqlCommand( _
"SELECT SubjectID, Subject FROM HelpDeskSubjects", conn)
' Enclose database code in Try-Catch-Finally
Try
' Open the connection
conn.Open()
' Execute the category command
reader = categoryComm.ExecuteReader()
' Populate the list of categories
categoryList.DataSource = reader
categoryList.DataValueField = "CategoryID"
categoryList.DataTextField = "Category"
categoryList.DataBind()
' Close the reader
reader.Close()
' Execute the subjects command
reader = subjectComm.ExecuteReader()
' Populate the list of subjects
subjectList.DataSource = reader
subjectList.DataValueField = "SubjectID"
subjectList.DataTextField = "Subject"
subjectList.DataBind()
' Close the reader
reader.Close()
Finally
' Close the connection
conn.Close()
End Try
End If
End Sub
End Class