3

我正在尝试将从 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);
}
4

1 回答 1

1

更新您的 js 代码,如下所示,并尝试使用 JSON.stringify 将值转换为 JSON 以传递您的参数:

$(document).ready(function () {
        $("#<%= Button1.ClientID %>").click(function () {
            var name = $('#<%=TextBox1.ClientID%>').val();
            var email = $('#<%=TextBox2.ClientID%>').val();
            var phone = $('#<%=TextBox3.ClientID%>').val();
            var message = $('#<%=TextBox4.ClientID%>').val();
            var data = JSON.stringify({ name: name, email: email, phone: phone, message: message });
            alert("comes here");
            $ajax({
                  type:"POST",
                  url:"JqueryFunction.aspx/insert_details",
                  data:data,
                  contentType: "application/json; charset=utf-8",
                  datatype:"json",
                  success: function () { alert("success"); },
                  error: function(){alert("error");}
                  });
        });
    });

上面的代码没有问题。

编辑
您的方法应该是 WebMethode :

[System.Web.Services.WebMethod]
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);
}
于 2012-11-22T13:53:28.213 回答