Dorknozzle Chapter 9 data not writing to tables

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

Code looks ok, what is the error message?

I am getting the error message from the earlier example in the chapter: “Error submitting the help desk request! Please try again later, and/or change the entered data!”

I am still getting this error no matter what data is entered, I’m thinking the problem might not be the code but something else such as a database setting, although I think I have followed the books instructions correctly so far.

Doh, thought it wasn’t eating the exception. Remove the try/catch and you should have a much better idea of what is going on. Alternatively, run it in the VS Express debugger and it should pop the exception for you.

Hi, Tried it without the try/catch and still no luck, the page just re-loaded when I hit submit but the data is still in the fields I’m presuming this is because of the View State? Although the book says this shouldn’t be happening:

In our Dorknozzle Help Desk request form script, we redirect the user back to the same web form. Why on earth would we want to do that? It’s because of view state if we didn’t end our event handler this way, the same page
would display in the browser, but ASP.NET would preserve all of the values that the user had typed into the form fields. The user might not realize the form had even been submitted, and might submit the form repeatedly in his or her confusion. Redirecting the user in the way that’s outlined above causes the browser to reload the page from scratch, clearing the form fields to indicate the completed submission.

I also ran the debugger and received the following:

'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_32\\mscorlib\\2.0.0.0__b77a5c561934e089\\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\9.0\\WebDev.WebServer.EXE', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_32\\WebDev.WebHost\\9.0.0.0__b03f5f7f11d50a3a\\WebDev.WebHost.dll'
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Windows.Forms\\2.0.0.0__b77a5c561934e089\\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System\\2.0.0.0__b77a5c561934e089\\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Drawing\\2.0.0.0__b03f5f7f11d50a3a\\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_32\\System.Web\\2.0.0.0__b03f5f7f11d50a3a\\System.Web.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Configuration\\2.0.0.0__b03f5f7f11d50a3a\\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Xml\\2.0.0.0__b77a5c561934e089\\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Web.RegularExpressions\\2.0.0.0__b03f5f7f11d50a3a\\System.Web.RegularExpressions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_32\\System.Data\\2.0.0.0__b77a5c561934e089\\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_32\\System.Transactions\\2.0.0.0__b77a5c561934e089\\System.Transactions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_32\\System.EnterpriseServices\\2.0.0.0__b03f5f7f11d50a3a\\System.EnterpriseServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Design\\2.0.0.0__b03f5f7f11d50a3a\\System.Design.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\Microsoft.JScript\\8.0.0.0__b03f5f7f11d50a3a\\Microsoft.JScript.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\Temporary ASP.NET Files\\dorknozzle\\b4066d36\\44076cf1\\App_global.asax.ysfzazme.dll', Symbols loaded.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\Microsoft.VisualBasic\\8.0.0.0__b03f5f7f11d50a3a\\Microsoft.VisualBasic.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Web.Mobile\\2.0.0.0__b03f5f7f11d50a3a\\System.Web.Mobile.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.ServiceModel\\3.0.0.0__b77a5c561934e089\\System.ServiceModel.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\SMDiagnostics\\3.0.0.0__b77a5c561934e089\\SMDiagnostics.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Web.Extensions\\3.5.0.0__31bf3856ad364e35\\System.Web.Extensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Web.Abstractions\\3.5.0.0__31bf3856ad364e35\\System.Web.Abstractions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Core\\3.5.0.0__b77a5c561934e089\\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\Temporary ASP.NET Files\\dorknozzle\\b4066d36\\44076cf1\\App_Web_g7tn6xv-.dll', Symbols loaded.
'WebDev.WebServer.EXE' (Managed): Loaded 'c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\Temporary ASP.NET Files\\dorknozzle\\b4066d36\\44076cf1\\App_Theme_Blue.qmc5l8i6.dll', Symbols loaded.
'WebDev.WebServer.EXE' (Managed): Loaded 'c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\Temporary ASP.NET Files\\dorknozzle\\b4066d36\\44076cf1\\App_Web_ummtscto.dll', Symbols loaded.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Web.Services\\2.0.0.0__b03f5f7f11d50a3a\\System.Web.Services.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.ServiceModel.Web\\3.5.0.0__31bf3856ad364e35\\System.ServiceModel.Web.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.WorkflowServices\\3.5.0.0__31bf3856ad364e35\\System.WorkflowServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Runtime.Serialization\\3.0.0.0__b77a5c561934e089\\System.Runtime.Serialization.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.IdentityModel\\3.0.0.0__b77a5c561934e089\\System.IdentityModel.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Data.DataSetExtensions\\3.5.0.0__b77a5c561934e089\\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Xml.Linq\\3.5.0.0__b77a5c561934e089\\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'WebDev.WebServer.EXE' (Managed): Loaded 'A_8cf3d2e0_8d23_404d_8e1c_2a5f3b168603'
'WebDev.WebServer.EXE' (Managed): Loaded 'M_8cf3d2e0_8d23_404d_8e1c_2a5f3b168603'
'WebDev.WebServer.EXE' (Managed): Loaded 'c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\Temporary ASP.NET Files\\dorknozzle\\b4066d36\\44076cf1\\App_Web_lfjjat36.dll', Symbols loaded.
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
The program '[4152] WebDev.WebServer.EXE: Managed' has exited with code 0 (0x0).
The program '[3848] iexplore.exe: Script program' has exited with code 0 (0x0).

A first chance exception of type ‘System.Data.SqlClient.SqlException’ occurred in System.Data.dll
The program ‘[4152] WebDev.WebServer.EXE: Managed’ has exited with code 0 (0x0).
The program ‘[3848] iexplore.exe: Script program’ has exited with code 0 (0x0).

Looks like something is still catching the error, make sure you got all the try/catch statements covered.

I had the same problem. I found that when I commented out the try/catch as suggested I got an error message telling me I had a problem with the following:

comm.ExecuteNonQuery();

I commented out that one line of code and the stored procedure ran as the authors said it would. After a moment of thinking, it made sense since the stored procedure is executing the SQL command for you.

HTH

p.s. I sent a note to the authors sharing this as well.