Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using Model;
- using System.Text;
- public partial class Register_Default : System.Web.UI.Page
- {
- ModelContainer db;
- protected void respond(object output)
- {
- // Response output using StringBuilder
- var res = new StringBuilder();
- res.Append(output);
- ctrlRes.Text = res.ToString();
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- // Check all fields are not null
- if (firstName.Text != "" && lastName.Text != "" && howOld.Text != "" && emailAddress.Text != "")
- {
- try
- {
- // Attempt to int age
- int age = Int32.Parse(howOld.Text);
- // Check age limit
- if (age <= 17)
- {
- // Under age - Error handler
- respond("<div style='color: red; font-weight: bold;'> Sorry, you're not old enough to register... </div>");
- }
- else
- {
- db = new ModelContainer();
- // Query selector
- var tbl = db.tbltst_table.Where(c => c.email == emailAddress.Text).FirstOrDefault();
- // Check it is taken or not
- if (tbl != null && !string.IsNullOrEmpty(tbl.name))
- {
- // Email exists - Error handler
- respond("<div style='color: red; font-weight: bold;'> This email already exists, please <a href='/login/'> login</a> to continue... </div>");
- }
- else
- {
- // Insert the new record
- tbltst_table nadd = db.tbltst_table.CreateObject();
- nadd.name = firstName.Text;
- nadd.email = emailAddress.Text;
- nadd.surname = lastName.Text;
- nadd.age = age;
- db.tbltst_table.AddObject(nadd);
- db.SaveChanges();
- // Completed - Success handler
- respond("<div style='color: green; font-weight: bold;'> Successfully registered, you can now <a href='/login/'> login</a>... </div>");
- }
- }
- }
- catch (Exception)
- {
- // Age is not as an integer - Handle error
- respond("<div style='color: red; font-weight: bold;'> Please state your age numericaly ie: 18... </div>");
- }
- }
- else
- {
- // Missing required fields - Handle error
- respond("<div style='color: red; font-weight: bold;'> Please fill in the required fields... </div>");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement