I am trying to build a simple ASP.NET Core website. I am trying to edit a model but it's not working. I edit the model but it does not save to the database. If you could help me, it will be really appreciated.
What I have tried:
public int Edit(CarDealershipModel carModel)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
string sqlQuery = "UPDATE CarDealership SET Make = @Make, Model = @Model, Year = @Year, Vin = @Vin, Color = @Color, Location = @Location WHERE Id = @Id";
SqlCommand command = new SqlCommand(sqlQuery, connection);
command.Parameters.Add("@Id", System.Data.SqlDbType.VarChar, 1000).Value = carModel.Id;
command.Parameters.Add("@Make", System.Data.SqlDbType.VarChar, 1000).Value = carModel.Make;
command.Parameters.Add("@Model", System.Data.SqlDbType.VarChar, 1000).Value = carModel.Model;
command.Parameters.Add("@Year", System.Data.SqlDbType.VarChar, 1000).Value = carModel.Year;
command.Parameters.Add("@Vin", System.Data.SqlDbType.VarChar, 1000).Value = carModel.Vin;
command.Parameters.Add("@Color", System.Data.SqlDbType.VarChar, 1000).Value = carModel.Color;
command.Parameters.Add("@Location", System.Data.SqlDbType.VarChar, 1000).Value = carModel.Location;
connection.Open();
int newId = command.ExecuteNonQuery();
connection.Close();
return newId;
}
}