Tri Setia

Tri Setia

  • 1.3k
  • 464
  • 24.4k

CRUD Using JQuery and Ajax in ASP.NET Webform

Mar 16 2021 3:29 PM
I'm learn for CRUD using Jquery and Ajax in ASP.NET Webform but I'm stuck on delete and update function. For the insert function working properly, but for delete and update function is not working. l'll try hard find the error but I can not find the error and can not solve it.
 
this the crud_ajax.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="crud_ajax.aspx.cs" Inherits="crud_ajax" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title>Crud Ajax</title>  
  6. <style type="text/css">  
  7. body  
  8. {  
  9. font-family: Arial;  
  10. font-size: 10pt;  
  11. }  
  12. </style>  
  13. </head>  
  14. <body>  
  15. <form id="form1" runat="server">  
  16. <table border="0" cellpadding="0" cellspacing="0">  
  17. <tr>  
  18. <td>ID:  
  19. </td>  
  20. <td>  
  21. <asp:TextBox ID="txtID" runat="server" Text="" />  
  22. </td>  
  23. </tr>  
  24. <tr>  
  25. <td>Username:  
  26. </td>  
  27. <td>  
  28. <asp:TextBox ID="txtUsername" runat="server" Text="" />  
  29. </td>  
  30. </tr>  
  31. <tr>  
  32. <td>Password:  
  33. </td>  
  34. <td>  
  35. <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />  
  36. </td>  
  37. </tr>  
  38. <tr>  
  39. <td></td>  
  40. <td>  
  41. <asp:Button ID="btnSave" Text="Save" runat="server" />  
  42. <asp:Button ID="btnUpdate" Text="Update" runat="server" />  
  43. <asp:Button ID="btnDelete" Text="Delete" runat="server" />  
  44. </td>  
  45. </tr>  
  46. </table>  
  47. <hr />  
  48. <asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2"  
  49. HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2">  
  50. <Columns>  
  51. <asp:BoundField DataField="Username" HeaderText="Username" />  
  52. <asp:BoundField DataField="Password" HeaderText="Password" />  
  53. </Columns>  
  54. </asp:GridView>  
  55. </form>  
  56. <script src="jquery.js"></script>  
  57. <script src="json2.js"></script>  
  58. <script type="text/javascript">  
  59. $(document).ready(function () {  
  60. $(document).on("click""[id*=btnSave]",function () {  
  61. var Username = $("[id*=txtUsername]");  
  62. var Password = $("[id*=txtPassword]");  
  63. $.ajax({  
  64. type: "POST",  
  65. url: "crud_ajax.aspx/SaveUser",  
  66. data: '{Username:"'+Username.val()+'",Password:"'+Password.val()+'"}',  
  67. contentType: "application/json; charset=utf-8",  
  68. dataType: "json",  
  69. success: function (response) {  
  70. alert("User has been added successfully.");  
  71. window.location.reload();  
  72. }  
  73. });  
  74. return false;  
  75. });  
  76. })  
  77. </script>  
  78. <script type="text/javascript">  
  79. $(document).ready(function () {  
  80. $(document).on("click""[id*=btnUpdate]",function () {  
  81. var ID = $("[id*=txtID]");  
  82. var Username = $("[id*=txtUsername]");  
  83. var Password = $("[id*=txtPassword]");  
  84. $.ajax({  
  85. type: "POST",  
  86. url: "crud_ajax.aspx/UpdateUser",  
  87. data: '{Username:"'+Username.val()+'",Password:"'+Password.val()+'",id:"'+ID.val()+'"}',  
  88. contentType: "application/json; charset=utf-8",  
  89. dataType: "json",  
  90. success: function (response) {  
  91. alert("User has been Update successfully.");  
  92. window.location.reload();  
  93. }  
  94. });  
  95. return false;  
  96. });  
  97. });  
  98. </script>  
  99. <script type="text/javascript">  
  100. $(document).ready(function () {  
  101. $(document).on("click","[id*=btnDelete]",function () {  
  102. var ID = $("[id*=txtID]");  
  103. $.ajax({  
  104. type: "POST",  
  105. url: "crud_ajax.aspx/DeleteUser",  
  106. data: '{deleteUserID:"'+ID.val()+'"}',  
  107. contentType: "application/json; charset=utf-8",  
  108. dataType: "json",  
  109. success: function (response) {  
  110. alert("User has been Delete successfully.");  
  111. window.location.reload();  
  112. }  
  113. });  
  114. return false;  
  115. });  
  116. });  
  117. </script>  
  118. </body>  
  119. </html>  
This the code bihind crud_ajax.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Configuration;  
  10. using System.Web.Services;  
  11. using System.Web.Script.Services;  
  12. public partial class crud_ajax : System.Web.UI.Page  
  13. {  
  14. protected void Page_Load(object sender, EventArgs e)  
  15. {  
  16. if (!this.IsPostBack)  
  17. {  
  18. string constr = ConfigurationManager.ConnectionStrings["db_tes"].ConnectionString;  
  19. using (SqlConnection con = new SqlConnection(constr))  
  20. {  
  21. using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users"))  
  22. {  
  23. using (SqlDataAdapter sda = new SqlDataAdapter())  
  24. {  
  25. DataTable dt = new DataTable();  
  26. cmd.CommandType = CommandType.Text;  
  27. cmd.Connection = con;  
  28. sda.SelectCommand = cmd;  
  29. sda.Fill(dt);  
  30. gvUsers.DataSource = dt;  
  31. gvUsers.DataBind();  
  32. }  
  33. }  
  34. }  
  35. }  
  36. }  
  37. [WebMethod]  
  38. [ScriptMethod]  
  39. public static void SaveUser(string Username, string Password)  
  40. {  
  41. string constr = ConfigurationManager.ConnectionStrings["db_tes"].ConnectionString;  
  42. using (SqlConnection con = new SqlConnection(constr))  
  43. {  
  44. using (SqlCommand cmd = new SqlCommand("INSERT INTO Users(Username,Password) VALUES(@Username, @Password)"))  
  45. {  
  46. cmd.CommandType = CommandType.Text;  
  47. cmd.Parameters.AddWithValue("@Username", Username);  
  48. cmd.Parameters.AddWithValue("@Password", Password);  
  49. cmd.Connection = con;  
  50. con.Open();  
  51. cmd.ExecuteNonQuery();  
  52. con.Close();  
  53. }  
  54. }  
  55. }  
  56. [WebMethod]  
  57. [ScriptMethod]  
  58. public static void UpdateUser(string Username, string Password, int ID)  
  59. {  
  60. string constr = ConfigurationManager.ConnectionStrings["db_tes"].ConnectionString;  
  61. using (SqlConnection con = new SqlConnection(constr))  
  62. {  
  63. using (SqlCommand cmd = new SqlCommand("Update Users SET Username=@Username,Password=@Password Where id=@id)"))  
  64. {  
  65. cmd.CommandType = CommandType.Text;  
  66. cmd.Parameters.AddWithValue("@Username", Username);  
  67. cmd.Parameters.AddWithValue("@Password", Password);  
  68. cmd.Parameters.AddWithValue("@id", ID);  
  69. cmd.Connection = con;  
  70. con.Open();  
  71. cmd.ExecuteNonQuery();  
  72. con.Close();  
  73. }  
  74. }  
  75. }  
  76. [WebMethod]  
  77. [ScriptMethod]  
  78. public static void DeleteUser(int deleteUserID)  
  79. {  
  80. string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
  81. using (SqlConnection con = new SqlConnection(constr))  
  82. {  
  83. using (SqlCommand cmd = new SqlCommand("DELETE FROM Users WHERE id = @id"))  
  84. {  
  85. cmd.Parameters.AddWithValue("@id", deleteUserID);  
  86. cmd.Connection = con;  
  87. con.Open();  
  88. cmd.ExecuteNonQuery();  
  89. con.Close();  
  90. }  
  91. }  
  92. }  
  93. }  
please help, any help I appreciate.

Answers (3)