Rahul Patil

Rahul Patil

  • 1.5k
  • 180
  • 29.4k

how to retriving the data from the database??

Dec 9 2019 6:57 AM
I m performing crud operation using ajax jquery. I m inserting data in the database but I want to get the data and that data update and delete run time but my getdata method does not work??
Emploee.aspx.cs
  1. [WebMethod] //insert  
  2. public static void insertdata(string name, int age, string address)  
  3. {  
  4. cn.Open();  
  5. SqlCommand cmd = new SqlCommand("sp_tblEmployee_Insert", cn);  
  6. cmd.CommandType = CommandType.StoredProcedure;  
  7. cmd.Parameters.AddWithValue("@EmpName", name);  
  8. cmd.Parameters.AddWithValue("@EmpAge", age);  
  9. cmd.Parameters.AddWithValue("@EmpAddress", address);  
  10. cmd.ExecuteNonQuery();  
  11. cn.Close();  
  12. }  
  13. [WebMethod]  
  14. public static string GetEmpData()  
  15. {  
  16. cn.Open();  
  17. string data = "";  
  18. SqlCommand cmd = new SqlCommand("sp_tblEmployee_Select", cn);  
  19. cmd.CommandType = CommandType.StoredProcedure;  
  20. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  21. DataSet ds = new DataSet();  
  22. da.Fill(ds);  
  23. cn.Close();  
  24. if (ds.Tables[0].Rows.Count > 0)  
  25. {  
  26. data = JsonConvert.SerializeObject(ds.Tables[0]);//read and write the jsontext  
  27. }  
  28. return data;  
  29. }  
Emploee.aspx
  1. <head runat="server">  
  2. <link href="StyleSheet.css" rel="stylesheet" />  
  3. <script src="scripts/jquery-3.4.1.min.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function () {  
  6. getdata();  
  7. });  
  8. //insert  
  9. function SaveData()  
  10. {  
  11. if ($("#btnsubmit").val() == "Submit")  
  12. {  
  13. $.ajax({  
  14. url: 'Emploee.aspx/insertdata',  
  15. type: 'post',  
  16. contentType: 'application/json;charset=utf-8',  
  17. dataType: 'json',  
  18. data: "{name:'" + $("#txtName").val() + "',age:'" + $("#txtAge").val() + "',address:'" + $("#txtAddress").val() + "'}",  
  19. success: function () {  
  20. alert("Insert Data Successfully");  
  21. getdata();  
  22. },  
  23. Error: function () {  
  24. alert("Insert Error");  
  25. }  
  26. });  
  27. }  
  28. else  
  29. {  
  30. $.ajax({  
  31. url: 'Emploee.aspx/Update',  
  32. type: 'post',  
  33. contentType: 'application/json;charset=utf-8',  
  34. dataType: 'json',  
  35. data: "{Id:'" + id + "',name:'" + $("#txtName").val + "',age:'" + $("#txtAge").val + "',address:'" + $("#txtAddress").val + "'}",  
  36. success: function () {  
  37. alert("Update data Successfully");  
  38. getdata();  
  39. },  
  40. error: function () {  
  41. alert("Update Error");  
  42. }  
  43. });  
  44. }  
  45. }  
  46. //getdata  
  47. function getdata() //i think this function is not work  
  48. {  
  49. $.ajax({  
  50. url: 'Emloyee.aspx/GetEmpData',  
  51. type: 'get',  
  52. contentType: 'application/json;charset=utf-8',  
  53. dataType: 'json',  
  54. data: "{}",  
  55. success: function (data) {  
  56. data = JSON.parse(data.d);  
  57. $("#tbl").find("tr:gt(0)").remove();  
  58. for (var i = 0; i < data.length; i++) {  
  59. $("#tbl").append('<tr><td>' + data[i].EmpName + '</td><td>' + data[i].EmpAge + '</td><td>' + data[i].EmpAddress + '</td><td><input type="button" id="btndelete" value="Delete" onclick="DeleteData(' + data[i].EmpId + ')" /></td> <td><input type="button" id="btnedit" value="Edit" onclick="EditData(' + data[i].EmpId + ')" /></td> </tr>');  
  60. }  
  61. },  
  62. Error: function () {  
  63. alert("get error");  
  64. }  
  65. });  
  66. }  
  67. </script>  
  68. <title>Jquery Crud Index Update Delete</title>  
  69. </head>  
  70. <body>  
  71. <form id="form1" runat="server">  
  72. <div>  
  73. <table>  
  74. <tr>  
  75. <td>Name:</td>  
  76. <td>  
  77. <input type="text" id="txtName" /></td>  
  78. </tr>  
  79. <tr>  
  80. <td>Age:</td>  
  81. <td>  
  82. <input type="text" id="txtAge" /></td>  
  83. </tr>  
  84. <tr>  
  85. <td>Address:</td>  
  86. <td>  
  87. <input type="text" id="txtAddress" /></td>  
  88. </tr>  
  89. <tr>  
  90. <td></td>  
  91. <td>  
  92. <input type="button" id="btnsubmit" value="Submit" onclick="SaveData()" /></td>  
  93. </tr>  
  94. </table>  
  95. <table id="tbl">  
  96. <tr>  
  97. <th>Enmployee Name</th>  
  98. <th>Employee Age</th>  
  99. <th>Employee Address</th>  
  100. <th></th>  
  101. <th></th>  
  102. </tr>  
  103. </table>  
  104. </div>  
  105. </form>  
  106. </body>  
  107. </html>  
store procedure sp_tblEmployee_Select
  1. CREATE PROCEDURE  
  2. sp_tblEmployee_Select  
  3. AS BEGIN select * from tblEmployee  
  4. END  
I want to update and delete the data so fetch the record of database using of getdata method but getdata method is not working??

Answers (3)