Jump to content

Recommended Posts

Can anyone help me figure out why my data is not inserting from my form?

 

<%@ 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();
            }
        }
    }
}

 

It only display my error message.

Which error message is it displaying?  One from failing validation, or the one from failing the insert itself?

 

EDIT: Why have you hard wired the EmployeeID value?  If you're using it as the primary key of the HelpDesk table, the insert will only work once.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.