Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am just creating database in sql server and insert data into the table. i want to display the data in webpage using jquery in gridview format
Posted

1 solution

You can send ajax request to the server that will be made through jQuery. You donot need to append any additional headers with the request but the web page you're trying to get would be required.

For instance, this is your server-side code to get the data from the database,

C#
var db = Database.Open("database_name");
var selectQuery = "SELECT * FROM table_name WHERE column_name = value";
var result = db.Query(selectQuery);

// now generate the GridView for the result, and return the HTML
// generated from the above code.


.. above code would return the HTML for the GridView of your database's table, usually that is the result that you want to show on the web page. You can read the data from database in any other method (SqlClient method) too. This was just an example.

Create a jQuery ajax request to capture that data, as this,

JavaScript
$(document).ready(function () {
   $.ajax({
      url: 'yourWebPage',
      success: function (data) {
        // a request was sent, and was successfull
        // the result from server is saved in 'data'
        $('.result').html(data); // an element with class result in your DOM
      }
   });
});


You will now get the result from your server-side page in your HTML DOM once this query would return with a success.

For more on jQuery ajax, read: http://api.jquery.com/jquery.ajax/[^]
 
Share this answer
 
Comments
Grant Weatherston 24-Nov-14 9:24am    
the Url you're directing the Ajax request to would have to be either an xml page and then build an object of data in a string builder, to use in the Response.Write() method.

or utilise a WCF webservice, or WebApi
Afzaal Ahmad Zeeshan 24-Nov-14 11:17am    
No - there is actually no need to transfer yourself to WCF or Web API, you can do this using a simple Web Pages application too. Just return an HTML filled with the content you want to show and the markup would be returned as a result.

Response.Write() is a good thing, but what would happen if you have to write a lengthy syntax or markup, Response.Write is good while writing a single or 3 4 lined paragraph. Otherwise, just create a variable add data to it and fill the HTML markup with it. The entire page would be captured and returned, it is better to add just a single <div> element to be returned.
Grant Weatherston 24-Nov-14 11:25am    
i usually utilize a string builder, do you know any examples of the way you do it?
Afzaal Ahmad Zeeshan 24-Nov-14 11:31am    
Yup! Just fill the HTML markup, and once returned it will be in the form of result from ajax request. :)

Following is allowed in Web Pages; no need for HTML tags such as html, body etc

@{
name = UrlData[0]; // get the first parameter in the URL
}

<div>
@name
</div>


Now, if I try to connect to it using an ajax request, I will get the name (Afzaal-Ahmad-Zeeshan) I passed to the URL, as http://www.example.com/page/Afzaal-Ahmad-Zeeshan. This way, I do it, however you can also do that using StringBuilder - if you are going to build entire syntax inside the string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900