public Treneris getTreneris(int id) { Treneris treneris = new Treneris(); string connection = ConfigurationManager.ConnectionStrings["MysqlConnection"].ConnectionString; MySqlConnection mySqlConnection = new MySqlConnection(connection); string sqlquery = "SELECT * FROM treneris where id__TRENERIS=?id"; MySqlCommand mySqlCommand = new MySqlCommand(sqlquery, mySqlConnection); mySqlCommand.Parameters.Add("?id", MySqlDbType.Int32).Value = id; mySqlConnection.Open(); MySqlDataAdapter mda = new MySqlDataAdapter(mySqlCommand); DataTable dt = new DataTable(); mda.Fill(dt); mySqlConnection.Close(); foreach (DataRow item in dt.Rows) { treneris.Vardas = Convert.ToString(item["Vardas"]); treneris.Pavarde = Convert.ToString(item["Pavarde"]); treneris.Tautybe = Convert.ToString(item["Tautybe"]); treneris.id = Convert.ToInt32(item["id__TRENERIS"]); } return(treneris); }
public ActionResult Delete(int id, FormCollection collection) { try { Treneris treneris = trenerisRepository.getTreneris(id); List <Asistentas> asistentas = trenerisRepository.getAsistentai(id); if (asistentas.Count > 0) { ViewBag.klaida = "Negalima ištrinti trenerio, nes jis turi asistentus"; return(View(treneris)); } else { trenerisRepository.deleteTreneris(id); } return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Edit(Treneris treneris) { try { if (ModelState.IsValid) { trenerisRepository.updateTreneris(treneris); } return(RedirectToAction("Index")); } catch { return(View(treneris)); } }
public bool updateTreneris(Treneris treneris) { try { string connection = ConfigurationManager.ConnectionStrings["MysqlConnection"].ConnectionString; MySqlConnection mySqlConnection = new MySqlConnection(connection); string sqlquery = @"UPDATE treneris a SET a.Vardas=?vardas, a.Pavarde=?pavarde, a.Tautybe=?tautybe WHERE a.id__TRENERIS=?id"; MySqlCommand mySqlCommand = new MySqlCommand(sqlquery, mySqlConnection); mySqlCommand.Parameters.Add("?id", MySqlDbType.Int32).Value = treneris.id; mySqlCommand.Parameters.Add("?vardas", MySqlDbType.VarChar).Value = treneris.Vardas; mySqlCommand.Parameters.Add("?pavarde", MySqlDbType.VarChar).Value = treneris.Pavarde; mySqlCommand.Parameters.Add("?tautybe", MySqlDbType.VarChar).Value = treneris.Tautybe; mySqlConnection.Open(); mySqlCommand.ExecuteNonQuery(); mySqlConnection.Close(); return(true); } catch (Exception) { return(false); } }
public bool addTreneris(Treneris treneris) { try { string connection = ConfigurationManager.ConnectionStrings["MysqlConnection"].ConnectionString; MySqlConnection mySqlConnection = new MySqlConnection(connection); string sqlquery = @"INSERT INTO treneris(Vardas,Pavarde,Tautybe,id__TRENERIS) VALUES(?vardas,?pavarde,?tautybe,?id);"; 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("?tautybe", MySqlDbType.VarChar).Value = treneris.Tautybe; mySqlCommand.Parameters.Add("?id", MySqlDbType.Int32).Value = getLastID(); mySqlConnection.Open(); mySqlCommand.ExecuteNonQuery(); mySqlConnection.Close(); return(true); } catch (Exception) { return(false); } }
public ActionResult Create() { Treneris treneris = new Treneris(); return(View(treneris)); }