0

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

1
  • 1
    You are not adding your id parameter to your update command, so there will be no matching entries in your database to update (WHERE ID=@id). Commented Jul 18, 2019 at 9:30

2 Answers 2

1

The issue is that article object only have the default empty fields because you are calling cmdslct.ExecuteNonQuery() that does not return data at all, only the number of rows affected by an insert, update, or delete.

So you should call ExecuteReader() that returns an object that can iterate over the entire result set while only keeping one record in memory at a time.

Your code should looks like the following code in order to work as expected:

 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); 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") }; } } cmdul.Parameters.AddWithValue("@userID", article.userID); cmdul.Parameters.AddWithValue("@Title", article.Title); cmdul.Parameters.AddWithValue("@Body", article.Body); cmdul.ExecuteNonQuery(); conn.Close(); } 

Regards,

Sign up to request clarification or add additional context in comments.

1 Comment

i have copied your code but unless i copied something wrong i don't get the result i want
0

In your update query you are using a where condition which uses parameter @id. But you are not passing the ID argument to your query. Add a argument '@id' for the update query. Add this line to the code

cmul.Parameters.AddWithValue("@id", id); 

6 Comments

UPDATE Article SET userID=@userID,Title=@Title,Body=@Body WHERE ID=@id are you refering to this query? if yes what should i change
Yes in the update query you are using this condition 'WHERE ID=@id'. But @id is not passed as argument to query. To the parameter list add cmul.Parameters.AddWithValue("@id", id);
i have tried that but i still dont get the result i want
Then check if the correct id is getting passed in the argument
when i use the select query, when i navigate to the edit page all the fields are already populated with the details that were passed when the article was created. on the update article query though i cannot get it to save the changes
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.