Home > OS >  The number of values in the VALUES clause must match the number of columns specified in the INSERT s
The number of values in the VALUES clause must match the number of columns specified in the INSERT s

Time:06-21

This is my models class (ObjektiMeQira.cs):

namespace crud2.Models
{
    public class ObjektiMeQira
    {

        public int ObjektiMeQiraId { get; set; }
        public string ObjektiMeQiraEmri { get; set; }
        public string LlojiObjektit { get; set; }
        public string Siperfaqe { get; set; }
        public int NumriIDHomave { get; set; }
        public string Kati { get; set; }
        public int Cmimi { get; set; }
        public string Vendndodhja { get; set; }
        public string Qyteti { get; set; }
        public string Shteti { get; set; }


    }
}

And now this is my Controllers class (ObjektiMeQiraController.cs): Note: I am only getting this error in Post method, otherwise Get method works fine.

 [HttpPost]
    public JsonResult Post(ObjektiMeQira omq)
    {
        string query = @"
                        insert into dbo.ObjektiMeQira(ObjektiMeQiraEmri,LlojiObjektit,Siperfaqe,
                        NumriIDHomave, Kati, Cmimi, Vendndodhja, Qyteti, Shteti)
                        values (@ObjektiMeQiraEmri), (@LLojiObjektit), (@Siperfaqe),
                        (@NumriIDHomave), (@Kati), (@Cmimi), (@Vendndodhja), (@Qyteti), (@Shteti)
                        ";

        DataTable table = new DataTable();
        string sqlDataSource = _configuration.GetConnectionString("EmployeeAppCon");
        SqlDataReader myReader;
        using (SqlConnection myCon = new SqlConnection(sqlDataSource))
        {
            myCon.Open();
            using (SqlCommand myCommand = new SqlCommand(query, myCon))
            {
                myCommand.Parameters.AddWithValue("@ObjektiMeQiraEmri", omq.ObjektiMeQiraEmri);
                myCommand.Parameters.AddWithValue("@LlojiObjektit", omq.LlojiObjektit);
                myCommand.Parameters.AddWithValue("@Siperfaqe", omq.Siperfaqe);
                myCommand.Parameters.AddWithValue("@NumriIDHomave", omq.NumriIDHomave);
                myCommand.Parameters.AddWithValue("@Kati", omq.Kati);
                myCommand.Parameters.AddWithValue("@Cmimi", omq.Cmimi);
                myCommand.Parameters.AddWithValue("@Vendndodhja", omq.Vendndodhja);
                myCommand.Parameters.AddWithValue("@Qyteti", omq.Qyteti);
                myCommand.Parameters.AddWithValue("@Shteti", omq.Shteti);
                myReader = myCommand.ExecuteReader();
                table.Load(myReader);
                myReader.Close();
                myCon.Close();
            }
        }

        return new JsonResult("Added Successfully");
    }

Even though I looked at other posts in stackoverflow I didn't quite find the solution, yet I'm a beginner and a little tip would be grateful

CodePudding user response:

Your insert query in C# is just plain wrong - it should be:

string query = @"INSERT INTO dbo.ObjektiMeQira (ObjektiMeQiraEmri, LlojiObjektit, Siperfaqe,
                                                NumriIDHomave, Kati, Cmimi, Vendndodhja, Qyteti, Shteti)
                 VALUES (@ObjektiMeQiraEmri, @LLojiObjektit, @Siperfaqe,
                         @NumriIDHomave, @Kati, @Cmimi, @Vendndodhja, @Qyteti, @Shteti);";

You MUST NOT put each parameter into its own round parenthesis!

You need to provide one row of data, with a total of 9 column values - in a single VALUES (.....) statement.

  • Related