Jun 21, 2010

Inserting data with out page refresh using jquery,json,asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="register.aspx.cs" Inherits="jquery.register" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>test Page</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>

    <script src="jquery-1.2.6.js" type="text/javascript"></script>

    <script type="text/javascript">

            $(document).ready(function() {
            $("#btnsubmit").click(function(){
//          alert($("#txtname").val());
//          alert($("#txtemail").val());
//           alert($("#txtmobile").val()); 
             var name = $("#txtname").val();
         
              var email = $("#txtemail").val();
              var mobile = $("#txtmobile").val();
             var options = {

                        type: "POST",

                        url: "register.aspx/login",

                        data: "{name:'" + name + "',email:'" + email + "',mobile:'" + mobile + "'}",

                        contentType: "application/json; charset=utf-8",

                        dataType: "json",
                         async: false,


                        success: function(response) {
                         
                           
                                alert(response.d);

                              
                                           
                           
                         

                        },
                         error: function(request, status, errorThrown) {
                      alert(status);
                         }


                    };
             
             $.ajax(options);

  
});

 });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td align="right">
                Name:
            </td>
            <td>
                <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right">
                Email:
            </td>
            <td>
                <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right">
                Mobile:
            </td>
            <td>
                <asp:TextBox ID="txtmobile" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                <asp:Button ID="btnsubmit" runat="server" Text="Submit" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>
    </form>
</body>
</html>




code:


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;
using System.Data.SqlClient;

namespace jquery
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]

        public static string login(string name,string email,string mobile)
        {
            string response = "";
            string name1 = name;
            string email1 = email;
            string mobile1 = mobile;
            SqlConnection con = new SqlConnection("user id=sa;password=sa;database=pavan;data source=localhost");
            string s="insert into reg(name,username,hobies)values('"+name+"','"+email+"','"+mobile+"')";
            SqlCommand cmd = new SqlCommand(s, con);
            con.Open();
           int s1= cmd.ExecuteNonQuery();
            con.Close();
            if (s1 == 1)
            {
                response = "Inserted";

            }

            return response;
         

        }
    }
}

No comments:

Post a Comment