In this article I’m going to explain how to call ASP.NET page method to execute store procedure with multiple parameters using jQuery ajax.

        One of my blog reader asked me a question that how to execute store procedure directly using jQuery. Actually we can’t execute store procedure directly by jQuery. But we can call ASP.NET page method to execute store procedure. I have already written few article related to call ASP.NET page method using jQuery. Please refer following articles

            Using jQuery AJAX to call ASP.NET page method

            Save data to database without postback using jQuery ajax in ASP.NET

            Bind GridView using jQuery in ASP.NET

          Here we go, Please follow the steps given below

Table Design:

 

Create Store Procedure:

CREATE PROCEDURE [dbo].[Insert_Data]

          (@name varchar(50),

           @age int,

           @sex varchar(10))

AS

BEGIN

          SET NOCOUNT ON; 

    INSERT INTO Personal_Details (Name,Age,Sex)

    VALUES(@name,@age,@sex)   

    END

GO


Add jQuery Library:

        First we have to add jQuery library. You can download jQuery library here, or you can use following jQuery CDN.

<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>

 

Add Script:

Here we have to write jQuery ajax method to call ASP.NET page method.

<script type="text/javascript">

        $(document).ready(function () {

            $('#btnsubmit').click(function () { 

                var name = $('#txtName').val();

                var age = $('#txtAge').val();

                var sex = $('#txtSex').val();

                $.ajax({

                    type: 'POST',

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

                    url: 'Default.aspx/Insert_Data',

                    data: "{'name':'" + name + "','age':'" + age + "','sex':'" + sex + "'}",

                    async: false,

                    success: function (response) {

                        $('#txtName').val(''); $('#txtAge').val(''); $('#txtSex').val('');

                        alert("Record saved successfully..!!");

                    },

                    error: function () {

                        alert("Error");

                    }

                });

            });

        });

    </script>

 

Connection String:

       Please add connection string on web.config file. 

<connectionStrings>

          <add name="myConnectionString" connectionString="Data Source=localhost; Initial                      Catalog=Demo; Integrated Security=True"/>

</connectionStrings>

 

Page Method:

Here we have to write page Method which is used to store data to SQL Server database. Method should be declared as static with the [WebMethod] attribute.

    [WebMethod]

    public static string Insert_Data(string name, string age, string sex)

    {       

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);

        try

        {           

            SqlCommand cmd = new SqlCommand();                       

            conn.Open();           

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "Insert_Data";

            cmd.Parameters.AddWithValue("@name", name);

            cmd.Parameters.AddWithValue("@age", age);

            cmd.Parameters.AddWithValue("@sex", sex);

            cmd.Connection = conn;

            cmd.ExecuteNonQuery();

            conn.Close();

            return "success";

        }

        catch (Exception ex)

        {

            return "error";

        }

    }

 

Putting it all together:

HTML code:

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>

    <title></title>

    <script type="text/javascript">

        $(document).ready(function () {

            $('#btnsubmit').click(function () { 

                var name = $('#txtName').val();

                var age = $('#txtAge').val();

                var sex = $('#txtSex').val(); 

                $.ajax({ 

                    type: 'POST',

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

                    url: 'Default.aspx/Insert_Data',

                    data: "{'name':'" + name + "','age':'" + age + "','sex':'" + sex + "'}",

                    async: false,

                    success: function (response) {

                        $('#txtName').val(''); $('#txtAge').val(''); $('#txtSex').val('');

                        alert("Record saved successfully..!!");

                    },

                    error: function () { 

                        alert("Error");

                    }

                }); 

            });

        });

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <table align="center" class="style1">

            <tr> 

<td> Name </td>

                <td> <asp:TextBox ID="txtName" runat="server" ClientIDMode="Static"></asp:TextBox> </td>               

            </tr>

            <tr>

                <td> Age </td>

                <td> <asp:TextBox ID="txtAge" runat="server" ClientIDMode="Static"></asp:TextBox> </td>               

            </tr>

            <tr>

                <td> Sex </td>

                <td> <asp:TextBox ID="txtSex" runat="server" ClientIDMode="Static"></asp:TextBox> </td>               

            </tr>

            <tr>

                <td> <input type="button" id="btnsubmit" value="Submit" /> </td>               

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

 

C# code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.Web.Services; 

public partial class _Default : System.Web.UI.Page

{  

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    [WebMethod]

