Monday, 27 January 2014

Retrieve Data from SQL Server and Dispaly using Jquery in

Take the look at this:

In the previous example we saw, How to insert data in database using jquery in now we will see how to display data in jquery in Here we will use html page and a aspx.cs file.
First Step:
CreateTable with column name "Name" and "Email"
Second Step:
 Create a new project
Third Step:
Add a html file in your project.
Fourth Step:
Create input textbox and a button with id "Button1".
Fifth Step:
Add the following code in the aspx.cs file


    public static string InsertMethod(string Name)
        string shashi = "";
         StringBuilder sb = new StringBuilder();
        SqlConnection con = new SqlConnection("Data Source=PREETI-PC;Initial Catalog=Record;Integrated Security=True");


            SqlCommand cmd = new SqlCommand("select * from Test where Name='"+Name+"'", con);


                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                shashi = sb.ToString();
                return shashi;



Sixth Step: 

<link href=""

        rel="stylesheet" type="text/css" />

    <script type="text/javascript" src=""></script>

    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(document).ready(function () {

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


                    type: 'POST',

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

                    url: 'Default.aspx/InsertMethod',

                    data: "{'Name':'" + document.getElementById('Text1').value + "' }",
                    async: false,

                    success: function (data) {


                        //                        $('#Text6').val('');
                        //                        $('#Text1').val('');
                        //                        $('#Text2').val('');
                                                document.getElementById('message').innerHTML = data.d ;


                    error: function ()

                    { alert("error"); }



  your process is okay.

Now here in code behind we have added string builder object and append our data according to our requirement you can also append it in the form of table. Return the sting "shashi" which contain string builder object in string formate.
This video is not exactly same as written but this may also help you.