使用JSON,ASP.NET Web服务和jQuery插入数据 [英] Insert Data using JSON, ASP.NET Web services and jQuery

查看:68
本文介绍了使用JSON,ASP.NET Web服务和jQuery插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是JSON n jquery的新手。我做一个简单的数据插入应用程序,但它不起作用。我有任何经验,请帮助。





目前数据未插入数据库



webservice

------------





i am new to JSON n jquery. i make a simple application for insert of data but it not work. i any have experiece in this please help.


currently data is not inserted to database

webservice
------------

public class WebService : System.Web.Services.WebService {

    public WebService ()
    {
        //InitializeComponent();
    }
    string conStr = WebConfigurationManager.ConnectionStrings["testconn"].ConnectionString;
    int rowsInserted = 0;

    [WebMethod]
    public string InsertWebservice(int id, string name, string address)
    {
        using (SqlConnection conn = new SqlConnection(conStr))
        {
            string sql = string.Format(@"INSERT INTO [TestDB].[dbo].[emp] ([id],[name],[Address]) VALUES ('{0}','{1}','{2}')", id, name, address);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            conn.Open();
            rowsInserted = cmd.ExecuteNonQuery();
            conn.Close();
            cmd = null;
        }
        return string.Format("Thank you, {0} number of rows inserted!", rowsInserted);

    }



-------------
$ p $ b at aspx page

-------------


-------------
at aspx page
-------------

<table>
   <tr><td>  Id:</td><td><asp:TextBox ID="_id" runat="server"></asp:TextBox></td></tr>
    <tr><td>Name:</td><td><asp:TextBox ID="_txtname" runat="server"></asp:TextBox></td></tr>
    <tr><td>Address:</td><td><asp:TextBox ID="_addr" runat="server"></asp:TextBox></td></tr>

   </table>

  <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="CallService();"

              />

 <script type="text/javascript">

       $(insert);
       function insert() {
           $('#Button1').click(CallService);
       }
       
       function CallService() {
         
           var id = $('#_id').val();
           var name = $('#_txtname').val();
           var addr = $('#_addr').val();
           $.ajax({
               type: "POST",
         
       url: "WebService.asmx/InsertWebservice",   
               
               contentType: "application/json;charset=utf-8",
               dataType: "json",                         

               //  data: "{ 'id': '" + id + "','name': '" + name + "', 'address':'" + addr + "'}",
               data: JSON.stringify({ id: 'id', name: 'name', address: 'address' }),
               async: false,
               success: function (data) {
                   $('#message').text(data.d);
               }
           });
       }
        </script>   

推荐答案

(insert);
函数insert(){
(insert); function insert() {


('#Button1')。click(CallService);
}

函数CallService(){

var id =
('#Button1').click(CallService); } function CallService() { var id =


('#_ id')。val() ;
var name =
('#_id').val(); var name =


这篇关于使用JSON,ASP.NET Web服务和jQuery插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