Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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;
    }
}
Posted
Updated 10-Jul-21 18:50pm
v2
Comments
Richard Deeming 12-Jul-21 6:26am    
NB: Your variable name is inaccurate - it seems to imply you are returning the ID of the record, whereas you're actually returning the number of rows affected by the command.

DbCommand.ExecuteNonQuery Method (System.Data.Common) | Microsoft Docs[^]

1 solution

We can't tell you how to fix that - we don't have access to your code while it is running, or to your database - and both are critical here.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Start by making sure that code is executed: if it is, then check the value of newId that is returned.
If it's zero, then that means that no rows matched your criteria - so look at the value of carModel.Id and at your database using SSMS to be sure it is in there.
If it's more than zero, that means that the DB was changed - so check that you are using the right DB.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But ... I'd say that your DB design needs some work: why is each columns 1000 characters long? It's unlikely that a colour for example will get that long even in the fevered dreams of a colour-naming-marketeer ...
Probably, you should be using NVARCHAR(MAX) for the column instead.
The variable name NewId worries me a little as well - the returned value from ExecuteNonQuery is the number of rows affected, not a ID value at all - so either it is badly named or it's going to cause problems elsewhere if you assume it's the "row that was changed" identifier.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900