How to insert and display the data from database using web service in asp.net application

By
In this tutorial ,i am going to show you,why most of good developers are used web services and WCF services in your asp.net application.There are some reasons behind it which are given below:-
  1. Website size is decreased
  2. Improve the performance of the application
  3. increased security
  4. Less overhead on on server because each (website,web service) are hosted on different server.it can be same server but different directories.
  5. Decrease website loading time because server is less overhead.
In this ,i have placed all connection related codes on web service file (myservice1.asmx) and passed all parameter values through website (mywebform.aspx) using Text box control.The main purpose to create a web service is placed maximum codes on web service (.asmx) file and less codes on website (mywebform.aspx). in previous tutorial i have already discussed more about web services.
  1. How to implement web service in asp.net application
  2. How to deploy web service by web setup project in visual studio 2010
There are some steps to implement this whole concepts which are given below:-
Step 1 :- First open your sql server database and create a  students 1 table with three column as shown below:-

table

Step 2 :- First open your visual studio 2010 -->File --> New -->website --> select ASP.NET Empty website--> OK-->open Solution Explorer --> Right click on project -->Add New Item--> Select web service and visual c# -->write your web service Name (myservice1.asmx)-->click Add as shown below:-


myservice

Step 3 :- Write the codes in myservice1.cs file as given below:-
using System;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Xml;
using System.Data;

/// <summary>
/// Summary description for myservice1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class myservice1 : System.Web.Services.WebService {

    public myservice1 () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string systemtime()
    {
        string time = DateTime.Today.ToLongDateString();
        return time;
    }
    [WebMethod]
    public void db_insert(string p ,int q ,string s)
    {
        SqlConnection con = new SqlConnection("data source=Ramashanker-PC;Integrated Security=Yes;Database=master");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into students1 values(@a,@b,@c)", con);
        cmd.Parameters.AddWithValue("a", p);
        cmd.Parameters.AddWithValue("b", q);
        cmd.Parameters.AddWithValue("c", s);
        int i = cmd.ExecuteNonQuery();
    }
    [WebMethod]
    public string Get_student()
    {
        SqlConnection con = new SqlConnection("data source=Ramashanker-PC;Integrated Security=Yes;Database=master");
        con.Open();
        SqlCommand cmd = new SqlCommand("select*from students1", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        string str = ds.GetXml();
        return str;
    }   
}

Step 4 :- Now open your web.config file and write the connection strings codes as shown below:-
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
 <system.web>
  <compilation debug="true" targetFramework="4.0"/>
 </system.web>
 <connectionStrings>
  <add name="con" connectionString="Data source=Ramashanker-PC;Integrated Security=Yes;Database=master"/>
 </connectionStrings>
</configuration>


Note:-If you are facing connection related problems read below links:-
  1. How to solve sql server problem in .NET
  2. How to use different connection strings in ado.net

Step 5 :-Now Run the myservice1.asmx (press F5) -->Copy the browser URL so that easily to configure on website as shown below:-



Step 6 :- Now click Get_student for test purpose as shown in above page -->Invoke -->You will see following output .It shows your connection is correct.


xml

Note:- You can test other methods such as db_insert , systemtime also.

Step 7 :-Now again open your visual studio-->File --> New -->website --> select ASP.NET Empty website--> OK-->open Solution Explorer --> Add a web Form (mywebform.aspx )-->drag and drop Label,Text Box ,Gridview and Button control as shown below:-
web form

Step 8 :- Now open solution Explorer --> Add web reference -->Paste the URL which had copied in step 5 (OR Run myservice1.asmx file again) --> Next Arrow as shown below:-


url



Step 9 :- Now Enter web reference name -->click Add reference button as shown below:-



Step 10 :- Now write the codes on page_Load,insert button and display Data button   handler in mywebform.aspx file as shown below:- 
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using studentdetails;

public partial class mywebform : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        myservice1 servicetime = new myservice1();
        string t = servicetime.systemtime();
        Label1.Text = t;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        myservice1 myform = new myservice1();
        myform.db_insert((txtname.Text).ToString(), Convert.ToInt32(textage.Text), (txtloc.Text).ToString());
        Label2.Text = "Data inserted successfully";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        myservice1 myserv = new myservice1();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(myserv.Get_student());
        DataSet ds = new DataSet();
        XmlNodeReader xmlreader = new XmlNodeReader(xmlDoc);
        ds.ReadXml(xmlreader, XmlReadMode.Auto);
        GridView1.DataSource = ds;
        GridView1.DataBind();      
    }
}

Note:- Includes all Namespace which are shown in above codes.
Description:-
  1. Here i have include Using studentdetails; because my web service Name = studentdetails.
  2. Creeate the object of myservice1 because it is my web service Name.
Step 11 :- Now Run the Application (Press  F5)-->Press Insert and Display Data button as shown below:


web_output

For More...
  1. How to add captcha image in asp.net application
  2. Navigation controls in asp.net
  3. File and File info class in c#
  4. How to host aSp.net website on IIS Server
  5. Delegate in C#
  6. Stored procedure and Function in c#
  7. Namespace in c#
  8. Call by value and call by reference in c#
  9. Abstract and Abstract method in c#
  10. Constructor and Descructor in c#
  11. How to insert and write the xml file
  12. Microsoft SQL Server
Download all attached file
        Download                

4 comments:

  1. Really nice bro,
    i need know how to retrieve image from db and shown in grid view ,,Help me please..

    ReplyDelete
  2. this tutorial is very useful to insert and display the data from database using web service

    ReplyDelete
  3. thank u it was very helpful

    ReplyDelete

Powered by Blogger.