C
C#2y ago
Adel

❔ Update sql code not working

public Car UpdateCar(Car car)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand command = con.CreateCommand())
{
command.CommandText = "UPDATE Car SET Title = @Title, Price = @Price, Miles = @Miles, Status = @Status WHERE Id = @Id";
command.Parameters.AddWithValue("@Id", car.Id);
command.Parameters.AddWithValue("@Title", car.Title ?? "");
command.Parameters.AddWithValue("@Price", car.Price ?? 0);
command.Parameters.AddWithValue("@Miles", car.Miles ?? 0);
command.Parameters.AddWithValue("@Status", car.Status ?? "");

command.ExecuteNonQuery();
}

con.Close();
return car;
}
}
catch (Exception ex)
{
// Handle the exception if needed
throw;
}
}
public Car UpdateCar(Car car)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand command = con.CreateCommand())
{
command.CommandText = "UPDATE Car SET Title = @Title, Price = @Price, Miles = @Miles, Status = @Status WHERE Id = @Id";
command.Parameters.AddWithValue("@Id", car.Id);
command.Parameters.AddWithValue("@Title", car.Title ?? "");
command.Parameters.AddWithValue("@Price", car.Price ?? 0);
command.Parameters.AddWithValue("@Miles", car.Miles ?? 0);
command.Parameters.AddWithValue("@Status", car.Status ?? "");

command.ExecuteNonQuery();
}

con.Close();
return car;
}
}
catch (Exception ex)
{
// Handle the exception if needed
throw;
}
}
5 Replies
Adel
AdelOP2y ago
and here is my controller, my insert works fine my delete works great but updating doesn't work i am not sure if the issue is because of image but maybe someone can see something I can't
// POST: Home/UpdateCar
[HttpPost]
public IActionResult UpdateCar(Car car)
{
if (!ModelState.IsValid)
{
return View("Edit", car);
}

try
{
Car updatedCar = _surveydb.UpdateCar(car); // Replace with your method to update the car in the repository
return RedirectToAction("Inventory"); // Redirect to the "Inventory" action method
}
catch (Exception ex)
{
// Handle the exception
ModelState.AddModelError("", "An error occurred while updating the car. Please try again.");
return View("Edit", car);
}
}
// POST: Home/UpdateCar
[HttpPost]
public IActionResult UpdateCar(Car car)
{
if (!ModelState.IsValid)
{
return View("Edit", car);
}

try
{
Car updatedCar = _surveydb.UpdateCar(car); // Replace with your method to update the car in the repository
return RedirectToAction("Inventory"); // Redirect to the "Inventory" action method
}
catch (Exception ex)
{
// Handle the exception
ModelState.AddModelError("", "An error occurred while updating the car. Please try again.");
return View("Edit", car);
}
}
when clicking on update it updates the page with the new numbers but now the database. Thank you 😄
TheRanger
TheRanger2y ago
did u debug this code? does it breakpoint in this method?
Adel
AdelOP2y ago
I'll try to debug in my SQL statement again but it didn't do anything last time I debugged my controller and I screenshotted what happened
TheRanger
TheRanger2y ago
well you have to show us that it hit a breakpoint in that method
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server