10 Replies - 2079 Views - Last Post: 19 November 2013 - 12:01 AM Rate Topic: -----

#1 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Entered/Selected entries from web forms not saved into database

Posted 14 November 2013 - 01:30 AM

I am using Visual Studio 2010 in order to create a home insurance web application that allows users to register new accounts and get a quote by filling out a quote form once they are logged in. Currently I am trying to create a database for my web application that stores the values entered in the text boxes and the selected values from the drop-down lists and radio-button lists into a database table using the following field names and SQL data types as shown in the attached "DatabaseTable.PNG" file:

In order to create a Dynamic Data Source to hold each client in the memory dynamically, I haved use a C# List type. Each client is represented by a Home class. The Home class' specification is given in the code below. I have also created a List, which is common data structure to all pages of the application (in the Basepage.cs file), as shown below:

public static List<Home> allHomes = new List<Home>(); 



All of my code behind files in my project inherit from the Basepage.cs file
A record (row of information) is added to the table whenever a new client registration is completed. Again when the property information is added the data table is updated for the relevant client. The allHomes list is populated using the data table in the actual database whenever a new client uses the application. For this purpose I added a static constructor, which reads the table from the database and populate the allHomes list. The code for my BasePage.cs file is shown below

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

namespace WebApplication06
{
    public class BasePage : System.Web.UI.Page
    {
        public static List<Home> allHomes = new List<Home>();

        static BasePage()
        {
            //write your code here to read the table from the actual database
            //and populate the allHomes list.
            SqlConnection connectionToServer = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

            string theStatement = "SELECT * FROM InsuranceCustomers";

            SqlCommand sqlCommand = new SqlCommand(theStatement, connectionToServer);

            try
            {
                connectionToServer.Open();

                SqlDataReader sqlReader = sqlCommand.ExecuteReader();

                while (sqlReader.Read())
                {
                    Home hm = new Home();

                    hm.Email = Convert.ToString(sqlReader["email"]);
                    hm.Password = Convert.ToString(sqlReader["password"]);
                    hm.FirstName = Convert.ToString(sqlReader["firstName"]);
                    hm.LastName = Convert.ToString(sqlReader["lastName"]);
                    hm.PropertyAddressStreet = Convert.ToString(sqlReader["propertyStreet"]);
                    hm.PropertyAddressCity = Convert.ToString(sqlReader["propertyCity"]);
                    hm.PropertyAddressState = Convert.ToString(sqlReader["propertyState"]);
                    hm.PropertyAddressZip = Convert.ToString(sqlReader["propertyZip"]);
                    hm.PropertyType = Convert.ToString(sqlReader["propertyType"]);
                    hm.ApproximateYearBuilt = Convert.ToInt16(sqlReader["YearBuilt"]);
                    hm.ApproximateSqFootage = Convert.ToSingle(sqlReader["SqFootage"]);
                    hm.OwnOrRent = Convert.ToInt16(sqlReader["ownOrRent"]);
                    hm.LivingStatus = Convert.ToInt16(sqlReader["livingStatus"]);
                    allHomes.Add(hm);
                }

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



Upon starting this web application, the "Default.aspx" page (which is the home page, attached snapshot is) will open up in the web browser. Clicking the "New Customer Registration" link will bring the user to the "NewCustomerRegistration.aspx" form. The snapshot for this file is shown in the attached file NewCustomerRegistration.PNG.


After the user fills out this form and clicks the "Register" button, the RegisterUser() function from the code behind file NewCustomerRegistration.aspx.cs (shown below) is executed.

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;

namespace WebApplication06
{
    public partial class NewCustomerRegistration : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CheckUser(object sender, EventArgs e)
        {
            // set up array for each user's email
            foreach(Home user in allHomes)
            {
                // if email already exists, show this message dialog
                if (GetEmailDetails(Convert.ToString(Session["Email"])) != null && Session["Email"] != null && !"".Equals(Email.Text.Trim()))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Title", "alert('" + "This email address already exists! Please login" + "');", true);
                }
            }  
        }

        protected void RegisterUser(object sender, EventArgs e)
        {
            Session["thisCustomer"] = Email.Text;     

            // if email already exists, show this message dialog
            if (GetEmailDetails(Convert.ToString(Session["thisCustomer"])) != null && Session["thisCustomer"] != null && !"".Equals(Email.Text.Trim()))
            {
                Session.Abandon();
                Session.Clear();
                ClientScript.RegisterStartupScript(this.GetType(), "Title", "alert('" + "This email address already exists! Please login" + "');", true);
            }

            else
            {
                Home user = new Home(); // otherwise, create new object for each Home user and assign it to user

                // set each text box ID to its corresponding get/set methods in the Home class
                user.FirstName = FirstName.Text;
                user.LastName = LastName.Text;
                user.Email = Email.Text;
                user.PropertyAddressZip = ZipCode.Text;
                user.Password = Password.Text;

                allHomes.Add(user);     // add user to allHomes list
                Session["thisCustomer"] = user;

                AddNewCustomerToTable();    //add registration details to the data table
                Response.Redirect("Default.aspx");
            }
            
        }

        public Home GetEmailDetails(string eemail)
        {
            if (Session["thisCustomer"] != null)
            {
                // set up array for each user's email
                foreach (Home user in allHomes)
                {
                    if (user.Email == eemail)
                        return user;
                }
            }
            return null;
        }

        protected void AddNewCustomerToTable()
        {
            string theStatement = "INSERT INTO InsuranceCustomers (";
            theStatement += "email, password, firstName, lastName) ";
            theStatement += "VALUES (";
            theStatement += "@email, @password, @firstName, @lastName)";
            SqlConnection connectionToServer = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand insertUser = new SqlCommand(theStatement, connectionToServer);

            insertUser.Parameters.AddWithValue("@email", Email.Text);
            insertUser.Parameters.AddWithValue("@password", Password.Text);
            insertUser.Parameters.AddWithValue("@firstName", FirstName.Text);
            insertUser.Parameters.AddWithValue("@lastName", LastName.Text);

            try
            {
                connectionToServer.Open();
                insertUser.ExecuteNonQuery();
            }
            finally
            {
                connectionToServer.Close();
            }
        }
    }
}



Afterwards, the user is registered and is logged in by default and is redirected back to the "Default.aspx" page. The user's first name, last name, email address, and password is saved into the table. When the user clicks the "Get A Quote" button, the "GetAQuote.aspx" page will appear. The design snapshot for this page is attached as GetAQuote.PNG. The items for the 3 drop-down-lists and radio-button group are added in the code behind for this file, GetAQuote.aspx.cs. Once the user fills this form and clicks the Process Quote Button, the code from the code behind's ProcQuote() function will be executed. Then the information from this from must be saved in the database table for the user who is currently logged in. The code behind for this quote form is shown below.

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;

namespace WebApplication06
{
    public partial class GetAQuote : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // add state abbreviation items for stateAbbrList
                stateAbbrList.Items.Add("AK");
                stateAbbrList.Items.Add("AL");
                stateAbbrList.Items.Add("AR");
                stateAbbrList.Items.Add("AZ");
                stateAbbrList.Items.Add("CA");
                stateAbbrList.Items.Add("CO");
                stateAbbrList.Items.Add("CT");
                stateAbbrList.Items.Add("DE");
                stateAbbrList.Items.Add("FL");
                stateAbbrList.Items.Add("GA");
                stateAbbrList.Items.Add("HI");
                stateAbbrList.Items.Add("IA");
                stateAbbrList.Items.Add("ID");
                stateAbbrList.Items.Add("IL");
                stateAbbrList.Items.Add("IN");
                stateAbbrList.Items.Add("KS");
                stateAbbrList.Items.Add("KY");
                stateAbbrList.Items.Add("LA");
                stateAbbrList.Items.Add("MA");
                stateAbbrList.Items.Add("MD");
                stateAbbrList.Items.Add("ME");
                stateAbbrList.Items.Add("MI");
                stateAbbrList.Items.Add("MN");
                stateAbbrList.Items.Add("MO");
                stateAbbrList.Items.Add("MS");
                stateAbbrList.Items.Add("MT");
                stateAbbrList.Items.Add("NC");
                stateAbbrList.Items.Add("ND");
                stateAbbrList.Items.Add("NE");
                stateAbbrList.Items.Add("NV");
                stateAbbrList.Items.Add("NH");
                stateAbbrList.Items.Add("NJ");
                stateAbbrList.Items.Add("NM");
                stateAbbrList.Items.Add("NY");
                stateAbbrList.Items.Add("OH");
                stateAbbrList.Items.Add("OK");
                stateAbbrList.Items.Add("OR");
                stateAbbrList.Items.Add("PA");
                stateAbbrList.Items.Add("RI");
                stateAbbrList.Items.Add("SC");
                stateAbbrList.Items.Add("SD");
                stateAbbrList.Items.Add("TN");
                stateAbbrList.Items.Add("TX");
                stateAbbrList.Items.Add("UT");
                stateAbbrList.Items.Add("VA");
                stateAbbrList.Items.Add("VT");
                stateAbbrList.Items.Add("WA");
                stateAbbrList.Items.Add("WI");
                stateAbbrList.Items.Add("WV");
                stateAbbrList.Items.Add("WY");

                // add home type items to propertyList
                propertyList.Items.Add("-- Select --");
                propertyList.Items.Add("Single Family Home");
                propertyList.Items.Add("Apartment");
                propertyList.Items.Add("Duplex");
                propertyList.Items.Add("Condominium");
                propertyList.Items.Add("Townhome");
                propertyList.Items.Add("MobilHome");

                // add own and rent items to ownOrRentList
                ownOrRentList.Items.Add("Own");
                ownOrRentList.Items.Add("Rent");
               
            }
        }

        protected void ProcQuote(object sender, EventArgs e)
        {              
            Home quote = new Home(); // otherwise, create new object for each Home user and assign it to user                   
            Session["thisCustomer"] = quote;
              
            // set each text box ID to its corresponding get/set methods in the Home class                   
            quote.PropertyAddressStreet = street.Text;
            quote.PropertyAddressCity = cityField.Text;
            quote.PropertyAddressState = stateAbbrList.SelectedValue;
            quote.PropertyAddressZip = zip.Text;
            quote.PropertyType = propertyList.SelectedValue;
            quote.ApproximateYearBuilt = Convert.ToInt16(yearBuiltField.Text);
            quote.ApproximateSqFootage = Convert.ToSingle(sqFootage.Text);
            quote.OwnOrRent = Convert.ToInt16(ownOrRentList.SelectedIndex);
            int index;
            if (live.Checked == true)
            {
                index = 1;
                quote.LivingStatus = index;
                
            }
            else if (rent.Checked == true)
            {
                index = 0;
                quote.LivingStatus = index;
            }
              
            Session["thisCustomer"] = quote.AnnualPremium;
            Session["thisCustomer"] = quote.MonthlyPayment;

            allHomes.Add(quote);     // add user to allHomes list
            AddPropertyInfoToTable();
                    
            Server.Transfer("YourQuote.aspx");
       }

        protected void AddPropertyInfoToTable()
        {
            Home quote = new Home();
            string theStatement = "INSERT INTO InsuranceCustomers (";
            theStatement += "propertyStreet, propertyCity, propertyState, propertyZip, propertyType, ";
            theStatement += "YearBuilt, SqFootage, ownOrRent, livingStatus) ";
            theStatement += "VALUES (";
            theStatement += "@propertyStreet, @propertyCity, @propertyState, @propertyZip, @propertyType, ";
            theStatement += "@YearBuilt, @SqFootage, @ownOrRent, @livingStatus)";
            SqlConnection connectionToServer = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand insertQuote = new SqlCommand(theStatement, connectionToServer);

            insertQuote.Parameters.AddWithValue("@propertyStreet", street.Text);
            insertQuote.Parameters.AddWithValue("@propertyCity", cityField.Text);
            insertQuote.Parameters.AddWithValue("@propertyState", stateAbbrList.Text);
            insertQuote.Parameters.AddWithValue("@propertyZip", zip.Text);
            insertQuote.Parameters.AddWithValue("@propertyType", propertyList.SelectedValue);
            insertQuote.Parameters.AddWithValue("@YearBuilt", yearBuiltField.Text);
            insertQuote.Parameters.AddWithValue("@SqFootage", sqFootage.Text);
            insertQuote.Parameters.AddWithValue("@ownOrRent", ownOrRentList.SelectedIndex);
            if (live.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", live.Checked.Equals(quote.LivingStatus));
            else if (rent.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", rent.Checked.Equals(quote.LivingStatus));
         
                   
            try
            {
                connectionToServer.Open();
                insertQuote.ExecuteNonQuery();
            }
            finally
            {
                connectionToServer.Close();
            }
        }
   }        
}



Right now, after I register a new customer and click the "Register" button, the following exception occurs, shown in the SqlException.PNG attached

I have no idea why this occurs. I have included the first four SQL variables (email, password, firstName, lastName) and used INSERT statements in the Registration form's code behind. And I used the remaining nine SQL variables (propertyStreet, propertyCity, propertyState, propertyZip, propertyType, YearBuilt, SqFootage, ownOrRent, livingStatus) in the INSERT statements in the Quote (Property Information) form's code behind. Also, whenever I check the table in the Visual Studio 2010, all the columns always have NULL values. None of the information entered from the New Customer Registration form or the Quote (Property Information) is added to the table in my database.

In my GetAQuote.aspx file, I currently have entered the following code for entering a value into the livingStatus column

if (live.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", live.Checked.Equals(quote.LivingStatus));
            else if (rent.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", rent.Checked.Equals(quote.LivingStatus));



I have also tried the following

if (live.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", live.Checked);
            else if (rent.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", rent.Checked);
         



and

if (live.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", live.Checked.ToString());
            else if (rent.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", rent.Checked.ToString());



but I get the same exception as before.

What am i doing wrong in my code behind? Why won't the values get saved into my table in my database?

Attached image(s)

  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Entered/Selected entries from web forms not saved into database

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Entered/Selected entries from web forms not saved into database

Posted 15 November 2013 - 06:27 AM

this:

if (live.Checked == true)
    insertQuote.Parameters.AddWithValue("@livingStatus", live.Checked.ToString());
else if (rent.Checked == true)
    insertQuote.Parameters.AddWithValue("@livingStatus", rent.Checked.ToString());




should probably just be

insertQuote.Parameters.AddWithValue("@livingStatus", rent.Checked.ToString())


In your database the livingStatus field is a non null field so it has to have a value. This means you need to get the value regardless of whether it is "live" or "rent". You need to make sure that a user can't submit the form without selecting a value.

Hmm, are you using individual radio buttons for that or a RadioButtonList? You should be using a RadioButtonList for those two values since they can only select one or the other. Then you just need to grab the value of the selected ListItem from that RadioButtonList.

This post has been edited by Nakor: 15 November 2013 - 06:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Entered/Selected entries from web forms not saved into database

Posted 17 November 2013 - 10:39 PM

Okay, I went ahead and changed the aspx code for the GetAQuote.aspx as follows:

<asp:RadioButtonList ID="LiveOrRentButtons" ForeColor="Maroon" RepeatLayout="flow" runat="server" RepeatDirection="Horizontal">
        <asp:ListItem Value="1" Selected="True">Live</asp:ListItem>
        <asp:ListItem Value="0">Rent</asp:ListItem>
    </asp:RadioButtonList>



And here's the corresponding code behind, GetAQuote.aspx.cs
insertQuote.Parameters.AddWithValue("@livingStatus", LiveOrRentButtons.SelectedItem.Value);



All of my code behind files inherit from the BasePage class, shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication06
{
    public class BasePage : System.Web.UI.Page
    {
        public static List<Home> allHomes = new List<Home>();

        static BasePage()
        {
            //write your code here to read the table from the actual database
            //and populate the allHomes list.
            SqlConnection connectionToServer = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

            string theStatement = "SELECT * FROM InsuranceCustomers";

            SqlCommand sqlCommand = new SqlCommand(theStatement, connectionToServer);

            try
            {
                connectionToServer.Open();

                SqlDataReader sqlReader = sqlCommand.ExecuteReader();

                while (sqlReader.Read())
                {
                    Home hm = new Home();

                    hm.Email = (string)sqlReader["email"];
                    hm.Password = (string)sqlReader["password"];
                    hm.FirstName = (string)sqlReader["firstName"];
                    hm.LastName = (string)sqlReader["lastName"];
                    hm.PropertyAddressStreet = (string)sqlReader["propertyStreet"];
                    hm.PropertyAddressCity = (string)sqlReader["propertyCity"];
                    hm.PropertyAddressState = (string)sqlReader["propertyState"];
                    hm.PropertyAddressZip = (string)sqlReader["propertyZip"];
                    hm.PropertyType = (string)sqlReader["propertyType"];
                    hm.ApproximateYearBuilt = (int)sqlReader["YearBuilt"];
                    hm.ApproximateSqFootage = (float)sqlReader["SqFootage"];
                    hm.OwnOrRent = (int)sqlReader["ownOrRent"];
                    hm.LivingStatus = (int)sqlReader["livingStatus"];
                    allHomes.Add(hm);
                }

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



I have tried running this Web application; but after clicking the "New Customer Registration" link, filling out the text boxes in the NewCustomer Registration.aspx form, and clicking the "Register" button, the exception shown in the attached snapshot called "SqlException - livingStatus" occurs.

Here's how the InsuranceCustomer table works:
A record (row of information) is added to this table after a new client has successfully registered. The data table is updated for the client who is currently logged in after the property information from the "GetAQuote.aspx" form has been added.

Unfortunately, my data table is still showing NULL for every column (field name) after I try to register a new customer. The table can't insert the value NULL into the livingStatus column, as said in the exception snapshot attached. Why won't my table insert any values into my database table?

Attached image(s)

  • Attached Image

This post has been edited by gutchman84: 17 November 2013 - 10:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 804
  • View blog
  • Posts: 3,514
  • Joined: 03-December 12

Re: Entered/Selected entries from web forms not saved into database

Posted 18 November 2013 - 11:06 AM

As a test change the fields to accept null values and see what is coming in as null. One null value when none are acceptable will cause the insert to fail.

Have you verified that values are being passed to the parameters for the insert statement?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,592
  • Joined: 12-December 12

Re: Entered/Selected entries from web forms not saved into database

Posted 18 November 2013 - 11:22 AM

The screenshot with the error message does not reflect the code you are discussing. It is using insertUser, not insertQuote. For insertUser you are inserting just the email, firstname, etc., but not the livingStatus (or other information).

Having insertUser and insertQuote suggests that you should have two tables. Just looking at the field names suggests that this information belongs in two (or more) tables.

This post has been edited by andrewsw: 18 November 2013 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,592
  • Joined: 12-December 12

Re: Entered/Selected entries from web forms not saved into database

Posted 18 November 2013 - 11:34 AM

PS Some people are rich and have more than one property ;)

PPS It will also be confusing if your database uses the term Customer but your code refers to Users.
Was This Post Helpful? 0
  • +
  • -

#7 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Entered/Selected entries from web forms not saved into database

Posted 18 November 2013 - 04:51 PM

I want to insert values from both the NewCustomerRegistration.aspx and the GetAQuote.aspx into one table. So should I use insertUser in each code behind file for each form?

View Postandrewsw, on 18 November 2013 - 06:22 PM, said:

The screenshot with the error message does not reflect the code you are discussing. It is using insertUser, not insertQuote. For insertUser you are inserting just the email, firstname, etc., but not the livingStatus (or other information).

Having insertUser and insertQuote suggests that you should have two tables. Just looking at the field names suggests that this information belongs in two (or more) tables.

Was This Post Helpful? 0
  • +
  • -

#8 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 804
  • View blog
  • Posts: 3,514
  • Joined: 03-December 12

Re: Entered/Selected entries from web forms not saved into database

Posted 18 November 2013 - 04:57 PM

Combine information into a single table is bad practice and will be bad to maintain. You need to normalize your table. You have a person with multiple policies and a change happens and your data is wrong. And too r code should be modular so it can be called when you need it without having to rewrite that block.

This post has been edited by astonecipher: 18 November 2013 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

#9 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Entered/Selected entries from web forms not saved into database

Posted 18 November 2013 - 04:59 PM

Here is my code behind for NewCustomerRegistration.aspx:

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;

namespace WebApplication06
{
    public partial class NewCustomerRegistration : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CheckUser(object sender, EventArgs e)
        {
            // set up array for each user's email
            foreach(Home user in allHomes)
            {
                // if email already exists, show this message dialog
                if (GetEmailDetails(Convert.ToString(Session["Email"])) != null && Session["Email"] != null && !"".Equals(Email.Text.Trim()))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Title", "alert('" + "This email address already exists! Please login" + "');", true);
                }
            }  
        }

        protected void RegisterUser(object sender, EventArgs e)
        {
            Session["thisCustomer"] = Email.Text;     

            // if email already exists, show this message dialog
            if (GetEmailDetails(Convert.ToString(Session["thisCustomer"])) != null && Session["thisCustomer"] != null && !"".Equals(Email.Text.Trim()))
            {
                Session.Abandon();
                Session.Clear();
                ClientScript.RegisterStartupScript(this.GetType(), "Title", "alert('" + "This email address already exists! Please login" + "');", true);
            }

            else
            {
                Home user = new Home(); // otherwise, create new object for each Home user and assign it to user

                // set each text box ID to its corresponding get/set methods in the Home class
                user.FirstName = FirstName.Text;
                user.LastName = LastName.Text;
                user.Email = Email.Text;
                user.PropertyAddressZip = ZipCode.Text;
                user.Password = Password.Text;

                allHomes.Add(user);     // add user to allHomes list
                Session["thisCustomer"] = user;

                AddNewCustomerToTable();    //add registration details to the data table
                Response.Redirect("Default.aspx");
            }
            
        }

        public Home GetEmailDetails(string eemail)
        {
            if (Session["thisCustomer"] != null)
            {
                // set up array for each user's email
                foreach (Home user in allHomes)
                {
                    if (user.Email == eemail)
                        return user;
                }
            }
            return null;
        }

        protected void AddNewCustomerToTable()
        {
            string theStatement = "INSERT INTO InsuranceCustomers (";
            theStatement += "email, password, firstName, lastName) ";
            theStatement += "VALUES (";
            theStatement += "@email, @password, @firstName, @lastName)";
            SqlConnection connectionToServer = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand insertUser = new SqlCommand(theStatement, connectionToServer);

            insertUser.Parameters.AddWithValue("@email", Email.Text);
            insertUser.Parameters.AddWithValue("@password", Password.Text);
            insertUser.Parameters.AddWithValue("@firstName", FirstName.Text);
            insertUser.Parameters.AddWithValue("@lastName", LastName.Text);

            try
            {
                connectionToServer.Open();
                insertUser.ExecuteNonQuery();
            }
            finally
            {
                connectionToServer.Close();
            }
        }
    }
}



And here is my code behind for GetAQuote.aspx:
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;

namespace WebApplication06
{
    public partial class GetAQuote : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // add state abbreviation items for stateAbbrList
                stateAbbrList.Items.Add("AK");
                stateAbbrList.Items.Add("AL");
                stateAbbrList.Items.Add("AR");
                stateAbbrList.Items.Add("AZ");
                stateAbbrList.Items.Add("CA");
                stateAbbrList.Items.Add("CO");
                stateAbbrList.Items.Add("CT");
                stateAbbrList.Items.Add("DE");
                stateAbbrList.Items.Add("FL");
                stateAbbrList.Items.Add("GA");
                stateAbbrList.Items.Add("HI");
                stateAbbrList.Items.Add("IA");
                stateAbbrList.Items.Add("ID");
                stateAbbrList.Items.Add("IL");
                stateAbbrList.Items.Add("IN");
                stateAbbrList.Items.Add("KS");
                stateAbbrList.Items.Add("KY");
                stateAbbrList.Items.Add("LA");
                stateAbbrList.Items.Add("MA");
                stateAbbrList.Items.Add("MD");
                stateAbbrList.Items.Add("ME");
                stateAbbrList.Items.Add("MI");
                stateAbbrList.Items.Add("MN");
                stateAbbrList.Items.Add("MO");
                stateAbbrList.Items.Add("MS");
                stateAbbrList.Items.Add("MT");
                stateAbbrList.Items.Add("NC");
                stateAbbrList.Items.Add("ND");
                stateAbbrList.Items.Add("NE");
                stateAbbrList.Items.Add("NV");
                stateAbbrList.Items.Add("NH");
                stateAbbrList.Items.Add("NJ");
                stateAbbrList.Items.Add("NM");
                stateAbbrList.Items.Add("NY");
                stateAbbrList.Items.Add("OH");
                stateAbbrList.Items.Add("OK");
                stateAbbrList.Items.Add("OR");
                stateAbbrList.Items.Add("PA");
                stateAbbrList.Items.Add("RI");
                stateAbbrList.Items.Add("SC");
                stateAbbrList.Items.Add("SD");
                stateAbbrList.Items.Add("TN");
                stateAbbrList.Items.Add("TX");
                stateAbbrList.Items.Add("UT");
                stateAbbrList.Items.Add("VA");
                stateAbbrList.Items.Add("VT");
                stateAbbrList.Items.Add("WA");
                stateAbbrList.Items.Add("WI");
                stateAbbrList.Items.Add("WV");
                stateAbbrList.Items.Add("WY");

                // add home type items to propertyList
                propertyList.Items.Add("-- Select --");
                propertyList.Items.Add("Single Family Home");
                propertyList.Items.Add("Apartment");
                propertyList.Items.Add("Duplex");
                propertyList.Items.Add("Condominium");
                propertyList.Items.Add("Townhome");
                propertyList.Items.Add("MobilHome");

                // add own and rent items to ownOrRentList

            }
        }

        protected void ProcQuote(object sender, EventArgs e)
        {              
            Home quote = new Home(); // otherwise, create new object for each Home user and assign it to user                   
            Session["thisCustomer"] = quote;
              
            // set each text box ID to its corresponding get/set methods in the Home class                   
            quote.PropertyAddressStreet = street.Text;
            quote.PropertyAddressCity = cityField.Text;
            quote.PropertyAddressState = stateAbbrList.SelectedValue;
            quote.PropertyAddressZip = zip.Text;
            quote.PropertyType = propertyList.SelectedValue;
            quote.ApproximateYearBuilt = Convert.ToInt16(yearBuiltField.Text);
            quote.ApproximateSqFootage = Convert.ToSingle(sqFootage.Text);
            quote.OwnOrRent = Convert.ToInt16(ownOrRentList.SelectedIndex);
            quote.LivingStatus = Convert.ToInt16(LiveOrRentButtons.SelectedItem.Value);

            Session["thisCustomer"] = quote.AnnualPremium;
            Session["thisCustomer"] = quote.MonthlyPayment;

            allHomes.Add(quote);     // add user to allHomes list
            AddPropertyInfoToTable();
                    
            Server.Transfer("YourQuote.aspx");
       }

        protected void AddPropertyInfoToTable()
        {
            Home quote = new Home();
            string theStatement = "INSERT INTO InsuranceCustomers (";
            theStatement += "propertyStreet, propertyCity, propertyState, propertyZip, propertyType, ";
            theStatement += "YearBuilt, SqFootage, ownOrRent, livingStatus) ";
            theStatement += "VALUES (";
            theStatement += "@propertyStreet, @propertyCity, @propertyState, @propertyZip, @propertyType, ";
            theStatement += "@YearBuilt, @SqFootage, @ownOrRent, @livingStatus)";
            SqlConnection connectionToServer = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand insertQuote = new SqlCommand(theStatement, connectionToServer);

            insertQuote.Parameters.AddWithValue("@propertyStreet", street.Text);
            insertQuote.Parameters.AddWithValue("@propertyCity", cityField.Text);
            insertQuote.Parameters.AddWithValue("@propertyState", stateAbbrList.Text);
            insertQuote.Parameters.AddWithValue("@propertyZip", zip.Text);
            insertQuote.Parameters.AddWithValue("@propertyType", propertyList.SelectedValue);
            insertQuote.Parameters.AddWithValue("@YearBuilt", yearBuiltField.Text);
            insertQuote.Parameters.AddWithValue("@SqFootage", sqFootage.Text);
            insertQuote.Parameters.AddWithValue("@ownOrRent", ownOrRentList.SelectedIndex);
            //insertQuote.Parameters.AddWithValue("@livingStatus", LiveOrRent.SelectedValue);
            insertQuote.Parameters.AddWithValue("@livingStatus", LiveOrRentButtons.SelectedItem.Value);
            /*
            if (live.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", live.Checked.ToString());
            else if (rent.Checked == true)
                insertQuote.Parameters.AddWithValue("@livingStatus", rent.Checked.ToString());
             */

            
         
                   
            try
            {
                connectionToServer.Open();
                insertQuote.ExecuteNonQuery();
            }
            finally
            {
                connectionToServer.Close();
            }
        }
   }        
}



The exception occured in the NewCustomerRegistration code behind. Should I try changing my INSERT statements from the GetAQuote code behind to UPDATE statements?
Was This Post Helpful? 0
  • +
  • -

#10 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 804
  • View blog
  • Posts: 3,514
  • Joined: 03-December 12

Re: Entered/Selected entries from web forms not saved into database

Posted 18 November 2013 - 05:25 PM

What line is the error on? Is this for public use, a school project, or just to learn?
Was This Post Helpful? 0
  • +
  • -

#11 gutchman84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Entered/Selected entries from web forms not saved into database

Posted 19 November 2013 - 12:01 AM

Just to learn
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1