0

I'm pretty new to C#, what I want to do is have the user enter data into fields and when they hit submit it writes to a database. I'm really stuck any help would be much appreciated.

Here is what I have so far.

My markup:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Your Earth, Your Home.</title>
<link rel="stylesheet" type="text/css" href="../StyleSheet.css" />
</head>
<body>
<form id="form1" runat="server">
<div class="container">
    <header>
        <img src="../logo.jpg" height="200" />
    </header>
        <nav>
        <ul>
            <li><a href="Default.aspx">Home</a></li>
            <li><a href="about-us.aspx">About Us</a></li>
            <li><a href="gallery.aspx">Image Gallery</a></li>
            <li><a href="http://your-earth-yourhome.blogspot.com.au/">Blog</a></li>
            <li><a href="subscribe/registration.aspx">Subscribe</a></li>
            <li><a href="calendar.aspx">Calendar</a></li>
            <li><a href="donate.aspx">Donate</a></li>
            <li><a href="shop.aspx">Shop</a></li>
        </ul>
    </nav>
    <section>
        <h1>Subscribe</h1>
        <div class="div1">
            <br />
            &nbsp;&nbsp;&nbsp;
            Name:
            <br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtName" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            Age:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtAge" runat="server" Width="200" Height="20">     </asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            Sex:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtSex" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
        </div>
        <div class="div2">
            &nbsp;&nbsp;&nbsp;
            Address:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtAddress" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            Email Address:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtEmail" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            Phone Number:<br />
            &nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="txtPhone" runat="server" Width="200" Height="20"></asp:TextBox>
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button1" runat="server" Text="Subscribe" Width="200" Height="30" OnClick="Button1_Click" />
            <asp:PlaceHolder ID="phSuccess" runat="server" Visible="false">
                <div id="success">
                    <p>Thank you for your registration!</p>
                </div>
            </asp:PlaceHolder>
    </section>
    <aside>
        <div class="links">
            <h3>Here are our affiliates:</h3>
            <ul>
                <li><a href="#">http://www.earthhour.org</a></li>
                <li><a href="#">http://www.50waystohelp.com</a></li>
                <li><a href="#">http://www.greenpeace.org</a></li>
                <li><a href="#">http://www.wwf.org.au/</a></li>
            </ul>
        </div>
    </aside>
    <footer>&copy; Copyright Your Earth, Your Home. All Rights Reserved</footer>
</div>
</form>
</body>
</html>

My c# code

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;
using System.Data;

public partial class subscribe_Registration : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Registration.Registration reg = new Registration.Registration();
        reg.Name = txtName.Text;
        reg.Age = Int32.Parse(txtAge.Text);
        reg.Sex = txtSex.Text;
        reg.Address = txtAddress.Text;
        reg.Email = txtEmail.Text;
        reg.Phone = Int32.Parse(txtPhone.Text);

        phSuccess.Visible = true;
    }

    public void InsertRegistration()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString);

        SqlCommand cmd = new SqlCommand("dbo.Procedure", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter parameterName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
        SqlParameter parameterAge = new SqlParameter("@Age", SqlDbType.VarChar, 50);
        SqlParameter parameterSex = new SqlParameter("@Sex", SqlDbType.VarChar, 50);
        SqlParameter parameterAddress = new SqlParameter("@Address", SqlDbType.VarChar, 50);
        SqlParameter parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 100);
        SqlParameter parameterPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 100);

        parameterName.Value = txtName;
        parameterAge.Value = txtAge;
        parameterEmail.Value = txtSex;
        parameterAddress.Value = txtAddress;
        parameterEmail.Value = txtEmail;
        parameterPhone.Value = txtPhone;

        cmd.Parameters.Add(parameterName);
        cmd.Parameters.Add(parameterAge);
        cmd.Parameters.Add(parameterSex);
        cmd.Parameters.Add(parameterAddress);
        cmd.Parameters.Add(parameterEmail);
        cmd.Parameters.Add(parameterPhone);

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
        }
    }
}

I also have a class:

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

namespace Registration
{
    public class Registration
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public int Phone { get; set; }
    }
}

