-->

How to Fetch & Show Sql Server database data in ASP.NET Page Using Ajax (Jquery).

Introduction

In this post, I am implementing how to Fetch & Show Sql Server database data in ASP.NET Page Using Ajax (Jquery).

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch from Jquery function.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Webpage and Design for show database data.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code
  1. <h3>Load Database Data into Page using Jquery.</h3>
  2. <input id="btnLoadData" type="button" value="Load Database Data" onclick="GetCompanies()" />
  3. <div id="UpdatePanel">
  4.  
  5. </div>
JS Code
  1. <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. function GetCompanies() {
  4. $("#UpdatePanel").html("<div style='text-align:center; background-color:yellow; border:1px solid red; padding:3px; width:200px'>Please Wait...</div>");
  5. $.ajax({
  6. type: "POST",
  7. url: "Default.aspx/GetCompanies",
  8. data: "{}",
  9. dataType: "json",
  10. contentType: "application/json; charset=utf-8",
  11. success: OnSuccess,
  12. error: OnError
  13. });
  14. }
  15. function OnSuccess(data) {
  16. var TableContent = "<table border='0'>" +
  17. "<tr>" +
  18. "<td>Rank</td>" +
  19. "<td>Company Name</td>" +
  20. "<td>Revenue</td>" +
  21. "<td>Industry</td>" +
  22. "</tr>";
  23. for (var i = 0; i < data.d.length; i++) {
  24. TableContent += "<tr>" +
  25. "<td>"+ data.d[i].Rank +"</td>" +
  26. "<td>"+data.d[i].CompanyName+"</td>" +
  27. "<td>"+data.d[i].Revenue+"</td>" +
  28. "<td>"+data.d[i].Industry+"</td>" +
  29. "</tr>";
  30. }
  31. TableContent += "</table>";
  32.  
  33. $("#UpdatePanel").html(TableContent);
  34. }
  35. function OnError(data) {
  36.  
  37. }
  38. </script>

Step-6: Write this function  page_load event for fetch different languages(hindi, english, bengali, marathi etc.) data from database and show in Page .

Write this function into your page behind.
  1. [WebMethod]
  2. [ScriptMethod(ResponseFormat= ResponseFormat.Json)]
  3. public static List<TopCompany> GetCompanies()
  4. {
  5. System.Threading.Thread.Sleep(5000);
  6. List<TopCompany> allCompany = new List<TopCompany>();
  7. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  8. {
  9. allCompany = dc.TopCompanies.ToList();
  10. }
  11. return allCompany;
  12. }

Step-7: Run Application.


Related Post


Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: