public ActionResult SignUp(string firstName, string lastName, string emailAddress)
        {
            if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName) || string.IsNullOrEmpty(emailAddress))
            {
                //NOTE: Tilda('~') is the root of this project.
                return(View("~/Views/Shared/Error.cshtml"));
            }
            else
            {
                using (NewsletterEntities db = new NewsletterEntities())
                {
                    //NOTE: Here are instantiating an object from the 'SignUp' class that 'EntityFramwork' created for us.
                    var signup = new SignUp();
                    signup.FirstName    = firstName;
                    signup.LastName     = lastName;
                    signup.EmailAddress = emailAddress;

                    //NOTE: Here we taking that object and adding its property values to create a database record.
                    db.SignUps.Add(signup);

                    //NOTE: After adding the objects to the databse, saves changes needs to get called on the
                    //      'db' object or else it won't save those changes to the database.
                    db.SaveChanges();
                }
                return(View("Success"));
            }
        }
 public ActionResult Unsubscribe(int Id)
 {
     using (NewsletterEntities db = new NewsletterEntities())
     {
         var signup = db.SignUps.Find(Id);
         signup.Removed = DateTime.Now;
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
Exemplo n.º 3
0
        public ActionResult Unsubscribe(int Id)
        {
            using (NewsletterEntities db = new NewsletterEntities())
            {
                var signup = db.SignUps.Find(Id);
                signup.Removed = DateTime.Now;  // Doesn't this need to be in SQL datetime format?
                db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
 public ActionResult Unsubscribe(int Id)
 {
     using (NewsletterEntities db = new NewsletterEntities())
     {
         //NOTE: The 'Find' method takes in a primary key value and looks for the record
         //      (now an object) that matches that key (property Id) and then returns the
         //      record 'SignUp' object to 'signup'. If it doesn't find that key, it will
         //      return null.
         var signup = db.SignUps.Find(Id);
         signup.Removed = DateTime.Now;
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
Exemplo n.º 5
0
        public ActionResult SignUp(string firstName, string lastName, string emailAddress)
        {
            if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName) || string.IsNullOrEmpty(emailAddress))
            {
                return(View("~/Views/Shared/Error.cshtml"));
            }
            else
            {
                using (NewsletterEntities db = new NewsletterEntities())
                {
                    var signup = new SignUp();
                    signup.FirstName    = firstName;
                    signup.LastName     = lastName;
                    signup.EmailAddress = emailAddress;

                    db.SignUps.Add(signup);
                    db.SaveChanges();
                }
                //string queryString = @"INSERT INTO SignUps (FirstName, LastName, EmailAddress) VALUES
                //                        (@FirstName, @LastName, @EmailAddress)";


                //using (SqlConnection connection = new SqlConnection(connectionString))
                //{
                //    SqlCommand command = new SqlCommand(queryString, connection);
                //    command.Parameters.Add("@FirstName", SqlDbType.VarChar);
                //    command.Parameters.Add("@LastName", SqlDbType.VarChar);
                //    command.Parameters.Add("@EmailAddress", SqlDbType.VarChar);

                //    command.Parameters["@FirstName"].Value = firstName;
                //    command.Parameters["@LastName"].Value = lastName;
                //    command.Parameters["@EmailAddress"].Value = emailAddress;

                //    connection.Open();
                //    command.ExecuteNonQuery();
                //    connection.Close();


                //}

                return(View("Success"));
            }
        }
 public ActionResult SignUp(string firstName, string lastName, string emailAddress)
 {
     if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName) || string.IsNullOrEmpty(emailAddress))
     {
         return(View("~/Views/Shared/Error.cshtml"));
     }
     else
     {
         using (NewsletterEntities db = new NewsletterEntities())
         {
             var signup = new SignUp();
             signup.FirstName    = firstName;
             signup.LastName     = lastName;
             signup.EmailAddress = emailAddress;
             db.SignUps.Add(signup);
             db.SaveChanges();
         }
         return(View("Success"));
     }
 }
Exemplo n.º 7
0
        public ActionResult SignUp(string firstName, string lastName, string emailAddress) // SignUp: method, SingUp.cs in Models
        {
            if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName) || string.IsNullOrEmpty(emailAddress))
            {
                return(View("~/Views/Shared/Error.cshtml"));
            }
            else
            {
                // The following shows a simple line replaces the detailed db connection (ADO.NET) by using EF (EntityFramework)
                using (NewsletterEntities db = new NewsletterEntities())
                {
                    var signup = new SignUp();       // SignUp: Model > SignUp.cs (class, datatype)
                    signup.FirstName    = firstName; // map the property for the object to the parameter that came in
                    signup.LastName     = lastName;
                    signup.EmailAddress = emailAddress;

                    db.SignUps.Add(signup); // SignUps (Models > Model1.Context.tt > Model1.Context.cs: public virtual DbSet<SignUp> SignUps { get; set; })
                    db.SaveChanges();
                }
                // The following is db connection without Entity framework
                //string queryString = @"INSERT INTO SignUps (FirstName, LastName, EmailAddress) VALUES (@FirstName, @LastName, @EmailAddress)"; // prevent raw sql input

                //using (SqlConnection connection = new SqlConnection(connectionString)) // use 'using' statement to cut off the connection when done to prevent memory leak
                //{
                //    SqlCommand command = new SqlCommand(queryString, connection);
                //    command.Parameters.Add("@FirstName", SqlDbType.VarChar);
                //    command.Parameters.Add("@LastName", SqlDbType.VarChar);
                //    command.Parameters.Add("@EmailAddress", SqlDbType.VarChar);

                //    command.Parameters["@FirstName"].Value = firstName;
                //    command.Parameters["@LastName"].Value = lastName;
                //    command.Parameters["@EmailAddress"].Value = emailAddress;

                //    connection.Open();
                //    command.ExecuteNonQuery();
                //    connection.Close();
                //}

                return(View("Success"));
            }
        }
