Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

Trouble Insert Values into DB

Hello Experts,

Please see my attahced CodeBehind. When I run the btn_NewUser_Click Event Handler Code the page does NOT add values to my DB. I cannot figure it out. I don't know if I need to do some PostBack checks on the other Events I have or not.


using System;
using System.Security.Cryptography;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Net.NetworkInformation;

public partial class programinfo_ghap_newuser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            RetrieveClientValues();
            RetrieveBldgLocationValues();
            RetrieveClientsStatesValues();
        
        pWork.Visible = false;
        pHome.Visible = false;
    }

    protected void rblContactMethod_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rblContactMethod.SelectedValue == "0")
        {
            pWork.Visible = true;
            pHome.Visible = false;
        }
        else if (rblContactMethod.SelectedValue == "1")
        {
            pHome.Visible = true;
            pWork.Visible = false;
        }
    }

    protected void RetrieveClientValues()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveClientValues";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                ddlClient.DataSource = rdr;
                ddlClient.DataValueField = "c_id";
                ddlClient.DataTextField = "c_initial";
                ddlClient.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void RetrieveBldgLocationValues()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveClientBldgValues";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                ddlBldgLocation.DataSource = rdr;
                ddlBldgLocation.DataValueField = "bldg_id";
                ddlBldgLocation.DataTextField = "bldg_name";
                ddlBldgLocation.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void RetrieveClientsStatesValues()
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveClientStateValues";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                ddlState.DataSource = rdr;
                ddlState.DataValueField = "s_id";
                ddlState.DataTextField = "s_initial";
                ddlState.DataBind();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void btn_NewUser_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_InsertUsers";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@c_id", SqlDbType.Int).Value = ddlClient.SelectedItem.Value;
            cmd.Parameters.AddWithValue("@bldg_id", SqlDbType.Int).Value = ddlBldgLocation.SelectedItem.Value;
            cmd.Parameters.AddWithValue("@s_id", SqlDbType.Int).Value = ddlState.SelectedItem.Value;
            cmd.Parameters.AddWithValue("@user_name", SqlDbType.VarChar).Value = txtName.Text;
            cmd.Parameters.AddWithValue("@user_street_address", SqlDbType.VarChar).Value = txtHomeAddress.Text;
            cmd.Parameters.AddWithValue("@user_city", SqlDbType.VarChar).Value = txtCity.Text;
            cmd.Parameters.AddWithValue("@user_zip", SqlDbType.VarChar).Value = txtZipCode.Text;
            cmd.Parameters.AddWithValue("@user_phone", SqlDbType.VarChar).Value = txtHomePhone.Text;
            cmd.Parameters.AddWithValue("@user_work_ext", SqlDbType.VarChar).Value = txtWorkExtension.Text;
            cmd.Parameters.AddWithValue("@user_email", SqlDbType.VarChar).Value = txtEmailAddress.Text;
            cmd.Parameters.AddWithValue("@user_username", SqlDbType.VarChar).Value = txtUserName.Text;
            cmd.Parameters.AddWithValue("@user_password", SqlDbType.VarChar).Value = txtPassword.Text;

            try
            {
                cmd.ExecuteNonQuery();

                Response.Redirect("newuser_success.aspx");
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }
}

Open in new window

HTML Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="newuser.aspx.cs" Inherits="programinfo_ghap_newuser" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>WelAccount</title>
    <link href="../../css/main.css" rel="stylesheet" type="text/css" />
</head>
<body class="info">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
	<div id="wrapper">
    	<div id="header">
        <img src="../../images/ShapeUpWithoutText_221x89_4_Logo.png" alt="Shape Up Logo" />
            <div id="nav">
                <ul>
                    <li><a href="../../index.aspx">HOME</a></li>
                    <li class="current"><a href="../index.aspx">PROGRAM INFORMATION</a></li>
                    <li><a href="http://wellne/" target="_blank">REPORTING SYSTEM</a></li>
                    <li><a href="../../contact/index.aspx">CONTACT</a></li>
                </ul>
            </div>
        </div>
        <div id="content">
            <div id="primary">
                <h1>NEW USER ACCOUNT</h1>
                <p>Employees must complete this form in order to schedule classes to take.</p>
                <asp:Label ID="lblName" runat="server" CssClass="CourseDescTitle" Text="Name"></asp:Label>
                <br />
                <asp:TextBox ID="txtName" Width="300" CssClass="txtCourseInsert" runat="server" MaxLength="50"></asp:TextBox>
                <br />
                <br />
                <asp:Label ID="lblClient" runat="server" CssClass="CourseDescTitle" Text="Employer"></asp:Label>
                <br />
                <asp:DropDownList ID="ddlClient" CssClass="ddlCourseInsert" Width="307" runat="server"></asp:DropDownList>
                <br />
                <br />
                <asp:Label ID="lblContactMethod" runat="server" CssClass="CourseDescTitle" Text="Preferred Contact Method"></asp:Label>
                <br />
                <asp:RadioButtonList ID="rblContactMethod" runat="server" RepeatDirection="Horizontal" OnSelectedIndexChanged="rblContactMethod_SelectedIndexChanged" AutoPostBack="True">
                    <asp:ListItem Value="0">Work</asp:ListItem>
                    <asp:ListItem Value="1">Home</asp:ListItem>
                </asp:RadioButtonList>
                <br />
                <asp:Panel ID="pWork" runat="server">
                    <asp:Label ID="lblWorkBldg" runat="server" CssClass="CourseDescTitle" Text="Select Your Location"></asp:Label>
                    <br />
                    <asp:DropDownList ID="ddlBldgLocation" CssClass="ddlCourseInsert" Width="307" runat="server"></asp:DropDownList>
                    <br />
                    <br />
                    <asp:Label ID="lblWorkExtension" runat="server" CssClass="CourseDescTitle" Text="Phone Extension"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtWorkExtension" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="4"></asp:TextBox>
                    <br />
                </asp:Panel>
                <asp:Panel ID="pHome" runat="server">
                    <asp:Label ID="lblHomeAddress" runat="server" CssClass="CourseDescTitle" Text="Street Address"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtHomeAddress" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="50"></asp:TextBox>
                    <br />
                    <br />
                    <asp:Label ID="lblCity" runat="server" CssClass="CourseDescTitle" Text="City"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtCity" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="50"></asp:TextBox>
                    <br />
                    <br />
                    <asp:Label ID="lblState" runat="server" CssClass="CourseDescTitle" Text="State"></asp:Label>
                    <br />
                    <asp:DropDownList ID="ddlState" CssClass="ddlCourseInsert" Width="307" runat="server"></asp:DropDownList>
                    <br />
                    <br />
                    <asp:Label ID="lblZipCode" runat="server" CssClass="CourseDescTitle" Text="Zip Code"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtZipCode" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="5"></asp:TextBox>
                    <br />
                    <br />
                    <asp:Label ID="lblHomePhone" runat="server" CssClass="CourseDescTitle" Text="Phone"></asp:Label>
                    <br />
                    <asp:TextBox ID="txtHomePhone" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="12"></asp:TextBox>
                    <br />
                </asp:Panel>
                <br />
                <asp:Label ID="lblEmailAddress" runat="server" CssClass="CourseDescTitle" Text="Email Address"></asp:Label>
                <br />
                <asp:TextBox ID="txtEmailAddress" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="100"></asp:TextBox>
                <br />
                <br />
                <asp:Label ID="lblConfirmEmail" runat="server" CssClass="CourseDescTitle" Text="Confirm Email Address"></asp:Label>
                <br />
                <asp:TextBox ID="txtConfirmEmail" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="100"></asp:TextBox>
                <br />
                <br />
                <asp:Label ID="lblUsername" runat="server" CssClass="CourseDescTitle" Text="Username"></asp:Label>
                <br />
                <asp:TextBox ID="txtUserName" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="50"></asp:TextBox>
                <br />
                <br />
                <asp:Label ID="lblPassword" runat="server" CssClass="CourseDescTitle" Text="Password"></asp:Label>
                <br />
                <asp:TextBox ID="txtPassword" CssClass="txtCourseInsert" Width="300" runat="server" MaxLength="50"></asp:TextBox>
                <br />
                <asp:Label ID="lblInsertError" runat="server"></asp:Label>
                <br />
                <asp:Button ID="btn_NewUser" runat="server" Text="Create Account" onclick="btn_NewUser_Click" />
            </div>
            <div id="secondary">
                <h1>PROGRAM OFFERINGS</h1>
                <ul>
                    <li><a href="../forms/index.aspx">Forms</a></li>
                    <li><a href="index.aspx">General Health Awareness Programs</a></li>
                </ul>
            </div>
        </div>
        <div id="footer">
            <%--<p>&#169</p>--%>
        </div>
    </div>
</form>
</body>
</html>

Open in new window

0
asp_net2
Asked:
asp_net2
1 Solution
 
Rouneh10Commented:
You appear to have forgotten to call conn.Open(); in your button click handler.
0
 
asp_net2Author Commented:
That was it :)

Can't belive I missed that :(
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now