© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
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;
            }
        }
image.png
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

database oledb sql UPDATE not working
C#CC# / help
3y ago
✅ SQL code
C#CC# / help
17mo ago
SQL code
C#CC# / help
17mo ago
✅ SQL code
C#CC# / help
17mo ago