I have the following form, but my data is not getting inserted into my database. Can anyone help?

<%@ Page Title="Dorknozzle Help Desk" Language="C#" MasterPageFile="~/Dorknozzle.master" AutoEventWireup="true" CodeFile="HelpDesk.aspx.cs" Inherits="HelpDesk" %>

<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" onclick="submitButton_Click" />
</p>
</asp:Content>

Code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class HelpDesk : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn;
            SqlCommand categoryComm;
            SqlCommand subjectComm;
            SqlDataReader reader;
            string connectionString = ConfigurationManager.ConnectionStrings["Dorknozzle"].ConnectionString;
            conn = new SqlConnection(connectionString);
            categoryComm = new SqlCommand("SELECT CategoryID, Category FROM HelpDeskCategories", conn);
            subjectComm = new SqlCommand(
            "SELECT SubjectID, Subject FROM HelpDeskSubjects", conn);
            try
            {
                conn.Open();
                reader = categoryComm.ExecuteReader();
                categoryList.DataSource = reader;
                categoryList.DataValueField = "CategoryID";
                categoryList.DataValueField = "Category";
                categoryList.DataBind();
                reader.Close();
                reader = subjectComm.ExecuteReader();
                subjectList.DataSource = reader;
                subjectList.DataValueField = "SubjectID";
                subjectList.DataValueField = "Subject";
                subjectList.DataBind();
                reader.Close();
            }
            finally
            {
                conn.Close();
            }
        }
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SqlConnection conn;
            SqlCommand comm;
            string connectionString = ConfigurationManager.ConnectionStrings["Dorknozzle"].ConnectionString;
            conn = new SqlConnection(connectionString);
            comm = new SqlCommand("INSERT INTO HelpDesk (EmployeeID, StationNumber, " + "CategoryID, SubjectID, Description, StatusID) " + "VALUES (@EmployeeID, @StationNumber, @CategoryID, " + "@SubjectID, @Description, @StatusID)", conn);
            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;
            try
            {
                conn.Open();
                comm.ExecuteNonQuery();
                Response.Redirect("HelpDesk.aspx");
            }
            catch
            {
                dbErrorMessage.Text = "Error submitting the help desk request! Please " +
                    "try again later, and/or change the entered data!";
            }
            finally
            {
                conn.Close();
            }
        }
    }
}

Can anyone tell me what I'm doing wrong? It only display my error message.

Did you try put a break point in the line conn.Open() in the submitButton_Click? If so, what is the error message? (Not your error message, but the one on Visual Studio)

I am not familiar with break points. How would I create one in ASP.NET?

In the line were you want to create the break point you click in the left side of the vertical line.
See this image: http://www.beansoftware.com/ASP.NET-Tutorials/Images/Client-Debugging-BreakPoint.jpg
The red ball is the break point.

You run your page and when it reaches the break point you click F8, and F8, and F8... until you get the error message :)

You can find lots of articles and tutorial on the internet about debugging ASP.NET applications. I strongly recommend you to learn it because it will help you a lot and make you spend much less time finding errors in your code.

Hope it helps :)

I tried inserting a break point, but I did not see any errors. I used the debugging mode, but I didn't see any errors.

Did you go step by step by clicking on F8? Because if you just clicked F5 when it reached the break point, the exception was handled by your catch and, because of this you didn't see any error message.

What you can do to force the error message is, just for now, is put your code before the try/catch/finally and comment the try/catch/finally fragment. In this case you exception will not be handled and it will throw an error message. :)

Oh, I know what is wrong! Try this: put you try/catch/finally inside a Using conn

[B]Using conn[/B]
    try
         your code here
    catch
         your code here
    finally
         your code here
[B]End Using[/B]

I think it will insert now :)

Like this? (this is the very end of the code)

using conn
            try
            {
                conn.Open();
                comm.ExecuteNonQuery();
                Response.Redirect("HelpDesk.aspx");
            }
            catch
            {
                dbErrorMessage.Text = "Error submitting the help desk request! Please " +
                    "try again later, and/or change the entered data!";
            }
            finally
            {
                conn.Close();
            }
            End Using
        }
    }
}

Yep! It's just include the try/catch/finally inside an Using conn {...} End conn

See if it works and let us know.

Actually, my code was in VB.NET. You are using C#, aren't you? So the version in C# would be:

using (DatabaseConnection) {
         try {
             //Your code
         }
         catch (Exception ex) {
             //Your code
         }
         finally {
             //Your code
         }
}

Now it is saying this:

Compiler Error Message: CS0103: The name 'DatabaseConnection' does not exist in the current context

Source Error:

Line 68: comm.Parameters["@StatusID"].Value = 1;
Line 69:
Line 70: using (DatabaseConnection) {
Line 71: try
Line 72: {

I guess I don't have something included at the top?

I don't even understand why my insert statement won't work because I have an update statement that works just fine on another page.

This is because in my example, my connection was called DatabaseConnection. In your case, it is conn

Can you please post the code for update you said that works? Thanks.

And you didn't answered my question about how you debugged your code (see the corresponding message in the thread).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.