Advertisement
Guest User

.NET/ASP/C# Web Site : Database Register Form

a guest
Feb 11th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 2.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using Model;
  8. using System.Text;
  9.  
  10. public partial class Register_Default : System.Web.UI.Page
  11. {
  12.  
  13.     ModelContainer db;
  14.  
  15.     protected void respond(object output)
  16.     {
  17.         // Response output using StringBuilder
  18.         var res = new StringBuilder();
  19.         res.Append(output);
  20.         ctrlRes.Text = res.ToString();
  21.     }
  22.  
  23.     protected void Page_Load(object sender, EventArgs e)
  24.     {
  25.  
  26.     }
  27.     protected void Button1_Click(object sender, EventArgs e)
  28.     {
  29.         // Check all fields are not null
  30.         if (firstName.Text != "" && lastName.Text != "" && howOld.Text != "" && emailAddress.Text != "")
  31.         {
  32.             try
  33.             {
  34.                 // Attempt to int age
  35.                 int age = Int32.Parse(howOld.Text);
  36.                 // Check age limit
  37.                 if (age <= 17)
  38.                 {
  39.                     // Under age - Error handler
  40.                     respond("<div style='color: red; font-weight: bold;'> Sorry, you're not old enough to register... </div>");
  41.                 }
  42.                 else
  43.                 {
  44.                     db = new ModelContainer();
  45.                     // Query selector
  46.                     var tbl = db.tbltst_table.Where(c => c.email == emailAddress.Text).FirstOrDefault();
  47.                     // Check it is taken or not
  48.                     if (tbl != null && !string.IsNullOrEmpty(tbl.name))
  49.                     {
  50.                         // Email exists - Error handler
  51.                         respond("<div style='color: red; font-weight: bold;'> This email already exists, please <a href='/login/'> login</a> to continue... </div>");
  52.                     }
  53.                     else
  54.                     {
  55.                         // Insert the new record
  56.                         tbltst_table nadd = db.tbltst_table.CreateObject();
  57.                         nadd.name = firstName.Text;
  58.                         nadd.email = emailAddress.Text;
  59.                         nadd.surname = lastName.Text;
  60.                         nadd.age = age;
  61.                         db.tbltst_table.AddObject(nadd);
  62.                         db.SaveChanges();
  63.                         // Completed - Success handler
  64.                         respond("<div style='color: green; font-weight: bold;'> Successfully registered, you can now <a href='/login/'> login</a>... </div>");
  65.                     }
  66.                 }
  67.             }
  68.             catch (Exception)
  69.             {
  70.                 // Age is not as an integer - Handle error
  71.                 respond("<div style='color: red; font-weight: bold;'> Please state your age numericaly ie: 18... </div>");
  72.             }
  73.         }
  74.         else
  75.         {
  76.             // Missing required fields - Handle error
  77.             respond("<div style='color: red; font-weight: bold;'> Please fill in the required fields... </div>");
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement