Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Incorrect syntax near 'Make'

namespace HondaDealership.Pages.Admin

{
[Authorize]
public class EditModel : PageModel
{

    public Car car = new Car();
    public string errorMessage = "";
    public string successMessage = "";

    public void OnGet()
    {
        string Id = Request.Query["Id"];

        try
        {
            string connectionString = "Server=(localdb)\\MSSQLLocalDB; database=HondaDealership;";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                String sql = "SELECT * FROM listCars WHERE Id=@Id";
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("@Id", Id);
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            car.Id = reader.GetInt32(0);
                            car.Make = reader.GetString(1);
                            car.CarModel = reader.GetString(2);
                            car.Year = reader.GetInt32(3);
                            car.Colour = reader.GetString(4);
                            car.Registration =  reader.GetString(5);
                            car.Price = reader.GetDecimal(6);
                        }
                    }
                }
            }
        }
        catch(Exception ex)
        {
            errorMessage = ex.Message;
        }
    }

    public void OnPost()
    {
        car.Make = Request.Form["Make"];
        car.CarModel = Request.Form["CarModel"];
        car.Year = Convert.ToInt32(Request.Form["Year"]);
        car.Colour = Request.Form["Colour"];
        car.Registration = Request.Form["Registration"];
        car.Price = Convert.ToDecimal(Request.Form["Price"]);
        car.Id = Convert.ToInt32(Request.Form["Id"]);

        if (car.Make.Length == 0 || car.CarModel.Length == 0 ||
            car.Year <= 0 || car.Colour.Length == 0 ||
            car.Registration.Length == 0 || car.Price <= 0)
        {
            errorMessage = "All the fields are required";
            return;
        }


        try
        {
            string connectionString = "Server=(localdb)\\MSSQLLocalDB; database=HondaDealership;";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sql = "UPDATE listCars" +
                             "SET Make=@Make, CarModel=@CarModel, Year=@Year, Colour=@Colour, Registration=@Registration, Price=@Price" + 
                             "WHERE Id=@Id";

                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("@Id", car.Id);
                    command.Parameters.AddWithValue("@Make", car.Make);
                    command.Parameters.AddWithValue("@CarModel", car.CarModel);
                    command.Parameters.AddWithValue("@Year", car.Year);
                    command.Parameters.AddWithValue("@Colour", car.Colour);
                    command.Parameters.AddWithValue("@Registration", car.Registration);
                    command.Parameters.AddWithValue("@Price", car.Price);


                    command.ExecuteNonQuery();
                }
            }
        }
        catch(Exception ex)
        {
            errorMessage = ex.Message;
            return;
        }

        Response.Redirect("/Admin/InStockCarsAdmin");
    }
}

}

The edit page populates, but when I hit ‘Submit’ It brings up the Error "incorrect syntax near ‘Make’. When I put a breakpoint on the command.Parameters.AddWithValue("Make", car.Make); it has the correct value of "Honda".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The start of the HTML form is:

<form method="POST">
<input type="hidden" name="Id" value="@Model.car.Id" />
<div class="row mb-3">
    <label class="row mb-3"></label>
        <label class="col-sm-3 col-form-label">Make</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" name="Make" value="@Model.car.Make" />
        </div>
</div>

This is the table in the sql database:

CREATE TABLE [dbo].[listCars] (
[Id]           INT             IDENTITY (1, 1) NOT NULL,
[Make]         NVARCHAR (MAX)  NULL,
[CarModel]     NVARCHAR (MAX)  NULL,
[Year]         INT             NOT NULL,
[Colour]       NVARCHAR (MAX)  NULL,
[Registration] NVARCHAR (MAX)  NULL,
[Price]        DECIMAL (18, 2) NOT NULL,
CONSTRAINT [PK_listCars] PRIMARY KEY CLUSTERED ([Id] ASC)

);

I have looked over this and tried to find solutions for hours with no luck.

>Solution :

string sql = "UPDATE listCars" +
             " SET Make=@Make, CarModel=@CarModel, Year=@Year, Colour=@Colour, Registration=@Registration, Price=@Price" + 
             " WHERE Id=@Id";

You missed a space between the table name and SET, the same goes for WHERE.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading