Insert/Read record(s) to/from Database Through jquery in asp.net by calling webservice (.asmx) method
Here is the output
Webservice method can be called via jquery $.ajax({}) if the underlying webservice is marked with Attribute
[System.Web.Script.Services.ScriptService]
and the underlying method should be marked as
[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
In this article we will learn how to call webservice method from asp.net application to insert data to database i will consider a database named "test" and the table name is "Employee(EmployeeID, Name, NationalIDNo, PhoneNo)". I want to pass the Employee as an object. So we will require to code a separate class from employee. Add the Employee.cs to your project. this class should include all the columns in the employee table with datatypes. This class should look like this.
1 | using System; |
and also we require to get the employee records from the database. for this in this article i am using the class GlobalData.cs (you can use any logic to retrieve data from the database. this class looks like this. but before this class you should add connection string to web.config
1 | <connectionstrings> |
1 | using System; |
Next we require to add webservice to our web application right. To Add webservice to your application in solution explorer right click on your project and select add new item. then Select [Web Service] with (.asmx) extension. this web service should look like this. web service name should be (InvoiceService.asmx) in this case.
1 | using System; |
For this line of Code.
IList lstToReturn = db.SelectRecords("Select * from Employee").ToList();
To convert DataTable to list we require to add extension method to DataTable class. To Add extension methods right click on your project and select add Class named (Extension) should look like this.
IList
To convert DataTable to list we require to add extension method to DataTable class. To Add extension methods right click on your project and select add Class named (Extension) should look like this.
1 | using System; |
Note. your master page. should include reference to jquery.js & bootstrap.js & bootstrap.css
Finally your page.aspx should look like this. html & javascript
Finally your page.aspx should look like this. html & javascript
1 | <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="frmEmployee.aspx.cs" Inherits="frmEmployee" %> |
Here is the output
2 Comments
Dear Sir.Zafar ali khan, i am really happy being a part and member of your blog , learnt a lot of things , i would like to add something if possible at your side.
ReplyDeleteplease give us the solution for multiple selection in dropdown list and its insertion in Database.
looking forward for your positive response .
Thanks
Abdul Baseer
Thanks. Abdul Baseer sb thank you for your support (in sha Allah) i will publish an article soon.
DeleteOr you can see this MVC article
http://asptipsandtricks.blogspot.com/2016/10/checkboxlist-in-mvc.html
Post a Comment