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
- [WebMethod]
- public static void insertdata(string name, int age, string address)
- {
- cn.Open();
- SqlCommand cmd = new SqlCommand("sp_tblEmployee_Insert", cn);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@EmpName", name);
- cmd.Parameters.AddWithValue("@EmpAge", age);
- cmd.Parameters.AddWithValue("@EmpAddress", address);
- cmd.ExecuteNonQuery();
- cn.Close();
- }
- [WebMethod]
- public static string GetEmpData()
- {
- cn.Open();
- string data = "";
- SqlCommand cmd = new SqlCommand("sp_tblEmployee_Select", cn);
- cmd.CommandType = CommandType.StoredProcedure;
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- cn.Close();
- if (ds.Tables[0].Rows.Count > 0)
- {
- data = JsonConvert.SerializeObject(ds.Tables[0]);
- }
- return data;
- }
Emploee.aspx
- <head runat="server">
- <link href="StyleSheet.css" rel="stylesheet" />
- <script src="scripts/jquery-3.4.1.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function () {
- getdata();
- });
-
- function SaveData()
- {
- if ($("#btnsubmit").val() == "Submit")
- {
- $.ajax({
- url: 'Emploee.aspx/insertdata',
- type: 'post',
- contentType: 'application/json;charset=utf-8',
- dataType: 'json',
- data: "{name:'" + $("#txtName").val() + "',age:'" + $("#txtAge").val() + "',address:'" + $("#txtAddress").val() + "'}",
- success: function () {
- alert("Insert Data Successfully");
- getdata();
- },
- Error: function () {
- alert("Insert Error");
- }
- });
- }
- else
- {
- $.ajax({
- url: 'Emploee.aspx/Update',
- type: 'post',
- contentType: 'application/json;charset=utf-8',
- dataType: 'json',
- data: "{Id:'" + id + "',name:'" + $("#txtName").val + "',age:'" + $("#txtAge").val + "',address:'" + $("#txtAddress").val + "'}",
- success: function () {
- alert("Update data Successfully");
- getdata();
- },
- error: function () {
- alert("Update Error");
- }
- });
- }
- }
-
- function getdata()
- {
- $.ajax({
- url: 'Emloyee.aspx/GetEmpData',
- type: 'get',
- contentType: 'application/json;charset=utf-8',
- dataType: 'json',
- data: "{}",
- success: function (data) {
- data = JSON.parse(data.d);
- $("#tbl").find("tr:gt(0)").remove();
- for (var i = 0; i < data.length; i++) {
- $("#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>');
- }
- },
- Error: function () {
- alert("get error");
- }
- });
- }
- </script>
- <title>Jquery Crud Index Update Delete</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>Name:</td>
- <td>
- <input type="text" id="txtName" /></td>
- </tr>
- <tr>
- <td>Age:</td>
- <td>
- <input type="text" id="txtAge" /></td>
- </tr>
- <tr>
- <td>Address:</td>
- <td>
- <input type="text" id="txtAddress" /></td>
- </tr>
- <tr>
- <td></td>
- <td>
- <input type="button" id="btnsubmit" value="Submit" onclick="SaveData()" /></td>
- </tr>
- </table>
- <table id="tbl">
- <tr>
- <th>Enmployee Name</th>
- <th>Employee Age</th>
- <th>Employee Address</th>
- <th></th>
- <th></th>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
store procedure sp_tblEmployee_Select
- CREATE PROCEDURE
- sp_tblEmployee_Select
- AS BEGIN select * from tblEmployee
- 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??