Friday 30 November 2012

what is class

A class is userdefined datatype,

Friday 23 November 2012

sending email With multiple attachments in asp.net

sending email With multiple attachments:

<html>
<head runat="server">
    <title></title>

<script type="text/javascript">
   
       function valiedbody() {
     
           if (document.getElementById("<%=editor.ClientID%>").value == "") {                                       
                   
               if (confirm('Do you want to send with out body')) {
                   return true;
                }
               else {
                   document.getElementById("<%=editor.ClientID%>").focus();
                   return false;
               }            
               return false;
           }
           if (document.getElementById("<%=txtsubject.ClientID%>").value == "") {

               if (confirm('Do you want to send without subject')) {
                   return true;
               }
               else {
                   document.getElementById("<%=txtsubject.ClientID%>").focus();
                   return false;
               }
               return false;
           }      
           }         
</script>

</head>
<body>
    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
     <table >
            <tr>
        <td align="center">      
            <table cellpadding="0" cellspacing="0">        
            <tr>                             
                                <td >
                                <asp:TextBox ID ="txtName" runat ="server"></asp:TextBox>                               
                                </td>
            </tr>
            <tr>
                           
                                <td >
                                <asp:TextBox ID ="txtfrommail" runat ="server" ></asp:TextBox>                                                            
                                </td>
            </tr>
            <tr>                           
                                <td >
                                <asp:TextBox ID ="txttomail" runat ="server"></asp:TextBox>                        
                                 </td>
            </tr>               
            <tr>                           
                                <td >
                             
                                <asp:TextBox ID="txtpassword" runat="server" Enabled="false"  Width="350"
                                        TextMode="Password"></asp:TextBox>                            
                                </td>
            </tr>        
            <tr>                         
                                <td >
                                  <asp:TextBox ID ="txtsubject" runat ="server" Width="350px" Height="17"
                                        MaxLength="200" ></asp:TextBox>                                                                                                                                                  
                                </td>
            </tr>
            <tr>                           
                                <td class="Col_Right1">                                                                                                                                          
                                     <HTMLEditor:Editor runat="server" Id="editor" Height="100px"
                                                                                           AutoFocus="true" Width="100%" />                                                                                                  
                                </td>
            </tr>       
             <tr>
             <td >                          
              <asp:UpdatePanel ID="update1" runat="server" updatemode="Conditional">                                                                                                
             <ContentTemplate>            
                 Attachment                                                      
              <asp:FileUpload ID="FileUpload1" runat="server"/>                                                                                                                      
                                    <asp:Button ID="btnfileupload" runat="server" Text="Upload"
                                        onclick="btnfileupload_Click"/>                                    
                                        <br />
                                        <asp:Label ID="lblattchament1" runat="server" ></asp:Label><br />                                        
                                                                                                                                                 
             </ContentTemplate>          
                                <Triggers><asp:PostBackTrigger ControlID="btnfileupload" /></Triggers>                                                                                          
            </asp:UpdatePanel>
            </td>
           </tr>     
            <tr>                       
                                <asp:Button ID="btnsend" runat="server" Text="click to send mail"
                                        OnClientClick="return valiedbody()" onclick="btnsend_Click"/>                                                                                
                                </td>                            
              </tr>
              <tr><td colspan="2"><asp:Label ID="lblmessage" runat="server"></asp:Label></td></tr>                                      
                </table>
               </td>
               </tr>
               </table>

</form>
</body>
</html>


using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web;
using System.Net.NetworkInformation;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

