我正在尝试将从 asp .net 表单中获取的四个字段插入到数据库中的表中,但我无法做到这一点。请给我完全编译的代码,它可以工作并且成功地将 4 个值输入到 db
这是我使用的代码
<script type="text/javascript">
$(document).ready(function () {
$("#<%= Button1.ClientID %>").click(function () {
/* $(".style2").hide();
return false; */
var name = $('#<%=TextBox1.ClientID%>').val();
var email = $('#<%=TextBox2.ClientID%>').val();
var phone = $('#<%=TextBox3.ClientID%>').val();
var message = $('#<%=TextBox4.ClientID%>').val();
var json = "{'" + name + "','" + email + "','" + phone + "','" + message + "'}";
alert("comes here");
$ajax({
type:"POST",
url:"JqueryFunction.aspx/insert_details",
data:'{name: "' + $("#<%=TextBox1.ClientID%>")[0].value + '",email: "' + $("#<%=TextBox2.ClientID%>")[0].value + '",phone: "' + $("#<%=TextBox3.ClientID%>")[0].value + '",message: "' + $("#<%=TextBox4.ClientID%>")[0].value + '"}',
clientType:"application/json; charset=utf-8",
datatype:"json",
async: false,
success: function () { alert("success"); },
error: function(){alert("error");}
});
});
});
</script>
这是 insert_details 函数:
public static void insert_details(string name,string emailid,string phone,string message)
{
SqlConnection Con = new SqlConnection(@"Server=HP-PC;Integrated Security=True;" + "Database=DB1");
//Console.WriteLine("connected to db");
string query = "INSERT INTO [SubmissionFormTable]([Name],[Email],[Phone],[Message])" +
"VALUES (@Name,@Email,@Phone,@Message)";
SqlCommand cmd = new SqlCommand(query, Con);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Email", emailid);
cmd.Parameters.AddWithValue("@Phone", phone);
cmd.Parameters.AddWithValue("@Message", message);
try
{
Con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
Con.Close();
}
//string str_insert = "INSERT INTO remark VALUES(@remark)";
//SqlParameter[] parameter = {
// new SqlParameter("@remark", remark),
// };
// int abc = DBclass.TruyVan_XuLy_VoiThamSo(str_insert, parameter);
}