// GET: Admin
        public ActionResult Index()
        {
            using (NewsletterEntities db = new NewsletterEntities())
            {
                //var signups = db.Signups.Where(x => x.Removed == null).ToList();
                var signups = (from c in db.Signups
                               where c.Removed == null
                               select c).ToList();
                var signupVms = new List <SignupVM>();
                foreach (var signup in signups)
                {
                    var signupVm = new SignupVM();
                    signupVm.Id           = signup.Id;
                    signupVm.FirstName    = signup.FirstName;
                    signupVm.LastName     = signup.LastName;
                    signupVm.EmailAddress = signup.EmailAddress;
                    signupVms.Add(signupVm);
                }


                return(View(signupVms));
            }
        }
        // GET: Admin
        public ActionResult Index()
        {
            using (NewsletterEntities db = new NewsletterEntities()) // instantiate connection to database
            {
                //var signups = db.SignUps.Where(x=> x.Removed == null).ToList(); ////using lambda
                // same results as the above 'lambda' but using 'link' syntax
                var signups = (from c in db.SignUps
                               where c.Removed == null
                               select c).ToList();
                var signupVms = new List <SignupVM>();
                foreach (var signup in signups)
                {
                    var signupVm = new SignupVM();
                    signupVm.Id           = signup.Id;
                    signupVm.FirstName    = signup.FirstName;
                    signupVm.LastName     = signup.LastName;
                    signupVm.EmailAddress = signup.EmailAddress;
                    signupVms.Add(signupVm);
                }

                return(View(signupVms));
            }
        }
Пример #3
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"));
        }
Пример #4
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"));
            }
        }
Пример #5
0
        // GET: Admin
        public ActionResult Index()
        {
            // The entity framework gives you access to your database.
            using (NewsletterEntities db = new NewsletterEntities())
            {
                // db.SignUps represents all of the records in our database that are subscribed
                //var signups = db.SignUps.Where(x => x.Removed == null).ToList();
                // another way to filter the database using Linq
                var signups = (from c in db.SignUps
                               where c.Removed == null
                               select c).ToList();

                // Making a new list of ViewModels that will be sent to the page.
                var signupVms = new List <SignupVM>();

                // Loop through the models and only map the properties in the view model.
                /* Why can't we just assign the viewmodel in the first place?  This extra step seems unneccessary. */
                /* Answer: It is just best practice to have a Model that maps exactly and a duplicate that is used to actually send to the page. */
                foreach (var signup in signups)
                {
                    var signupVM = new SignupVM();
                    signupVM.Id           = signup.Id;
                    signupVM.FirstName    = signup.FirstName;
                    signupVM.LastName     = signup.LastName;
                    signupVM.EmailAddress = signup.EmailAddress;
                    signupVms.Add(signupVM);
                }

                return(View(signupVms));
            }

            /*================================================
            *
            *   The code above: var signupVM = new SignupVM();
            *   makes this code below absolete.
            *
            *  ================================================*/

            //string queryString = @"SELECT * FROM SignUps";

            //List<NewsletterSignUp> signups = new List<NewsletterSignUp>();

            //using (SqlConnection connection = new SqlConnection(connectionString))
            //{
            //    // Initialize the SQL command with the query
            //    SqlCommand command = new SqlCommand(queryString, connection);

            //    // Open the connection to read
            //    connection.Open();

            //    // Make the command read from the database (because we are SELECTING)
            //    SqlDataReader reader = command.ExecuteReader();

            //    // While there is data to read, transfer the data from SQL format to C# format.
            //    // We are putting the records into the object that represents those records.
            //    while(reader.Read())
            //    {
            //        var signup = new NewsletterSignUp();
            //        signup.Id = Convert.ToInt32(reader["Id"]);
            //        signup.FirstName = reader["FirstName"].ToString();
            //        signup.LastName = reader["LastName"].ToString();
            //        signup.EmailAddress = reader["EmailAddress"].ToString();
            //        signup.SocialSecurityNumber = reader["SocialSecurityNumber"].ToString();
            //        signups.Add(signup);
            //    }
            //}
        }