public partial class Newsendmail : System.Web.UI.Page
{

 
    protected void Page_Load(object sender, EventArgs e)
    {
     
            if (!IsPostBack)
            {
                ViewState["Array"] = null;
             
                  //Delete Items with in folder          

                foreach (var f in System.IO.Directory.GetFiles(Server.MapPath("~/NewFolder1")))

                    System.IO.File.Delete(f);
            }
     
    }
    protected void btnsend_Click(object sender, EventArgs e)
    {
        string str = "";

        string pno = "";
        try
        {
            string frommail = txtfrommail.Text.ToString();

            string mystr = frommail.Substring(frommail.Length - 9);         

            if (mystr.ToString() == "gmail.com") //this is for gmail
            {
                str = "smtp.gmail.com";

                pno = "587";
            }
            else if (mystr.ToString() == "yahoo.com")// this is for yahoo mail
            {
                str = "smtp.mail.yahoo.co.in";

                pno = "587";
            }
         
            SmtpClient smtp = new SmtpClient();

            MailMessage mail = new MailMessage();

            MailAddress madd = new MailAddress(txtfrommail.Text);
                     
            mail.From = madd;

            mail.To.Add(txttomail.Text);

            mail.Subject = txtsubject.Text;

            mail.IsBodyHtml = true;

            mail.Body = editor.Content;

           string body1 = editor.Content;

           string strtext = Regex.Replace(body1, @"<(.|\n)*?>", String.Empty);      

            ArrayList arrValues = new ArrayList();

            string projName = lblattchament1.Text;

           List<string> languages = projName.Split(new char[] { ',' }).ToList();        

            string Body = body1;

           if (ViewState["Array"] != null)
            {            
                ArrayList myarrli = (ArrayList)ViewState["Array"];           
                for (int i = 0; i < myarrli.Count; i++)
                {
                    mail.Attachments.Add(new Attachment(Server.MapPath("~/NewFolder1/" + myarrli[ i])));             
                }                                 
            }        
            smtp.Host = str;

            smtp.Port = Convert.ToInt32(pno);

            smtp.EnableSsl = true;

            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;        
                 
              smtp.Credentials = new NetworkCredential(txtfrommail.Text, txtpassword.Text);
         
            smtp.Send(mail);

            mail.Dispose();

            lblmessage.Text = "Message successfully sent";

            ViewState["Array"] = null;
        }
        catch
        {
            lblmessage.Text = "Message sending fail";        
        }
        finally
        {
            txtName.Text = "";

            txtfrommail.Text = "";

            txttomail.Text = "";

            txtpassword.Enabled = false;

            lblattchament1.Text = "";

            txtsubject.Text = "";                    
        }
    }
 
    protected void btnfileupload_Click(object sender, EventArgs e)
    {
     
        if (FileUpload1.HasFile)
        {
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

            string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);

            string fileLocation = Server.MapPath("~/NewFolder1/" + fileName);

            FileUpload1.SaveAs(fileLocation);

            lblattchament1.Text += String.Format("{0},", FileUpload1.FileName);
               
            if (lblattchament1.Text != "")
            {
                ArrayList myarrli = new ArrayList();

                if (ViewState["Array"] != null)
                {
                    myarrli = (ArrayList)ViewState["Array"];
                }
                myarrli.Add(FileUpload1.FileName);

                ViewState["Array"] = myarrli;
            }        
        }
    }
}
 

Send Email with Attachment in asp.net

Sending Email With Attachments:

<script type="text/javascript">
       function valiedbody() {
           if (document.getElementById("<%=editor.ClientID%>").value == "") {
         
               if (confirm('Do you want to send with out body')) {
                   return true;
                }
               else {
                   document.getElementById("<%=editor.ClientID%>").focus();
                   return false;
               }            
               return false;
           }

           if (document.getElementById("<%=txtsubject.ClientID%>").value == "") {

               if (confirm('Do you want to send without subject')) {
                   return true;
               }
               else {
                   document.getElementById("<%=txtsubject.ClientID%>").focus();
                   return false;
               }
               return false;
           }      
         
       }
</script>
    <asp:ScriptManager ID="ScriptManager1" runat="server">      
    </asp:ScriptManager>

<table >
            <tr>
        <td align="center">
     
            <table cellpadding="0" cellspacing="0" >            
            <tr>
                                <td >
                                        Name
                                </td>
                                <td >
                                <asp:TextBox ID ="txtName" runat ="server"></asp:TextBox>
                               
                                </td>
            </tr>
            <tr>
                                <td >
                                        From Mail
                                </td>
                                <td >
                                <asp:TextBox ID ="txtfrommail" runat ="server" Width="350px" Height="17"
                                        MaxLength="50" ></asp:TextBox>                              
                               
                                </td>
            </tr>
            <tr>
                                <td >
                                        To Mail
                                </td>
                                <td >
                                <asp:TextBox ID ="txttomail" runat ="server"></asp:TextBox>                                    
                                 
                                </td>
            </tr>
            <tr>
                                <td >
                                        Password
                                </td>
                                <td >
                                <asp:TextBox ID ="txtpassword" runat ="server" Width="350px" Height="17" MaxLength="20" TextMode="Password"></asp:TextBox>                                    
                                 
                                </td>
            </tr>          
            <tr>
                                <td >
                                        Subject
                                </td>
                                <td >
                                  <asp:TextBox ID ="txtsubject" runat ="server" Width="350px" Height="17"
                                        MaxLength="200" ></asp:TextBox>                                                                                                            
                                </td>
            </tr>
            <tr>
                                <td >
                                        Body
                                </td>
                                <td>                                                                                                                                          
                                     <HTMLEditor:Editor runat="server" Id="editor" Height="100px"
                                                                                        AutoFocus="true" Width="100%" />
                                   
                                </td>
            </tr>
             <tr>
                                <td>
                                        Attachments
                                </td>
                                <td >                                    
                                    <asp:FileUpload ID="FileUpload1" runat="server"/>                                                  
                             
            </tr>
            <tr>
                                <td >
                                <asp:Button ID="btnsend" runat="server" Text="click to send mail"                   onclick="btnsend_Click" OnClientClick="return valiedbody()"/>
           
                                <asp:Button ID="btncancel" runat="server" Text="Cancel" />                                                    
                                </td>
              </tr>
              <tr><td colspan="2"><asp:Label ID="lblmessage" runat="server"></asp:Label></td></tr>                                      
                </table>
               </td>
               </tr>
               </table>

using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web;
using System.Net.NetworkInformation;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

public partial class SentMail : System.Web.UI.Page
{
    protected void Page_Load(object sender,EventArgs e)
    {
        if (!IsPostBack)
        {
         
        }
    }

    protected void btnsend_Click(object sender, EventArgs e)      
    {    

        string str = "";

        string pno = "";

        string frommail = txtfrommail.Text.ToString() ;

        string mystr = frommail.Substring(frommail.Length - 9);   

        if (mystr.ToString() == "gmail.com")
        {
            str = "smtp.gmail.com";

            pno = "587";
        }
        else if (mystr.ToString() == "yahoo.com")
        {
            str = "smtp.mail.yahoo.co.in";

            pno = "587";
        }

        SmtpClient smtp = new SmtpClient();

        MailMessage mail = new MailMessage();

        MailAddress madd = new MailAddress(txtfrommail.Text);

        mail.From = madd;

        mail.To.Add(txttomail.Text);

         if (FileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream,
                                                                                                 FileUpload1.FileName));
        }
         mail.Subject = txtsubject.Text;

        string body = editor.Content;

        string strtext = Regex.Replace(body, @"<(.|\n)*?>", String.Empty);    

       mail.Body =strtext.ToString();

        smtp.Host = str;

        smtp.Port =Convert.ToInt32(pno);    

        smtp.EnableSsl = true;

        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

        smtp.Credentials = new NetworkCredential(txtfrommail.Text, txtpassword.Text);    

        smtp.Send(mail);

        lblmessage.Text = "Message successfully sent";

        txtName.Text = "";

        txtfrommail.Text = "";

        txttomail.Text = "";

        txtsubject.Text = "";    
    }
}


Monday 19 November 2012

