public TrenerisEditViewModel getTreneris(int id) { TrenerisEditViewModel modelis = new TrenerisEditViewModel(); string conn = ConfigurationManager.ConnectionStrings["MysqlConnection"].ConnectionString; MySqlConnection mySqlConnection = new MySqlConnection(conn); string query = @"SELECT `vardas`, `pavarde`, `amzius`, `slapyvardis`, `id_TRENERIS` FROM treneris a WHERE a.id_TRENERIS =" + id; MySqlCommand mySqlCommand = new MySqlCommand(query, mySqlConnection); mySqlConnection.Open(); MySqlDataAdapter mda = new MySqlDataAdapter(mySqlCommand); DataTable dt = new DataTable(); mda.Fill(dt); mySqlConnection.Close(); foreach (DataRow item in dt.Rows) { modelis.id_TRENERIS = Convert.ToInt32(item["id_TRENERIS"]); modelis.vardas = Convert.ToString(item["vardas"]); modelis.pavarde = Convert.ToString(item["pavarde"]); modelis.slapyvadis = Convert.ToString(item["slapyvardis"]); modelis.amzius = Convert.ToInt32(item["amzius"]); } return(modelis); }
public ActionResult Edit(int id, TrenerisEditViewModel treneris) { try { //zaidimas.id_ZAIDIMAS = id; treneriai.updateTreneris(treneris); return(RedirectToAction("Index")); } catch { return(View(treneris)); } }
public ActionResult Delete(int id, TrenerisEditViewModel treneris) { try { // TODO: Add delete logic here treneriai.deleteTreneris(id); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Create(TrenerisEditViewModel treneris) { try { if (ModelState.IsValid) { int trenerioId = treneriai.addTreneris(treneris); if (trenerioId < 0) { ViewBag.failed = "Nepavyko iterpti"; return(View(treneris)); } } return(RedirectToAction("Index")); } catch { return(View()); } }
public int addTreneris(TrenerisEditViewModel treneris) { int insertedId = -1; string conn = ConfigurationManager.ConnectionStrings["MysqlConnection"].ConnectionString; MySqlConnection mySqlConnection = new MySqlConnection(conn); string query = @"INSERT INTO `treneris`(`vardas`, `pavarde`, `amzius`, `slapyvardis`, `id_TRENERIS`) VALUES(?vardas, ?pavarde, ?amzius, ?slapyvardis, NULL)"; MySqlCommand mySqlCommand = new MySqlCommand(query, mySqlConnection); mySqlCommand.Parameters.Add("?vardas", MySqlDbType.VarChar).Value = treneris.vardas; mySqlCommand.Parameters.Add("?amzius", MySqlDbType.Int32).Value = treneris.amzius; mySqlCommand.Parameters.Add("?pavarde", MySqlDbType.VarChar).Value = treneris.pavarde; mySqlCommand.Parameters.Add("?slapyvardis", MySqlDbType.VarChar).Value = treneris.slapyvadis; mySqlConnection.Open(); mySqlCommand.ExecuteNonQuery(); mySqlConnection.Close(); insertedId = Convert.ToInt32(mySqlCommand.LastInsertedId); return(insertedId); }
public bool updateTreneris(TrenerisEditViewModel treneris) { string conn = ConfigurationManager.ConnectionStrings["MysqlConnection"].ConnectionString; MySqlConnection mySqlConnection = new MySqlConnection(conn); string sqlquery = @"UPDATE `treneris` SET `vardas`=?vardas, `pavarde`=?pavarde, `amzius`=?amzius, `slapyvardis`=?slapyvardis WHERE id_TRENERIS =" + treneris.id_TRENERIS; MySqlCommand mySqlCommand = new MySqlCommand(sqlquery, mySqlConnection); mySqlCommand.Parameters.Add("?vardas", MySqlDbType.VarChar).Value = treneris.vardas; mySqlCommand.Parameters.Add("?pavarde", MySqlDbType.VarChar).Value = treneris.pavarde; mySqlCommand.Parameters.Add("?slapyvardis", MySqlDbType.VarChar).Value = treneris.slapyvadis; mySqlCommand.Parameters.Add("?amzius", MySqlDbType.Int32).Value = treneris.amzius; mySqlConnection.Open(); mySqlCommand.ExecuteNonQuery(); mySqlConnection.Close(); return(true); }
// GET: Treneris/Delete/5 public ActionResult Delete(int id) { TrenerisEditViewModel treneris = treneriai.getTreneris(id); return(View(treneris)); }
// GET: Treneris/Create public ActionResult Create() { TrenerisEditViewModel treneris = new TrenerisEditViewModel(); return(View(treneris)); }