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.System.Data.SqlClient.SqlException:スカラー変数 "@LocationID"を宣言しなければなりません。 MVCにおける
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@LocationID".
Source Error:
Line 43: Line 44: conn.Open(); Line 45:
adapter.Fill(dt); Line 46: conn.Close(); Line 47:Source File: C:\Users\Dev D\Documents\Visual Studio 2015\Projects\MySafar\MySafar\Models\DBUtility.cs Line: 45
Location.cs
public ActionResult Location_Edit()
{
Location l = new Location();
l.LocationID = Convert.ToInt32(Request.QueryString["ID"]);
l.SelectByPK();
return View(l)
}
[HttpPost]
public ActionResult Location_List_Edit()
{
Location l = new Location();
l.LocationID = Convert.ToInt32(Request.QueryString["ID"]);
l.LocationName = Request.Form["LocationName"];
l.PhotoPath = Request.Form["PhotoPath"];
l.Details = Request.Form["
l.CityID = Request.Form["CityID"];
l.Update();
return View("Location_List", new DataTable());
}
DBUtility.cs(Modelクラス変更インサートのデータベースメソッドの定義とデータを選択AdminControllerにおけるビューの(Modelクラス)
public class Location
{
public int LocationID;
public string LocationName;
public string PhotoPath;
public string Details;
public string CityID;
}
public int Update()
{
string query = "UPDATE Location SET LocationName = @LocationName, PhotoPath = @PhotoPath, Details = @Details, CityID = @CityID WHERE LocationID = @LocationID";
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@LocationID", this.LocationID));
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);
}
public bool SelectByPK()
{
string query = "SELECT * FROM Location WHERE LocationID = @LocationID";
List<SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@LocationID", this.LocationID));
DataTable dt = DBUtility.SelectData(query, parameters);
if (dt.Rows.Count > 0)
{
this.LocationName = dt.Rows[0]["LocationName"].ToString();
this.PhotoPath = dt.Rows[0]["PhotoPath"].ToString();
this.Details = dt.Rows[0]["Details"].ToString();
//this.CityID = Convert.ToInt32(dt.Rows[0]["CityID"]);
this.CityID = dt.Rows[0]["CityID"].ToString();
return true;
}
else
{
return false;
}
}
}
方法)
public static DataTable SelectData(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;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
conn.Open();
adapter.Fill(dt);
conn.Close();
return dt;
}
SqlCommandコマンド=新しいSqlCommand(); command.Connection = conn; foreach(Listパラメータのvar param)) command.Parameters.Add(param); command.CommandText = query; DataTable dt = new DataTable(); SqlDataAdapterアダプタ=新しいSqlDataAdapter(); adapter.SelectCommand = command; conn.Open(); adapter.Fill(dt); conn.Close(); return dt; } –