3-Tier Architecture using Asp.net

 long ago programmers used to write much lines of code and various logical functions and procedure to support reuse of their code. But in modern world you have to go beyond just re usability and mere programming logic. Today’s applications demands vast data support, User Interface Improvisation, data security and dynamic business needs. Here the 3-tier applications came into picture to solve developer’s problem. Separating the tiers in applications helps programmer to concentrate on very particular logic and build strong and dynamic programming architecture to support business applications. Theoretically many of you have read about concept and working of 3-tier architecture so will go directly to implementation of 3-tier architecture in .Net.

1. Data Access Layer:
The layer which is responsible for accessing the data from databases. You can write your connection string, various data accessing functions like Executing commands and filling of datasets.
2. Business Layer or Logic Layer:
This is layer where you can write the business logic such as calculation of Salary etc. The Layer consists of various properties and stored procedure accessing functions for selection, insertion, updation and deletion of data.
3. Presentation Layer or Application Layer:
The application layer takes care of your User Interface representation, data display and event handling.
3-Tier programming architecture also enables re-use of code and provides a better way for multiple developers to work on the same project simultaneously.
Advantages
  1. Easy to modify with out affecting other modules
  2. Fast communication
  3. Performance will be good in three tier architecture
Step1.         
  1. Start a new website project.
  2. After Creating website,Create Data base, after that Add three projects in the solution explore
solution explorer
        |-BusinessAccessClass
        |-DataAccessClass
        |-Default.aspx
        |-ObjectClass

    Create database:Table name:Student, columns are
                SNo int,
                Sname varchar,
                SAdd varchar.

 ->one project is Data Access Layer.It s name is  DataAClass.
 ->Second  Project is Business Access Layer.Its name is BusinessAClass.
 -> the thired Project is Object Class     using this Class declaring variables . Its name is ObjectClass.

Step2:
       After add projects,we must add Reference and namespaces.

       Goto solution Explorer
                   |
           Right Click
                    |
       Select AddReference
                    |
           Select Project
           And add reference.
         
       1.DataAClass-ObjectClass Reference.
       2.BusinessAClass-ObjectClass And DataAClass References.
      3.presentation Layer .aspx -BusinessAClass and ObjectClass reference.

Declaring Variables within Object Class. Show Bellow like this,these are all with in database table columns.

ObjClass.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObjectClass
{
    public class ObjClass
    {
        public string SNo
        {
            get;
            set;
        }     
        public string Sname
        {
            get;
            set;
        }
        public string Sadd
        {
            get;
            set;
        }
     }
}

DAClass.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ObjectClass;

namespace DataAClass
{
    public class DAClass
    {
        #region Private variables
        SqlConnection con = new SqlConnection(@"your connection string");
        ObjClass varObj = new ObjClass();
        #endregion

        public DAClass() //Empty constructor
        {
        }
        public DAClass(ObjClass newvarObj) //peramater constractor
        {
            varObj = newvarObj;
        }
        public DataTable LoadGridData()
        {
            SqlDataAdapter da = new SqlDataAdapter("select *  from Student", con);         
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public void insertdata()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into Student (SNo,Sname,Sadd) values
             (@SNo,@Sname,@Sadd)", con);         
          
            cmd.Parameters.AddWithValue("@SNo ", varObj. SNo);
            cmd.Parameters.AddWithValue("@Sname ", varObj.Sname)
           cmd.Parameters.AddWithValue("@Sadd ", varObj. Sadd);
           cmd.ExecuteNonQuery();
           con.Close();
        }
    }
}
BAClass.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DataAClass;
using ObjectClass;

namespace BusinessAClass
{
    public class BAClass
    {
        #region Private variables
        ObjClass varObj = new ObjClass();
        DAClass varDAC = new DAClass();
        #endregion

        public BAClass() //Empty constractor
        {

        }     
        public BAClass(ObjClass newvarObj) //parameter constractor
        {
            varObj = newvarObj;
            varDAC = new DAClass(varObj);
        }
        public DataTable LoadGridData ()
        {
            return varDAC. LoadGridData ();
        }
       public void insertdata()
        {
            varDAC.insertdata();
        }
    }
}

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
    <form id="form1" runat="server">
   Student No:   <asp:TextBox id=”txtSNo”,runat=”srever”></asp:TextBox>
  Student Name:<asp:TextBox id=”txtSname”,runat=”srever”></asp:TextBox>
  <asp:Button id=”btnsubmit” runat=”server” ></asp:Button>
  <asp:GridView ID="GridView1" runat="server” Width="193px"></asp:GridView>
    </form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using BusinessAClass;
using ObjectClass;

public partial class _Default : System.Web.UI.Page
{
    #region Private variables
      ObjClass varObj = new ObjClass();
      BAClass varBAC = new BAClass();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            form1_load();          
        }
    }
     private void form1_load()
    {
        DataTable dt = new DataTable();
        dt = varBAC.LoadGridData();      
        GridView1.DataSource = dt;
        GridView1.DataBind();   
    }  
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
      varObj.SNo = txtSNo.Text;
       varObj.Sname = txtSname.Text;
      VarObj.Sadd=txtSadd.Text;
       varBAC = new BAClass(varObj);
       varBAC.insertdata();
       ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('Record successfully    inserted');</script>", false);
        form_load();
    }  
}

Friday 16 November 2012

Web services Example

This article will explain you everything about Web Services in .Net, so lets get started with Web Service

What is Web Service?
  • Web Service is an application that is designed to interact directly with other applications over the internet. In simple sense, Web Services are means for interacting with objects over the Internet.
  • Web Service is
    • Language Independent
    • Protocol Independent
    • Platform Independent
    • It assumes a stateless service architecture.
  • We will discuss more on web service as the article proceed. Before that lets understand bit on how web service comes into picture.

Example of Web Service
  • Weather Reporting: You can use Weather Reporting web service to display weather information in your personal website.
  • Stock Quote: You can display latest update of Share market with Stock Quote on your web site.
  • News Headline: You can display latest news update by using News Headline Web Service in your website.
  • In summary you can any use any web service which is available to use. You can make your own web service and let others use it. Example you can make Free SMS Sending Service with footer with your companies advertisement, so whosoever use this service indirectly advertise your company... You can apply your ideas in N no. of ways to take advantage of it.

Web Service Communication
Web Services communicate by using standard web protocols and data formats, such as
  • HTTP
  • XML
  • SOAP



Web Services Communication Protocols 
Advantages of Web Service Communication
Web Service messages are formatted as XML, a standard way for communication between two incompatible system. And this message is sent via HTTP, so that they can reach to any machine on the internet without being blocked by firewall.

Terms which are frequently used with web services
  • What is SOAP?
    • SOAP are remote function calls that invokes method and execute them on Remote machine and translate the object communication into XML format. In short, SOAP are way by which method calls are translate into XML format and sent via HTTP.
  • What is WSDL?
    • WSDL stands for Web Service Description Language, a standard by which a web service can tell clients what messages it accepts and which results it will return.
    • WSDL contains every details regarding using web service
      • Method and Properties provided by web service
      • URLs from which those method can be accessed.
      • Data Types used.
      • Communication Protocol used.
  • What is UDDI?
    • UDDI allows you to find web services by connecting to a directory.
  • What is difference between Disco and UDDI?
    • Disco is Microsoft's Standard format for discovery documents which contains information about Web Services, while UDDI is a multi-vendor standard for discovery documents which contains information about Web Services.
  • What is Web Service Discovery Tool (disco.exe) ?
    • The Web Services Discovery Tool (disco.exe) can retrieve discovery information from a server that exposes a web service.
  • What is Proxy Class?
    • A proxy class is code that looks exactly like the class it meant to represent; however the proxy class doesn't contain any of the application logic. Instead, the proxy class contains marshalling and transport logic.
    • A proxy class object allows a client to access a web service as if it were a local COM object.
    • The Proxy must be on the computer that has the web application.
  • What is Web Service Description Language Tool (wsdl.exe)?
    • This tool can take a WSDL file and generate a corresponding proxy class that you can use to invoke the web service.
    • Alternate of generating Proxy class through WSDL.exe is you can use web reference. Web Reference automatically generate a proxy classes for a web service by setting a web reference to point to the web service.
    • Advantage of using Web Reference as compare to using WSDL.exe Tool is you can update changes done in web service class easily by updating web reference, which is more tedious task with WSDL.exe tool.
  • Testing a Web Service?
    • You can test web service without building an entire client application.
      • With Asp.net you can simply run the application and test the method by entering valid input paramters.
      • You can also use .Net Web Service Studio Tool comes from Microsoft.

Example of Creating Web Service in .Net
This Web Service will retrieve CustomerList Country Wise and return as dataset to client application for display.
Step1: Create a Web Service Application by File > New > Web Site > Asp.net Web Services
Named the web service, for example here i have choosen name "WSGetCustomerCountryWise"

Step2: Rename the default Service.asmx file to proper name, you also need to switch design view and change the class name with the same name you use to rename the service.asmx.
For example, "WSGetCustomerCountryWise.asmx" and switch to design view and change the class="Service" to class="WSGetCustomerCountryWise"

Step3: Rename the Service.CS File to proper name, you need to change the class name and constructor name too.
For example, "WSGetCustomerCountryWise.CS" and switch to code view and change the class and constructor name to "WSGetCustomerCountryWise"

After three steps your solution explorer looks as shown in figure





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

[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 Service : System.Web.Services.WebService
{
    public Service () {

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

    [WebMethod]
    public string HelloWorld(string message) {
        return "Hello World"+message;
    }

    [WebMethod]
    public int Add(int a,int b)
    {
        return a + b;
    }

    [WebMethod]
    public XmlElement GetUserDetails(string empname)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\WebSite\Webservices1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Emp where EmpName like @EmpName +'%'" , con);
        cmd.Parameters.AddWithValue("@EmpName",empname);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        // Create an instance of DataSet.
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        // Return the DataSet as an XmlElement.
        XmlDataDocument xmldata = new XmlDataDocument(ds);
        XmlElement xmlElement = xmldata.DocumentElement;
        return xmlElement;
    }
}



  Now  Build Web Service and Run the Web Service for testing by pressing F5 function key.






By pressing "Invoke" button will generate XML File.

So you are done creating web service application.

Example of Testing Web Service in .NetThis Web Service will display the information which had been retrieved from Remote computer by accessing public method "GetCustomerCountryWise".

 Create Web Site :

1. To create new Web Site project, choose New from File menu, then choose Web Site as shown below.



2. Choose ASP.NET Web Site. Name the project and click OK

Second Step: Add web Reference

After creating the Web Site project, it’s time to add a web reference for our web service.
1. In the solution explorer, right click the project node, choose Add Web Reference

2. A new window with Add Web Reference title will be opened.
In the URL field, insert the URL for the web service. In this tutorial as I mentioned before I’ll use the test published web services form Extentrix. “Extentrix Web Services 2.0 – Application Edition”
After clicking the Go button you will see the web services APIs.

3. Set a name for your web service reference in the web reference name field and click Add Reference



Aspx page:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblhello" runat="server"></asp:Label><br />
    sum is:<asp:Label ID="lbladd" runat="server"></asp:Label>   <br />
   
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            localhost.Service newobj = new localhost.Service();

            lblhello.Text = newobj.HelloWorld("My Name is siva");

            lbladd.Text = newobj.Add(5, 4).ToString();          

            BindUserDetails("");
        }
    }  
      
    private void BindUserDetails(string Empname)
    {
        localhost.Service objUserDetails = new localhost.Service();
        DataSet dsresult = new DataSet();
        XmlElement exelement = objUserDetails.GetUserDetails(Empname);
        if (exelement != null)
        {
            XmlNodeReader nodereader = new XmlNodeReader(exelement);
            dsresult.ReadXml(nodereader, XmlReadMode.Auto);
            GridView1.DataSource = dsresult;
            GridView1.DataBind();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        BindUserDetails(TextBox1.Text);
    }

}