Exemplo n.º 8
0
        public ActionResult Subscribe(string firstName, string lastName, string emailAddress)
        {
            if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName) || string.IsNullOrEmpty(emailAddress))
            {
                return(View("~/Views/Shared/Error.cshtml"));
            }
            else
            {
                using (NewsletterEntities db = new NewsletterEntities())
                {
                    var subscription = new Subscription();
                    subscription.FirstName    = firstName;
                    subscription.LastName     = lastName;
                    subscription.EmailAddress = emailAddress;

                    db.Subscriptions.Add(subscription);
                    db.SaveChanges();
                }
                return(View("Success"));
            }
        }
Exemplo n.º 9
0
        public ActionResult SignUp(string firstName, string lastName, string emailAddress)
        {
            // The ~ indicates a relative path
            if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName) || string.IsNullOrEmpty(emailAddress))
            {
                return(View("~/Views/Shared/Error.cshtml"));
            }
            else
            {
                using (NewsletterEntities db = new NewsletterEntities())
                {
                    // Creating a new signup record to add it to the database.
                    var signup = new SignUp();
                    signup.FirstName    = firstName;
                    signup.LastName     = lastName;
                    signup.EmailAddress = emailAddress;

                    db.SignUps.Add(signup);
                    db.SaveChanges();
                }


                /*================================================
                *
                *   The code above makes this code below absolete.
                *
                *  ================================================*/


                ///*

                //    Using ADO.NET to connect to Database

                // */

                //// The query that will be passed to the database.
                //// We use parameters, @, to prevent SQL injections
                //string queryString = @"INSERT INTO SignUps (FirstName, LastName, EmailAddress)
                //                       VALUES (@FirstName, @LastName, @EmailAddress)";

                //// When you are connection to a database, be sure to use 'using' so that the connection is stopped when you are done.
                //// Use SqlConnection to connect to your SQL database and pass in the connection string.
                //using (SqlConnection connection = new SqlConnection(connectionString))
                //{
                //    // SqlCommand will actually perform the query with.  Needs the query and connection to the database.
                //    SqlCommand command = new SqlCommand(queryString, connection);

                //    // Add the parameters after you make SqlCommand.
                //    // Parameters.Add() needs the name of the parameter (including @) and the type of data it is.
                //    command.Parameters.Add("@FirstName", SqlDbType.VarChar);
                //    command.Parameters.Add("@LastName", SqlDbType.VarChar);
                //    command.Parameters.Add("@EmailAddress", SqlDbType.VarChar);

                //    // After the parameters are added, we can add their values.
                //    // Remember this is a method that is called when the user posts, so their data is represented as parameters
                //    // We use the parameters of this method as the values for the SQL parameters.
                //    command.Parameters["@FirstName"].Value = firstName;
                //    command.Parameters["@LastName"].Value = lastName;
                //    command.Parameters["@EmailAddress"].Value = emailAddress;

                //    // Now that the SQL command is ready, we can open the connection and execute it.
                //    connection.Open();
                //    command.ExecuteNonQuery();
                //    connection.Close();
                //}
                return(View("Success"));
            }
        }