public ActionResult Edit(TextModel model) { if (ModelState.IsValid) { // Attempt to save the text try { S_Text text = TextManager.GetTextById(model.Id); text.text = model.text; text.id = model.Id; TextManager.Update(text); return(RedirectToAction("index", "text", null)); } catch (Exception e) { TempData["error"] = "Er is een fout opgetreden"; } } // If we got this far, something failed, redisplay form return(View(model)); }
//Update statement public static void Update(S_Text text) { try { DatabaseConnection databaseconnection = new DatabaseConnection(); //open connection if (databaseconnection.OpenConnection()) { //create command and assign the query and connection from the constructor MySqlCommand command = new MySqlCommand(); command.Connection = databaseconnection.getConnection(); command.CommandText = "UPDATE text SET label=@label, text=@text WHERE id=@id "; command.Parameters.AddWithValue("@id", Conversion.LongToSql(text.id)); command.Parameters.AddWithValue("@label", Conversion.StringToSql(text.label)); command.Parameters.AddWithValue("@text", Conversion.StringToSql(text.text)); //Execute command command.ExecuteNonQuery(); //close connection databaseconnection.CloseConnection(); } } catch (Exception ex) { logger.Error(string.Format("Update, Error updating text data: {0}", ex.Message)); } }
private static S_Text DataToObject(MySqlDataReader dataReader) { S_Text text = new S_Text(); text.id = Conversion.SqlToIntOrNull(dataReader["id"]).Value; text.label = Conversion.SqlToString(dataReader["label"]); text.text = Conversion.SqlToString(dataReader["text"]); return(text); }
public ActionResult Edit(long id) { S_Text text = TextManager.GetTextById(id); TextModel textModel = new TextModel(); textModel.Id = text.id; textModel.label = text.label; textModel.text = text.text; return(View(textModel)); }
public static S_Text GetTextById(long id) { S_Text text = null; try { DatabaseConnection databaseconnection = new DatabaseConnection(); //Open connection if (databaseconnection.OpenConnection()) { //Create Command MySqlCommand command = new MySqlCommand(); command.Connection = databaseconnection.getConnection(); command.CommandText = "SELECT * FROM text WHERE id=@id"; command.Parameters.AddWithValue("@id", Conversion.LongToSql(id)); //Create a data reader and Execute the command MySqlDataReader dataReader = command.ExecuteReader(); //Read the data and store them in the object if (dataReader.Read()) { text = DataToObject(dataReader); } //close Data Reader dataReader.Close(); //close Connection databaseconnection.CloseConnection(); } } catch (Exception ex) { logger.Error(string.Format("GetTextsById, Error reading text data: {0}", ex.Message)); } return(text); }
//Insert statement public static long?Insert(S_Text text) { long?lastInsertedId = null; try { DatabaseConnection databaseconnection = new DatabaseConnection(); //open connection if (databaseconnection.OpenConnection()) { //create command and assign the query and connection from the constructor MySqlCommand command = new MySqlCommand(); command.Connection = databaseconnection.getConnection(); command.CommandText = "INSERT INTO text ( label, text) VALUES (@label, @text)"; command.Parameters.AddWithValue("@label", Conversion.StringToSql(text.label)); command.Parameters.AddWithValue("@text", Conversion.StringToSql(text.text)); //Execute command command.ExecuteNonQuery(); lastInsertedId = command.LastInsertedId; //close connection databaseconnection.CloseConnection(); } } catch (Exception ex) { logger.Error(string.Format("Insert, Error inserting text data: {0}", ex.Message)); } return(lastInsertedId.Value); }