예제 #1
0
 public ActionResult CreatePartial(Task task)
 {
     if (ModelState.IsValid)
     {
         task.UserName = User.Identity.Name;
         _repo.InsertTask(task);
         return Json("passed");
     }
     return Json("failed");
 }
예제 #2
0
        public void EditTask(Task task)
        {
            int res = 0; //return value from sql
            SqlCommand cmd = null;
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter( "@id", task.Id),
                new SqlParameter("@title", task.Title ?? ""),
                new SqlParameter("@description", task.Description ?? ""),
                new SqlParameter("@note", task.Note ?? ""),
                new SqlParameter("@finished", task.Finished),
                new SqlParameter("@date", task.DueDate),
                new SqlParameter("@user", task.UserName ?? "")
            };

            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "UPDATE tasks SET UserName = @user, Title = @title, Description = @description, Note = @note, DueDate = @date, Finished = @finished WHERE id = @id";
            cmd.Parameters.AddRange(parameters);

            try
            {
                con.Open();
                res = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
            }
        }
예제 #3
0
        public void InsertTask(Task task)
        {
            int res = 0; //return value from sql
            SqlCommand cmd = null;
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@title", task.Title ?? ""),
                new SqlParameter("@description", task.Description ?? ""),
                new SqlParameter("@note", task.Note ?? ""),
                new SqlParameter("@finished", task.Finished),
                new SqlParameter("@date", task.DueDate),
                new SqlParameter("@user", task.UserName ?? "")
            };

            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO tasks VALUES ( @user, @title, @description, @note, @date, @finished)";
            cmd.Parameters.AddRange(parameters);

            try
            {
                con.Open();
                res = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
            }
        }
예제 #4
0
        public Task GetTaskById(int id)
        {
            Task result = new Task();
            SqlCommand cmd = null;
            SqlParameter param = new SqlParameter("@id", id);
            DataTable table = new DataTable();

            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM tasks WHERE id=@id";
            cmd.Parameters.Add(param);

            try
            {
                con.Open();
                SqlDataAdapter da = null;
                using (da = new SqlDataAdapter(cmd))
                {
                    da.Fill(table);
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
            }

            result = (from DataRow row in table.Rows
                      select new Task
                      {
                          Id = Convert.ToInt32(row["Id"]),
                          Title = row["Title"].ToString(),
                          Description = row["Description"].ToString(),
                          Note = row["Note"].ToString(),
                          DueDate = Convert.ToDateTime(row["DueDate"]),
                          Finished = Convert.ToBoolean(row["Finished"]),
                          UserName = row["UserName"].ToString()
                      }).SingleOrDefault();
            return result;
        }
예제 #5
0
 public ActionResult Edit(Task task)
 {
     if (ModelState.IsValid)
     {
         task.UserName = User.Identity.Name;
         _repo.EditTask(task);
         return RedirectToAction("Index");
     }
     return View(task);
 }
예제 #6
0
 public void InsertTask(Task task)
 {
     db.Tasks.Add(task);
     db.SaveChanges();
 }
예제 #7
0
 public void EditTask(Task task)
 {
     db.Entry(task).State = EntityState.Modified;
     db.SaveChanges();
 }