【发布时间】:2020-12-11 20:55:35
【问题描述】:
我正在尝试使用 ASP.NET MVC 4、jQuery 和 Ajax 更新 SQL Server 中表的记录。但是,每当我单击更新时,都会出现以下错误:
System.Data.SqlClient.SqlException 未被用户代码处理
HResult=-2146232060
Message=参数化查询“(@ID int,@AlertStatus nvarchar(4000),@Comment nvarchar(4000))UPD”需要参数“@AlertStatus”,但未提供该参数。
Source=.Net SqlClient 数据提供者
在进一步的研究中,我发现使用?? DBNull.Value 可以避免这种情况,但是我不想将该列更新为NULL。我怀疑错误的原因是我在更新函数中的 Ajax 调用中没有将数据发送到控制器(数据:'{task:' + JSON.stringify(task) + '}',)。
请查看下面的代码,感谢您的帮助。
jQuery Ajax
$("body").on("click", "#tblTask .Edit", function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find("input").length > 0) {
$(this).find("input").show();
$(this).find("span").hide();
}
});
row.find(".Update").show();
row.find(".Cancel").show();
row.find(".Delete").hide();
$(this).hide();
});
$("body").on("click", "#tblTask .Update", function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find("input").length > 0) {
var span = $(this).find("span");
var input = $(this).find("input");
span.html(input.val());
span.show();
input.hide();
}
});
row.find(".Edit").show();
row.find(".Delete").hide();
row.find(".Cancel").hide();
$(this).hide();
var task = {};
task.taskID = row.find(".taskID").find("span").html();
task.GroupSubsidiary = row.find(".GroupSub").find("span").html();
task.FunctionName = row.find(".Fname").find("span").html();
task.FunctionDesc = row.find(".Fdesc").find("span").html();
task.CheckPeriod = row.find(".Checkp").find("span").html();
task.Profiledate = row.find(".Pdate").find("span").html();
task.PeriodDay = row.find(".Pday").find("span").html();
task.AlertStatus = row.find(".Status").find("span").html();
task.Comment = row.find(".Comment").find("span").html();
$.ajax({
type: 'POST',
url: "@Url.Action("UpdateTask","Task")",
data: '{task:' + JSON.stringify(task) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Updated Sucessfully");
},
error: function () {
alert("An Error Occured");
}
})
});
控制器:
[HttpPost]
public ActionResult UpdateTask(Task tasks)
{
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
string query = "UPDATE AS_AlertsDefinition SET GroupSubsidiary= @GroupSubsidiary, FunctionName= @FunctionName, FunctionDesc= @FunctionDesc, CheckPeriod= @CheckPeriod, Profiledate= @Profiledate, PeriodDay= @PeriodDay, AlertStatus= @AlertStatus, Comment= @Comment WHERE ID= @ID";
using (SqlConnection sqlcon = new SqlConnection(constr))
{
using (SqlCommand sqlcmd = new SqlCommand(query))
{
sqlcmd.Parameters.AddWithValue("@ID", tasks.ID);
sqlcmd.Parameters.AddWithValue("@GroupSubsidiary", tasks.GroupSubsidiary);
sqlcmd.Parameters.AddWithValue("@FunctionName", tasks.FunctionName);
sqlcmd.Parameters.AddWithValue("@FunctionDesc", tasks.FunctionDesc);
sqlcmd.Parameters.AddWithValue("@CheckPeriod", tasks.CheckPeriod);
sqlcmd.Parameters.AddWithValue("@Profiledate", tasks.Profiledate);
sqlcmd.Parameters.AddWithValue("@PeriodDay", tasks.PeriodDay);
sqlcmd.Parameters.AddWithValue("@AlertStatus", tasks.AlertStatus);
sqlcmd.Parameters.AddWithValue("@Comment", tasks.Comment);
sqlcmd.Connection = sqlcon;
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
}
}
return new EmptyResult();
}
}
类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AlertNotificationWeb.Models
{
public class Task
{
public int ID { get; set; }
public string GroupSubsidiary { get; set; }
public string FunctionName { get; set; }
public string FunctionDesc { get; set; }
public string CheckPeriod { get; set; }
public string Profiledate { get; set; }
public int PeriodDay { get; set; }
public string AlertStatus { get; set; }
public string Comment { get; set; }
}
}
【问题讨论】:
-
UPDATE不太可能有问题,但您确实应该使用Parameters.Add而不是Parameters.AddWithValue。 AddWithValue is Evil -
可以分享一下Task类吗?
-
通过阅读错误,我认为这可能是变量“@AlertStatus”的问题。检查您的代码并确保您正确设置它并且它不是NULL,因为这可能是这种情况。查找变量值的一个非常好的方法是在错误之前使用 Console.Log(variable) ;)
标签: c# sql-server asp.net-mvc-4 error-handling asp.net-ajax