I am new in coding and i was recently chosen to start training in C# and Asp.Net. I am trying to update an entry into the database but to no avail. The delete, create and read functions work properly. (i know that my code lacks try and catch)
I 've tried different solutions that i found while googling but since i am still training i never fully understand if what i am following is completely correct. Currently i am able to diplay the fields with their info when navigating to the "Article" i am trying to edit but when i click the Save button nothing happens.
This is in my Article Context file:
public Article EditSingleArticle(int id, int userID, string Title, string Body) { Article article = new Article(); using (MySqlConnection conn = GetConnection()) { MySqlCommand cmdslct = new MySqlCommand("SELECT * FROM Article WHERE ID=@id", conn); MySqlCommand cmdul = new MySqlCommand("UPDATE Article SET userID=@userID,Title=@Title,Body=@Body WHERE ID=@id", conn); conn.Open(); cmdslct.Parameters.AddWithValue("@ID", id); cmdslct.ExecuteNonQuery(); cmdul.Parameters.AddWithValue("@userID", article.userID); cmdul.Parameters.AddWithValue("@Title", article.Title); cmdul.Parameters.AddWithValue("@Body", article.Body); cmdul.ExecuteNonQuery(); using (MySqlDataReader reader = cmdslct.ExecuteReader()) { while (reader.Read()) { article = new Article() { userID = reader.GetInt32("userID"), ID = reader.GetInt32("ID"), Title = reader.GetString("Title"), Body = reader.GetString("Body") }; } } conn.Close(); } The code below is in my ArticleController file:
public IActionResult Edit(int id, int userID, string Title, string Body) { ArticleContext context = HttpContext.RequestServices.GetService(typeof(ArticleContext)) as ArticleContext; if (ModelState.IsValid) { return View(context.EditSingleArticle(id, userID, Title, Body)); } return View(); } I don't get any error messages but the article i am trying to edit never updates
WHERE ID=@id).