Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.08 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Web.Mvc;
  5. using DormXpress.Models;
  6. using DormXpress.ViewModels;
  7.  
  8. namespace DormXpress.Controllers
  9. {
  10.     public class DormitoryController : Controller
  11.     {
  12.         public string ConnectionString = @"Data Source=sql7001.smarterasp.net;Initial Catalog=DB_A2AE9A_dormxpress;User ID=DB_A2AE9A_dormxpress_admin;Password=odnoc2017";
  13.         public DataTable DataTableDormitory = new DataTable();
  14.         // GET: Dormitory
  15.         public ActionResult Index()
  16.         {
  17.             if (Session["IsLoggedIn"] == null)
  18.             {
  19.                 TempData["LogInError"] = "You must log in first.";
  20.                 return RedirectToAction("Index", "Login");
  21.             }
  22.  
  23.             if (Session["IsConfirmed"] != null)
  24.             {
  25.                 TempData["NotConfirmed"] = "Your account is not yet confirmed.";
  26.                 return RedirectToAction("NotConfirmed", "Home");
  27.             }
  28.  
  29.             using (var sqlCon = new SqlConnection(ConnectionString))
  30.             {
  31.                 if (sqlCon.State == ConnectionState.Closed)
  32.                 {
  33.                     sqlCon.Open();
  34.                     DataTableDormitory.Clear();
  35.  
  36.                     var sqlDataAdapter = new SqlDataAdapter("SELECT * FROM DormDetail_T " +
  37.                                                             "WHERE UserID = @UserID", sqlCon);
  38.  
  39.                     sqlDataAdapter.SelectCommand.Parameters.AddWithValue("@UserID", Session["UserID"]);
  40.  
  41.                     sqlDataAdapter.Fill(DataTableDormitory);
  42.                     sqlCon.Close();
  43.                 }
  44.             }
  45.             return View(DataTableDormitory);
  46.         }
  47.  
  48.         // GET: Dormitory/Add
  49.         public ActionResult Add()
  50.         {
  51.             if (Session["IsLoggedIn"] == null)
  52.             {
  53.                 TempData["LogInError"] = "You must log in first.";
  54.                 return RedirectToAction("Index", "Login");
  55.             }
  56.             return View();
  57.         }
  58.  
  59.        
  60.         [HttpPost]
  61.         [ValidateAntiForgeryToken]
  62.         public ActionResult Add(Dormitory Dormitory)
  63.         {
  64.             if (!ModelState.IsValid)
  65.             {
  66.                 ViewBag.CustomId = "input-validation-error";
  67.                 return View();
  68.             }
  69.             using (var sqlCon = new SqlConnection(ConnectionString))
  70.             {
  71.                
  72.                 if (sqlCon.State == ConnectionState.Closed)
  73.                 {
  74.                     sqlCon.Open();
  75.                     string query = "INSERT INTO DormDetail_T (DormType, Price, Address, Description, Latitude, Longitude, UserID) VALUES" +
  76.                                    "(" +
  77.                                    "    @DormType, @Price, @Address, @Description, @Latitude, @Longitude, @UserID" +
  78.                                    ") ";
  79.                     var sqlCommand = new SqlCommand(query, sqlCon);
  80.  
  81.                     try
  82.                     {
  83.                         sqlCommand.Parameters.AddWithValue("@DormType", Dormitory.DormType);
  84.                         sqlCommand.Parameters.AddWithValue("@Price", Dormitory.Price);
  85.                         sqlCommand.Parameters.AddWithValue("@Address", Dormitory.Address);
  86.                         sqlCommand.Parameters.AddWithValue("@Description", Dormitory.Description);
  87.                         sqlCommand.Parameters.AddWithValue("@Latitude", Dormitory.Latitude);
  88.                         sqlCommand.Parameters.AddWithValue("@Longitude", Dormitory.Longitude);
  89.                         sqlCommand.Parameters.AddWithValue("@UserID", Session["UserID"]);
  90.  
  91.                         sqlCommand.ExecuteNonQuery();
  92.                     }
  93.                     catch (Exception e)
  94.                     {
  95.                         TempData["Error"] = "Oops! Something went wrong.";
  96.                         Console.WriteLine(e);
  97.                         return RedirectToAction("Add", FormMethod.Get);
  98.                     }
  99.                     finally
  100.                     {
  101.                         sqlCon.Close();
  102.                     }
  103.                 }
  104.             }
  105.  
  106.             return RedirectToAction("Index");
  107.         }
  108.  
  109.         // GET: Dormitory/Edit/5
  110.         public ActionResult Edit(int id)
  111.         {
  112.             return View();
  113.         }
  114.  
  115.         // POST: Dormitory/Edit/5
  116.         [HttpPost]
  117.         public ActionResult Edit(int id, FormCollection collection)
  118.         {
  119.             try
  120.             {
  121.                 // TODO: Add update logic here
  122.  
  123.                 return RedirectToAction("Index");
  124.             }
  125.             catch
  126.             {
  127.                 return View();
  128.             }
  129.         }
  130.  
  131.         // GET: Dormitory/Delete/5
  132.         public ActionResult Delete(int id)
  133.         {
  134.             return View();
  135.         }
  136.  
  137.         // POST: Dormitory/Delete/5
  138.         [HttpPost]
  139.         public ActionResult Delete(int id, FormCollection collection)
  140.         {
  141.             try
  142.             {
  143.                 // TODO: Add delete logic here
  144.  
  145.                 return RedirectToAction("Index");
  146.             }
  147.             catch
  148.             {
  149.                 return View();
  150.             }
  151.         }
  152.  
  153.         public ActionResult View(int id)
  154.         {
  155.             DormitoryViewViewModel DormitoryView = new DormitoryViewViewModel();
  156.  
  157.             using (var sqlCon = new SqlConnection(ConnectionString))
  158.             {
  159.  
  160.                 if (sqlCon.State == ConnectionState.Closed)
  161.                 {
  162.                     sqlCon.Open();
  163.                     var sqlDataAdapter = new SqlDataAdapter("SELECT * " +
  164.                                                             "FROM DormDetail_T D JOIN User_T U ON D.UserID = U.UserID " +
  165.                                                             "WHERE DormID = " + id, sqlCon);
  166.                     sqlDataAdapter.Fill(DormitoryView.DormitoryInformation);
  167.  
  168.                     DormitoryView.ID = (int) DormitoryView.DormitoryInformation.Rows[0]["DormID"];
  169.  
  170.                     sqlCon.Close();
  171.                 }
  172.             }
  173.  
  174.             return View(DormitoryView);
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement