[英]Web Form and MVC Application is throwing Internal Server Error 500 while storing data in SQL Server
I am probably repeating same issue discussed on forum but I think this issue should be discussed as it persist with many ways I tried. 我可能会重复在论坛上讨论的同一问题,但我认为应该讨论此问题,因为它以我尝试过的许多方式持续存在。
I have referred number of links including 我已提及了一些链接,包括
Here first one works for me but second is not working so tried it with WebForm and MVC with and without [System.Web.Script.Services.ScriptService]. 在这里,第一个对我有用,但是第二个对我不起作用,因此无论是否使用[System.Web.Script.Services.ScriptService],都可以使用WebForm和MVC对其进行尝试。 Virtually, It became hit and trail case for me.
实际上,这对我来说成了偶然的案例。 Also, my Fiddler Tool says JSON is correctly supplied in every case when I tried single value or multiple.
另外,我的Fiddler工具说,当我尝试单个值或多个值时,在每种情况下都可以正确提供JSON。 Last but not least there are lots of resources over the web that how achieve AJAX jQuery but merely some specific thing I found how to debug with Inter Server Error.
最后但并非最不重要的一点是,网络上有很多资源可以实现AJAX jQuery,但是我发现了一些特定的东西,可以通过Inter Server Error进行调试。 Any note will be appreciable.
任何笔记将是可观的。
In my ASP.NET MVC application 在我的ASP.NET MVC应用程序中
Services is Like : 服务就像:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SaveDb : System.Web.Services.WebService
{
[WebMethod]
public string InsertIntoDB(string p)
{
try
{
SqlConnection sqlConnection = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=School;Integrated Security=True");
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand("insert into UserNamePassword values('uNm1','" + p + "')");
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
catch (Exception)
{
return "Error";
}
return "success";
}
}
HTML View is Like : HTML视图就像:
@{
Layout = null;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#btn').on('click', function (e) {
e.preventDefault();
var p = $('#p1').val();
var jsonData = JSON.stringify({ p: p });
$.ajax({
type: "POST",
url: 'MVCP/SaveDb.asmx/InsertIntoDB',
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnErrorCall
})
function OnSuccess(response) {
var result = response.d;
if (result == "success") {
alert('Job Done');
}
}
function OnErrorCall(response) {
alert('failed agin');
}
})
})
</script>
</head>
<body>
User Name
<br />
Password <input id="p1" type="text" />
<br />
<button id="btn">Click</button>
</body>
</html>
Note : Putting a break point over the Service Page is not received control and Explicitly loading the Service on Browser and passing the p Parameter worked. 注意:不能在服务页面上放置一个断点,并且无法在浏览器上显式加载该服务并传递有效的p参数。 So, I guess guess Err is coming from Web Service Part thus adding Web Service Tag/ WCF.
因此,我猜想Err来自Web服务部分,从而增加了Web服务标签/ WCF。
And..! 和..! This Code is working absolutely fine.
该守则绝对有效。
.ASPX Page .ASPX页面
<script type="text/javascript">
$(function () {
$("[id*=btnSave]").bind("click", function () {
var user = {};
user.Username = $("[id*=txtUsername]").val();
user.Password = $("[id*=txtPassword]").val();
$.ajax({
type: "POST",
url: "CS.aspx/SaveUser",
data: '{user: ' + JSON.stringify(user) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("User has been added successfully.");
window.location.reload();
}
});
return false;
});
});
</script>
.ASPX.cs Page .ASPX.cs页面
[WebMethod]
[ScriptMethod]
public static void SaveUser(User user)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Users VALUES(@Username, @Password)"))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Username", user.Username);
cmd.Parameters.AddWithValue("@Password", user.Password);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
public class User
{
public string Username { get; set; }
public string Password { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.