Garima Bansal

Garima Bansal

  • 1.1k
  • 686
  • 40.3k

Must declare the scalar variable

Dec 14 2022 11:56 AM

i am trying to save record in database but getting error

Sql Table

SELECT TOP (1000) [Id]
      ,[Name_Department]
      ,[Father_OrgName]
      ,[City]
      ,[Street]
      ,[Locality]
      ,[Pincode]
      ,[PlotNo]
      ,[District]
      ,[Tehsil]
      ,[Village]
      ,[Ward]
      ,[KhasraNo]
      ,[KhewetNo]
      ,[KhataNo]
      ,[Landmesure]
      ,[CompoundWall]
      ,[LandDevelopment]
  FROM [PDA].[dbo].[BasicDetails]

 

HomeController

 string constr ="Data Source=DESKTOP-U788ISC\\SQLEXPRESS;Initial Catalog=PDA;Persist Security Info=True;User ID=sa;Password=sa123;MultipleActiveResultSets=True";
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "INSERT INTO BasicDetails(Name_Department,Father_OrgName,City,Street,Locality,District,Tehsil,Village,Ward,KhasraNo,KhewetNo,KhataNo,Landmesure,CompoundWall, LandDevelopment) VALUES(@Name_Department,@Father_OrgName,@City,@Street,@Locality,@District,@Tehsil,@Village,@Ward,@KhasraNo,@KhewetNo,@KhataNo,@Landmesure,@CompoundWall,@LandDevelopment)";
                query += " SELECT SCOPE_IDENTITY()";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    con.Open();
                    string Name_Department = Request.Form["Name_Department"];
                    string Father_OrgName = Request.Form["Father_OrgName"];
                    string City = Request.Form["City"];
                    string Street = Request.Form["Street"];
                    string Locality = Request.Form["Locality"];
                    string District = Request.Form["District"];
                    string Tehsil = Request.Form["Tehsil"];
                    string Village = Request.Form["Village"];
                    string Ward = Request.Form["Ward"];
                    string KhasraNo = Request.Form["KhasraNo"];
                    string KhewetNo = Request.Form["KhewetNo"];
                    string KhataNo = Request.Form["KhataNo"];
                    string Landmesure = Request.Form["Landmesure"];
                    string CompoundWall = Request.Form["CompoundWall"];
                    string LandDevelopment = Request.Form["LandDevelopment"];

                    cmd.ExecuteNonQuery();
                    con.Close();
                 
                    return View();
                }
           

Error : Must declare the scalar variable "Name_Department"


Answers (4)