Data Not Inserting into Database

Discussion in '.NET' started by twilitegxa, Oct 20, 2009.

  1. twilitegxa

    twilitegxa

    Joined:
    Oct 19, 2009
    Messages:
    3
    Likes Received:
    0
    My form upon submission is displaying my error message and not inserting my data into my database. What am I doing wrong? Here is my page:

    Code (Text):

    <%@ 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:
    Code (Text):

    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();
                }
            }
        }
    }
     
     
    twilitegxa, Oct 20, 2009
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.