    public static string Insert_Data(string name, string age, string sex)

    {       

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);

        try

        {           

            SqlCommand cmd = new SqlCommand();                       

            conn.Open();           

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "Insert_Data";

            cmd.Parameters.AddWithValue("@name", name);

            cmd.Parameters.AddWithValue("@age", age);

            cmd.Parameters.AddWithValue("@sex", sex);

            cmd.Connection = conn;

            cmd.ExecuteNonQuery();

            conn.Close();

            return "success";

        }

        catch (Exception ex)

        {

            return "error";

        }

    }

}

 

cytotec abortion

http://www.extrageek.com/template/default.aspx?an-abortion-pill buy abortion pills online
medical abortion pill online abortion pill abortion pill


View demo Download
http://www.dotnetfox.com/Document/1108/0Demo2.gif

If you enjoyed this article, get email updates (it's free).


Related Articles
  • Ajax Accordian control in ASP.NET In this article I’m going to explain how to use Ajax Accordian control in asp.net. The Accordian is a web control that allows you to provide multiple panes and display them at a time and only one panes expanded at a time. Accordian control has a templates for header and content. Also we can add server controls to the Accordian.
  • Ajax Calendar Extender in ASP.NET In this article I’m going to explain how to use Ajax Calendar Extender control in asp.net. Ajax Calendar Extender provides client side date picking functionality with customizable date and UI in a popup control. You can interact with calendar by clicking today date, past and future date. Also you can modify date format, popup display position and set custom styles to the calendar.
  • How to use Ajax ModalPopup Extender in ASP.NETIn this article I’m going to explain how to use Ajax Modalpopup Extender control in asp.net. The ModalPopup extender allows a page to display content to the user in a “modal” manner which prevents the user from interacting with rest of the page. We can add any web controls inside the modal content. When modal popup displayed only the modal content can be interacted with the user and rest of the pa...
  • How to use Ajax CalenderExtender in ASP.NET GridViewIn this article I’m going to explain how to use Ajax CalenderExtender in ASP.NET GridView. Today one of my blog subscriber asked me a question that’s how to use Ajax CalenderExtender in ASP.NET GridView. I hope you already know how to use Ajax CalenderExtender in normal page. There is no different to use Ajax CalenderExtender in normal page and GridView.
  • How to use Ajax ColorPickerExtender in ASP.NETIn this article I’m going to explain how to use Ajax ColorPickerExtender in ASP.NET. Ajax ColorPickerExtender provides client-side color-picking functionality with UI in a popup control. It can be attached to any ASP.NET TextBox control.
  • Google Geo Chart or Geo map with custom Color and Tooltip in ASP.NET I have already written an article for implementing Geo Chart in ASP.NET. In this article I’m going to explain how to use custom colors and tooltip in Google Geo Chart. In this demo I have implemented Geo chart for countries and percentage of world populations. For that first we have to create table with following fields.
  • Ajax Cascading DropDown example with database in ASP.NETCascadingDropDown is an ASP.NET AJAX extender that can be attached to an ASP.NET DropDownList control. It will automatically populate set of DropDownList controls without page postback. Each time the selection of one the DropDownList controls changes, the CascadingDropDown makes a call to a specified web service to retrieve the list of values for the next DropDownList in the set.
  • AJAX FileUpload or multiple FileUpload with progress example in ASP.NETThe AjaxFileUpload control is used to upload multiple files to the server folder. AjaxFileUpload uses one of two methods of showing file upload progress. If you are using a browser which fully supports the HTML5 File API (such as Google Chrome or Mozilla Firefox) then upload progress is displayed using client-side events. If, on the other hand, you are using a browser which does not fully support ...
  • UpdatePanel with Triggers in ASP.NETASP.NET AJAX provides the most visible feature that the ability to partial or incremental page updates with PostBack to the server. Before AJAX implementation is ASP.NET page update require a round trip to the server which requires a page refresh. UpdatePanel control remove the requirement to refresh the whole page with each postback which improves user experience. While working with UpdatePanel ...
  • Using jQuery ajax to call ASP.NET page method to execute store procedureCall ASP.NET page method to execute store procedure with multiple parameters using jQuery ajax. One of my blog reader asked me a question that how to execute store procedure directly using jQuery. Actually we can’t execute store procedure directly by jQuery. But we can call ASP.NET page method to execute store procedure.

Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox