2017-12-09 17 views
0

現在、最終的な1年間のプロジェクト.net MVCで作業しています。 私はLocation_new(LocationControllerでアクション)を開き、提出をクリックすると、それは提供された値の列名または数がテーブル定義と一致しません - .NET MVC

"

Column name or number of supplied values does not match table definition.

"

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Column name or number of supplied values does not match table definition.

Source Error:

Line 22: Line 23: conn.Open(); Line 24: int count = command.ExecuteNonQuery(); Line 25: conn.Close(); Line 26:

Source File: C:\Users\Dev D\Documents\Visual Studio 2015\Projects\MySafar\MySafar\Models\DBUtility.cs Line: 24

Location.cs(クエリのModelクラス)

エラーを示してい
public class Location 
    { 
     public int LocationID; 
     public string LocationName; 
     public string PhotoPath; 
     public string Details; 
     public string CityID; 

     public int Insert() 
     { 
      string query = "Insert Location VALUES(@LocationName, @PhotoPath, @Details, @CityID)"; 
      List<SqlParameter> parameters = new List<SqlParameter>(); 
      parameters.Add(new SqlParameter("@LocationName", this.LocationName)); 
      parameters.Add(new SqlParameter("@PhotoPath", this.PhotoPath)); 
      parameters.Add(new SqlParameter("@Details", this.Details)); 
      parameters.Add(new SqlParameter("@CityID", this.CityID)); 

      return DBUtility.ModifyData(query, parameters); 
     } 

DBUtility.cs(データベースのモデルクラス)

public class DBUtility 
    { 
     public static int ModifyData(String query, List<SqlParameter> parameter) 
     { 
      SqlConnection conn = new SqlConnection(); 
      conn.ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\Dev D\\Documents\\Visual Studio 2015\\Projects\\MySafar\\MySafar\\App_Data\\MySafarDB.mdf\";Integrated Security=True"; 

      SqlCommand command = new SqlCommand(); 
      command.Connection = conn; 

      command.CommandText = query; 
      command.Parameters.AddRange(parameter.ToArray()); 

      conn.Open(); 
      int count = command.ExecuteNonQuery(); 
      conn.Close(); 

      return count; 
     } 

AdminController

public ActionResult Location_New() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult Location_New_Submit() 
     { 
      Location l = new Location(); 
      l.LocationName = Request.Form["LocationName"]; 
      l.PhotoPath = Request.Form["PhotoPath"]; 
      l.Details = Request.Form["PhotoPath"]; 
      //l.CityID = Convert.ToInt32(Request.Form["CityID"]); 
      l.CityID = Request.Form["CityID"]; 

      l.Insert(); 

      return RedirectToAction("Location_List"); 
     } 

[HttpPost] 
     public ActionResult Location_List() 
     { 
      return View(); 
     } 

答えて

0

あなたの文で、あなたの列を指定して、あなたが値を指定しないとアイデンティティーフィールドを持っている場合、このエラーが発生します。

代わりの

string query = "Insert Location VALUES(@LocationName, @PhotoPath, @Details, @CityID)"; 

使用

string query = "Insert INTO Location(LocationName, PhotoPath, Details, CityID) VALUES(@LocationName, @PhotoPath, @Details, @CityID)"; 

ちょうどあなたの実際の列名にそれらを変更することを確認し、私は私の例のパラメータ名のオフに名前を仮定しました。

+0

ソリューションありがとうございました しかし変更はありません 同じエラー...... –

関連する問題