我正在尝试使用控制器向数据库添加贡献者数据,我收到以下错误:
另一个SqlParameterCollection已经包含了SqlParameter。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
Exception Details: System.ArgumentException: The SqlParameter is already contained by another SqlParameterCollection.
Source Error:
Line 195: //@returnInventoryId AS int OUTPUT
Line 196:
Line 197: var recordsAffected = dbFFS.Database.ExecuteSqlCommand(
Line 198: "sp_InsertOrUpdateContributor @electionId, @candidateId, @contributorId, @eligibleForRebate, @lastName, @firstName, @middleName, @fullName, @contributorTypeAbbr, @streetNo, @streetName, @unit, @city, @province, @country, @postalCode, @mailingAddress1, @mailingAddress2, @mailingAddress3, @mailingAddress4, @homePhone, @workPhone, @cellPhone, @emailAddress, @notes, @returnInventoryId OUT ",
Line 199: param1, param2, param3, param4, param2, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15, param16, param18, param19, param20, param21, param22, param23, param24, param25, param15, param15, pOutput);
以下是我的代码[Controller]:
public ActionResult Edit(ffsContributor model)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
//0 Username | 1 Fullname | 2 User Id | 3 Login Type | 4 Election Id
string[] UserData = ticket.UserData.Split('|');
if (UserData[3] != "candidate")
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
int candidateid = Convert.ToInt32(UserData[2]);
int electionID = Convert.ToInt32(UserData[4]);
model = dbFFSSave(electionID, candidateid, model);
//check inventory request is the same with the model
return View(model);
}
public ActionResult Add()
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
//0 Username | 1 Fullname | 2 User Id | 3 Login Type | 4 Election Id
string[] UserData = ticket.UserData.Split('|');
if (UserData[3] != "candidate")
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
int candidateid = Convert.ToInt32(UserData[2]);
int electionID = Convert.ToInt32(UserData[4]);
ffsContributor model = new ffsContributor
{
CandidateId = candidateid,
ContributorId = 0
};
model.DropDownList = DbFFSGetOffices(electionID, candidateid);
return View(model);
}
private Models.ffsContributor dbFFSSave(int electionId, int candidateID, Models.ffsContributor model)
{
SqlParameter param1 = new SqlParameter("@electionId", electionId);
SqlParameter param2 = new SqlParameter("@candidateId", model.CandidateId);
//SqlParameter param3 = new SqlParameter("@contributorId", model.ContributorId);
SqlParameter param4 = new SqlParameter("@eligibleForRebate", model.EligibleForRebate);
SqlParameter param5 = new SqlParameter("@lastName", model.LastName);
SqlParameter param6 = new SqlParameter("@firstName", model.FirstName);
SqlParameter param7 = new SqlParameter("@middleName", model.MiddleName);
SqlParameter param8 = new SqlParameter("@fullName", model.FullName);
SqlParameter param9 = new SqlParameter("@contributorTypeAbbr", model.ContributorTypeAbbr);
SqlParameter param10 = new SqlParameter("@streetNo", model.StreetNo);
SqlParameter param11 = new SqlParameter("@streetName", model.StreetName);
SqlParameter param12 = new SqlParameter("@unit", model.Unit);
SqlParameter param13 = new SqlParameter("@streetName", model.StreetName);
SqlParameter param14 = new SqlParameter("@city", model.City);
SqlParameter param15 = new SqlParameter("@province", model.Province);
SqlParameter param16 = new SqlParameter("@postalCode", model.PostalCode);
SqlParameter param18 = new SqlParameter("@mailingAddress1", model.MailingAddress1);
SqlParameter param19 = new SqlParameter("@mailingAddress2", model.MailingAddress2);
SqlParameter param20 = new SqlParameter("@mailingAddress3", model.MailingAddress3);
SqlParameter param21 = new SqlParameter("@mailingAddress4", model.MailingAddress4);
SqlParameter param22 = new SqlParameter("@homePhone", model.HomePhone);
SqlParameter param23 = new SqlParameter("@workPhone", model.WorkPhone);
SqlParameter param24 = new SqlParameter("@emailAddress", model.EmailAddress);
SqlParameter param25 = new SqlParameter("@notes", model.Notes);
SqlParameter param3 = new SqlParameter("@contributorId", DBNull.Value);
//if (model.ContributorId < 0)
//{
// param5.Value = model.ContributorId;
//}
var pOutput = new SqlParameter("@returnContributorId", System.Data.SqlDbType.Int)
{
Direction = System.Data.ParameterDirection.Output
};
//@returnInventoryId AS int OUTPUT
var recordsAffected = dbFFS.Database.ExecuteSqlCommand(
"sp_InsertOrUpdateContributor @electionId, @candidateId, @contributorId, @eligibleForRebate, @lastName, @firstName, @middleName, @fullName, @contributorTypeAbbr, @streetNo, @streetName, @unit, @city, @province, @country, @postalCode, @mailingAddress1, @mailingAddress2, @mailingAddress3, @mailingAddress4, @homePhone, @workPhone, @cellPhone, @emailAddress, @notes, @returnInventoryId OUT ",
param1, param2, param3, param4, param2, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15, param16, param18, param19, param20, param21, param22, param23, param24, param25, param15, param15, pOutput);
model.DropDownList = DbFFSGetOffices(electionId, candidateID);
return model;
}
我的cshtml文件:
@using (Html.BeginForm("Edit", "ffsContributors"))
{
@Html.HiddenFor(m => m.ContributorId)
@Html.TextBoxFor(m => m.FullName, new { @placeholder = "FullName" })
@Html.TextBoxFor(m => m.EmailAddress, new { @placeholder = "EmailAddress" })
@Html.TextBoxFor(m => m.WorkPhone, new { @placeholder = "WorkPhone" })
@Html.TextBoxFor(m => m.HomePhone, new { @placeholder = "HomePhone" })
// @Html.DropDownListFor(m => m.CellPhone, Model.DropDownList)
<input type="submit" value="Save" />
}
如何解决此错误?
答案 0 :(得分:1)
您正在传递参数&#34; @ streetName&#34;两次到您的ExecuteSqlCommand()。 删除其中一个:
SqlParameter param11 = new SqlParameter("@streetName", model.StreetName);
SqlParameter param13 = new SqlParameter("@streetName", model.StreetName);
希望它有所帮助。
答案 1 :(得分:1)
SqlParameter param11 = new SqlParameter("@streetName", model.StreetName);
SqlParameter param13 = new SqlParameter("@streetName", model.StreetName);
此外,使用command.Parameters.AddWithValue()
可能有助于减少错误。