Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Web.Mvc;
- using DormXpress.Models;
- using DormXpress.ViewModels;
- namespace DormXpress.Controllers
- {
- public class DormitoryController : Controller
- {
- public string ConnectionString = @"Data Source=sql7001.smarterasp.net;Initial Catalog=DB_A2AE9A_dormxpress;User ID=DB_A2AE9A_dormxpress_admin;Password=odnoc2017";
- public DataTable DataTableDormitory = new DataTable();
- // GET: Dormitory
- public ActionResult Index()
- {
- if (Session["IsLoggedIn"] == null)
- {
- TempData["LogInError"] = "You must log in first.";
- return RedirectToAction("Index", "Login");
- }
- if (Session["IsConfirmed"] != null)
- {
- TempData["NotConfirmed"] = "Your account is not yet confirmed.";
- return RedirectToAction("NotConfirmed", "Home");
- }
- using (var sqlCon = new SqlConnection(ConnectionString))
- {
- if (sqlCon.State == ConnectionState.Closed)
- {
- sqlCon.Open();
- DataTableDormitory.Clear();
- var sqlDataAdapter = new SqlDataAdapter("SELECT * FROM DormDetail_T " +
- "WHERE UserID = @UserID", sqlCon);
- sqlDataAdapter.SelectCommand.Parameters.AddWithValue("@UserID", Session["UserID"]);
- sqlDataAdapter.Fill(DataTableDormitory);
- sqlCon.Close();
- }
- }
- return View(DataTableDormitory);
- }
- // GET: Dormitory/Add
- public ActionResult Add()
- {
- if (Session["IsLoggedIn"] == null)
- {
- TempData["LogInError"] = "You must log in first.";
- return RedirectToAction("Index", "Login");
- }
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Add(Dormitory Dormitory)
- {
- if (!ModelState.IsValid)
- {
- ViewBag.CustomId = "input-validation-error";
- return View();
- }
- using (var sqlCon = new SqlConnection(ConnectionString))
- {
- if (sqlCon.State == ConnectionState.Closed)
- {
- sqlCon.Open();
- string query = "INSERT INTO DormDetail_T (DormType, Price, Address, Description, Latitude, Longitude, UserID) VALUES" +
- "(" +
- " @DormType, @Price, @Address, @Description, @Latitude, @Longitude, @UserID" +
- ") ";
- var sqlCommand = new SqlCommand(query, sqlCon);
- try
- {
- sqlCommand.Parameters.AddWithValue("@DormType", Dormitory.DormType);
- sqlCommand.Parameters.AddWithValue("@Price", Dormitory.Price);
- sqlCommand.Parameters.AddWithValue("@Address", Dormitory.Address);
- sqlCommand.Parameters.AddWithValue("@Description", Dormitory.Description);
- sqlCommand.Parameters.AddWithValue("@Latitude", Dormitory.Latitude);
- sqlCommand.Parameters.AddWithValue("@Longitude", Dormitory.Longitude);
- sqlCommand.Parameters.AddWithValue("@UserID", Session["UserID"]);
- sqlCommand.ExecuteNonQuery();
- }
- catch (Exception e)
- {
- TempData["Error"] = "Oops! Something went wrong.";
- Console.WriteLine(e);
- return RedirectToAction("Add", FormMethod.Get);
- }
- finally
- {
- sqlCon.Close();
- }
- }
- }
- return RedirectToAction("Index");
- }
- // GET: Dormitory/Edit/5
- public ActionResult Edit(int id)
- {
- return View();
- }
- // POST: Dormitory/Edit/5
- [HttpPost]
- public ActionResult Edit(int id, FormCollection collection)
- {
- try
- {
- // TODO: Add update logic here
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
- // GET: Dormitory/Delete/5
- public ActionResult Delete(int id)
- {
- return View();
- }
- // POST: Dormitory/Delete/5
- [HttpPost]
- public ActionResult Delete(int id, FormCollection collection)
- {
- try
- {
- // TODO: Add delete logic here
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
- public ActionResult View(int id)
- {
- DormitoryViewViewModel DormitoryView = new DormitoryViewViewModel();
- using (var sqlCon = new SqlConnection(ConnectionString))
- {
- if (sqlCon.State == ConnectionState.Closed)
- {
- sqlCon.Open();
- var sqlDataAdapter = new SqlDataAdapter("SELECT * " +
- "FROM DormDetail_T D JOIN User_T U ON D.UserID = U.UserID " +
- "WHERE DormID = " + id, sqlCon);
- sqlDataAdapter.Fill(DormitoryView.DormitoryInformation);
- DormitoryView.ID = (int) DormitoryView.DormitoryInformation.Rows[0]["DormID"];
- sqlCon.Close();
- }
- }
- return View(DormitoryView);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement