Guest User

Untitled

a guest
Feb 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public class SweepstakesSubmission
  13. {
  14. public int Id;
  15.  
  16. private string _FirstName;
  17. public string FirstName
  18. {
  19. get
  20. {
  21. return _FirstName;
  22. }
  23. set
  24. {
  25. if (String.IsNullOrEmpty(value))
  26. throw new Exception("You must supply your first and last name");
  27. else
  28. _FirstName = value;
  29. }
  30. }
  31.  
  32. private string _LastName;
  33. public string LastName
  34. {
  35. get
  36. {
  37. return _LastName;
  38. }
  39. set
  40. {
  41. if (String.IsNullOrEmpty(value))
  42. throw new Exception("You must supply your first and last name");
  43. else
  44. _LastName = value;
  45. }
  46. }
  47.  
  48. private string _Email;
  49. public string Email
  50. {
  51. get { return _Email; }
  52. set
  53. {
  54.  
  55. try
  56. {
  57. System.Net.Mail.MailAddress Address = new System.Net.Mail.MailAddress(value);
  58. _Email = value;
  59.  
  60. }
  61. catch { throw new Exception("The email address supplied appears to be invalid."); }
  62. }
  63. }
  64.  
  65. private string _ZipCode;
  66. public string ZipCode
  67. {
  68. get { return _ZipCode; }
  69. set
  70. {
  71. if ((String.IsNullOrEmpty(value)) || (value.Length < 4))
  72. throw new Exception("Please enter your Zip Code");
  73. else
  74. _ZipCode = value;
  75. }
  76. }
  77.  
  78. public void Record()
  79. {
  80. try
  81. {
  82. SqlConnection objConnection = new SqlConnection();
  83. objConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Sweepstakes"].ConnectionString;
  84. objConnection.Open();
  85.  
  86. SqlCommand objCommand = new SqlCommand(
  87. "INSERT INTO " + ConfigurationManager.AppSettings["SweepstakesSubmission_DbName"] + " (FirstName, LastName, Email, ZipCode, UserAgent, IpAddress, TimeStamp) VALUES (@FirstName,@LastName,@Email,@ZipCode,@UserAgent,@IpAddress,GetDate());",
  88. objConnection);
  89. objCommand.Parameters.AddWithValue("@FirstName", this.FirstName);
  90. objCommand.Parameters.AddWithValue("@LastName", this.LastName);
  91. objCommand.Parameters.AddWithValue("@Email", this.Email);
  92. objCommand.Parameters.AddWithValue("@ZipCode", this.ZipCode);
  93. objCommand.Parameters.AddWithValue("@UserAgent", HttpContext.Current.Request.ServerVariables["http_user_agent"].ToString());
  94. objCommand.Parameters.AddWithValue("@IpAddress", HttpContext.Current.Request.ServerVariables["remote_addr"].ToString());
  95.  
  96. if (!(objCommand.ExecuteNonQuery() == 1))
  97. throw new Exception();
  98.  
  99. objCommand = null;
  100.  
  101. objConnection.Close();
  102. objConnection = null;
  103. }
  104. catch (Exception Ex) { throw new Exception("The record could not be recorded to the database."); }
  105. }
  106. }
Add Comment
Please, Sign In to add comment