I'm really stuck I'm not exactly sure where to go from here. I want to save the user data into a database. Not sure whats the best way or where to start. If you could at least point me in the right direction any help would be much appreciated.

Thanks in advance :)

13
  • what are you stuck on.. where do you even call the code that inserts into the database.. I see the code for this Event.. Button1_Click do you know how to use the debugger..? if so put some breakpoints in your code and start stepping through the code and evaluating the variables etc.... Commented Aug 26, 2016 at 16:20
  • i want to call the code with the Button1_Click. Is the best way to create a stored procedure or? I'm very new to programming atm just trying to finish an assignment. EDIT: sorry just read your whole comment, ill try using the debugger but I just need some tips on how to like save the data into a database. Commented Aug 26, 2016 at 16:23
  • Everything works properly just not sure where to go from here about saing to database Commented Aug 26, 2016 at 16:26
  • 1
    In addition to the comments above you also need to look at your parameter values. The way you have this coded you are trying to set the value of your parameters to the textboxes....you need to specify the text property of those textboxes. Commented Aug 26, 2016 at 16:36
  • 1
    If this were my code I would move that insert logic off your web page and into your Registration class. You would receive the values as parameters and the only code on your web page would be to call Registration.Insert Commented Aug 26, 2016 at 16:38

2 Answers 2

1

1.Create a Users table in SQL for storing user data and a stored procedure to insert into this table:

CREATE PROCEDURE [dbo].[Procedure]
@Name VARCHAR(50),
@Age VARCHAR(50),
@Sex VARCHAR(50),
@Address VARCHAR(50),
@Email VARCHAR(100),
@Phone VARCHAR(100)
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO Users(Name,Age,Sex,[Address],Email,Phone)
    VALUES(@Name,@Age,@Sex,@Address,@Email,@Phone)
END
GO

2.Change the code behind file like this:

  protected void Button1_Click(object sender, EventArgs e)
    {
        var reg = new Registration.Registration
        {
            Name = txtName.Text,
            Age = Int32.Parse(txtAge.Text),
            Sex = txtSex.Text,
            Address = txtAddress.Text,
            Email = txtEmail.Text,
            Phone = Int32.Parse(txtPhone.Text)
        };

        this.InsertRegistration(reg);
        phSuccess.Visible = true;
    }

    public void InsertRegistration(Registration.Registration reg)
    {
        try
        {
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
            {
                using (var cmd = new SqlCommand("dbo.Procedure", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    var parameterName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
                    var parameterAge = new SqlParameter("@Age", SqlDbType.VarChar, 50);
                    var parameterSex = new SqlParameter("@Sex", SqlDbType.VarChar, 50);
                    var parameterAddress = new SqlParameter("@Address", SqlDbType.VarChar, 50);
                    var parameterEmail = new SqlParameter("@Email", SqlDbType.VarChar, 100);
                    var parameterPhone = new SqlParameter("@Phone", SqlDbType.VarChar, 100);

                    parameterName.Value = reg.Sex;
                    parameterAge.Value = reg.Age;
                    parameterSex.Value = reg.Email;
                    parameterAddress.Value = reg.Address;
                    parameterEmail.Value = reg.Email;
                    parameterPhone.Value = reg.Phone;

                    cmd.Parameters.Add(parameterName);
                    cmd.Parameters.Add(parameterAge);
                    cmd.Parameters.Add(parameterSex);
                    cmd.Parameters.Add(parameterAddress);
                    cmd.Parameters.Add(parameterEmail);
                    cmd.Parameters.Add(parameterPhone);

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Denys, your help is much appreciated :)
Im getting an error on the code "The type or namespace name 'Registration' could not be found"
So add a correct using statement to your code behind file so that it knows where the Registration class resides
ahhh yes, thats what I forgot. Thanks for your help. Seemed to do the trick I have given you the answer :)
1

you should use breakpoints in your .cs code. most common mistake in this kind of program is that name of the field in database or .cs file are slightly different, b'coz of that server did not get the value for every field in your database. this gives an error.

So proper use of breakpoints and use debugger to debugg your program, so that u found at which point u got an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.