0

The model in the parameter has values all the way to the DAL method where the query is but somehow nothing changes in the database. Anyone got any clue why? Also no errors occurred. Controller:

public ActionResult AddCar(CarModel car)
{
        if(ModelState.IsValid)
        {
            u.AddCar(new DTO.CarDTO(car.Color, car.Type, car.NumberOfSeats, car.Price, car.MaxKilometres, car.Addition, car.ModelID, car.License_Plate, car.Fueltype, car.ReservationStatus));
            return RedirectToAction("Index", "Home");
        }
        return View();
}

Logic:

public void AddCar(CarDTO c)
{
    carDAL.AddCar(new CarDTO(c.Color, c.Type, c.NumberOfSeats, c.Price, c.MaxKilometres, c.Addition, c.ModelID, c.License_Plate, c.Fueltype, c.ReservationStatus));
}

Interface layer:

public interface ICarDAL<CarDTO>
{
    public void AddCar(CarDTO c) { }
}

DAL, class does have connectionstring:

public void AddCar(CarDTO c)
{
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        string query = "INSERT INTO [dbo].[Car](Price, Addition, License_Plate, MaxKm, Type, Color, NumberOfSeats, Fueltype, reservationStatus, ModelID) " +
            "VALUES(@Price, @Addition, '@LicensePlate', @MaxKm, '@Type', '@Color', @NumberOfSeats, @FuelType, @Reserved, @ModelID)";

        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@Price", c.Price);
            cmd.Parameters.AddWithValue("@Addition", c.Addition);
            cmd.Parameters.AddWithValue("@LicensePlate", c.License_Plate);
            cmd.Parameters.AddWithValue("@MaxKm", c.MaxKilometres);
            cmd.Parameters.AddWithValue("@Type", c.Type);
            cmd.Parameters.AddWithValue("@Color", c.Color);
            cmd.Parameters.AddWithValue("@NumberOfSeats", c.NumberOfSeats);
            cmd.Parameters.AddWithValue("@FuelType", c.Fueltype);
            cmd.Parameters.AddWithValue("@ReservationStatus", c.ReservationStatus);
            cmd.Parameters.AddWithValue("@ModelID", c.ModelID);
        }
    }
}

Found the error but I still dont know how to fix it: https://i.sstatic.net/Stdh5.jpg

Solution: First of all I needed to use ExecuteNonQuery() to make the query actually do something...I thought SqlCommand already executed it for you. Secondly my password consisted out of ************ which I thought it wouldn't matter but I guess it did.

12
  • 1
    You're not executing the command. So cmd.ExecuteNonQuery(); after assigning your params would work. Also on another note use Add and specify the data type not AddWithValue as the values would be inferred and may not be what you need. Commented Nov 10, 2019 at 17:31
  • @Çöđěxěŕ So I put that code in the cmd using, so right under the last cmd.Parameters.AddWithValue();. But now I get 'ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.' Commented Nov 10, 2019 at 18:00
  • 2
    P.s. ADO.NET is one of the ways you can connect to a database from .NET programs. So you can use it as part of an asp.net application, or part of a desktop app or console app, or whatever. Commented Nov 10, 2019 at 21:14
  • 1
    No. That's completely irrelevant. Did you try googling your error message? There are several previous questions on this site about that error, as well as on other sites. For example this one or this one and several others all have a lot of useful suggestions which might be relevant to your situation. Please do some searching and try out the possible solutions. Commented Nov 11, 2019 at 9:38
  • 1
    @ADyson I sincerely thank you for helping me. I didn't know the query wouldn't execute if I didn't use ExecuteNonQuery(). Also the problem was that my password consisted of only ********* instead of the actual password, but thanks for helping :D Commented Nov 11, 2019 at 10:06

2 Answers 2

1
public void AddCar(CarDTO c)
{
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        if (con.State==ConnectionState.Closed)
            {                      
                con.Open();   
             }
         [Your code]
         con.Close();
    }
 }
Sign up to request clarification or add additional context in comments.

3 Comments

It says "SqlException: Login failed for user 'mydatabase'"
Check your connectionstring is true.
and check your user can access your database
-2

You may need to use a "save" function, I'm not seeing anywhere in your code where the SQL command is actually executed, I see it being built, but I've used it in the past with something similar like cmd.Execute()

4 Comments

I didn't downvote anything so i dont know. Sorry how can you see if you use entity framework or not?
So what you're saying is using (SqlCommand cmd = new SqlCommand(query, con)) isn't going to execute the query? I thought it would...
Don't know about anyone else but I downvoted this because it's self-evident from the code shown that it isn't using entity framework
Thanks for letting me know, I edited my response. I think my answer is applicable, the actual SQL command is not being executed without the Execute function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.