Iterate through the table and calculate the total price through storeprocedure

create proc test
as
Select price_id Into  #Temp From   agreement_price
Declare @Id int
declare @val int
Declare @calc int
declare @it int

set @val=0
set @it=(Select Count(*) From #Temp)
While (@it) > 0
Begin
  set @calc=(select price from price where price_id in (Select Top 1 price_id From  #Temp))
  set @val=@val+@calc
set @id=(Select Top 1 price_id From  #Temp)
 Delete from #Temp Where price_id= @Id
set @it=@it-1
End
select @val
exec test

WCF

http://www.codeproject.com/KB/WCF/WCF_Example.aspx


Abstract

In this article, I am going to show you a small Addition programme using WCF. After going through this snippet, the reader will get a clear and basic understanding of the WCF programme.

Introduction

WCF (Windows Communication Framework) is a unification technology, which unites the following technologies:
  • NET remoting
  • MSMQ
  • Web services
  • COM+
It is based on SOA (Service Oriented Architecture).

What are Ends, Contract, Address, and Bindings?

The above terminologies are the core on which SOA stands. Every service must expose one or more ends by which the service can be available to the client. End consists of three important things: where, what and how:

Contract (What)

Contract is an agreement between two or more parties. It defines the protocol how the client should communicate with your service. Technically, it describes parameters and return values for a method.

Address (Where)

An address indicates where we can find this service. Address is a URL, which points to the location of the service.

Binding (How)

Bindings determine how this end can be accessed. It determines how communications are done. For instance, you expose your service, which can be accessed using SOAP over HTTP or BINARY over TCP. So for each of these communications medium two bindings will be created.

The Important Principles of SOA?

WCF is based on SOA. SOA is based on four important concepts:

Boundaries are Well Defined

In SOA, everything is formalized. The client who is consuming the service does not need to know how the implementation of the service is done. If you look at some old methodologies of communication like DCOM, any changes at server level the client also has to change. Therefore, the server and client implementation was so much bound that changes need to be done at all places. In SOA, the rule is if you do enhancement you do not need to change anything at the client. SOA based application only understands that there is an end point, contract, and bindings.
Note: Just to clarify shortly about end point and contract, any SOA service is exposed through an end point. End point defines three important aspects What, Where and How.

Services Evolve

Change is the law of nature and services will evolve. In SOA, services can be versioned and you can host those services in new ends. For instance, you have a service called as “Search Tickets (Ticket Number)“ which gives details based on Ticket Number and it's exposed on end point “ep1”. Tomorrow you want make your Search Tickets service more useful by also providing an extra option of allowing him to search by passenger name. Therefore, you just declare a new end “ep2” with service “Search Tickets (Ticket Number, Passenger Name)”. So the client who is consuming the service at end ep1 continues and at the other end, we have evolved our service by adding new ends ep2.

Services Share Only Schemas and Contracts

Services use Schemas to represent data and contracts to understand behavior. They do not use language dependent types or classes in order to understand data and behavior. XML is used to define schemas and contracts. Due to this, there is no heavy coupling between environments.

Service Compatibility is Policy Based

Policy describes the capabilities of the system. Depending on policies, the service can degrade to match the service for the client. For instance, your service needs to be hosted for two types of clients, one which uses Remoting as the communication methodology while the other client uses DCOM. An ideal SOA service can cater to both of them according to their communication policies.

Code Snippet

(Service/Server Side)
Steps Involved:
Step 1: Open VS2008 , File->New -> Website->WCF Service->Browse->Click or select default Website ->Right to corner Create New Web Application->Rename to" MyService" ->Open->OK 
Step 2: You will see the solution window, in that open "service.cs".



Step 3: Open "service.svc.cs" and write code here as I have written code for my "addfunction". You can write your code inside your function. The picture is shown below:


Step 4: This is the most important step. In this, we are declaring the end point. Inside this, we can define an end point as shown in the picture. End point is defined automatically. We can also define it by program.

Step 5: Save the project and run it. This will display like this. Copy the address from explorer address bar.

Code Snippet

(Client Side)
Let the server service run (the above page).
Step 1: Open VS2008 , File->New -> Website->Asp .NET Website->Browse->Click or select default Website ->Right to corner Create New Web Application->Rename to" WCF Client" ->Open->OK   Right click on solution name and then go to "Add Service Reference" one window will open paste the previously copied link to the address bar and press GO. Then the service will appear in service section, then press OK. The service reference will appear in solution explorer as below:

Step 2: Now add one button on the default page, then double click on button you'll be in code behind section. Here create the object of the service which appeared in the solution explorer. Something like this:
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceClient cls = 
 new ServiceReference1.ServiceClient();
cls.Open();
Response.Write( cls.GetData(5));
cls.Close();
}
Step 3: Now save the project and run it. If everything goes fine, then the output will be like this:

Hope this article may helped you to understand and build a simple addition program in WCF.
Thanks.

Web service in asp.net

http://msdn.microsoft.com/en-us/library/8wbhsy70.aspx#Y1444

To create a Web service


  1. Open Visual Web Developer.
  2. On the File menu, click New Web Site.
    The New Web Site dialog box appears.
  3. Under Visual Studio installed templates, click ASP.NET Web Service.
  4. Click Browse.
  5. Click Local IIS.
  6. Click Default Web Site.
  7. Click Create New Web Application.
    Visual Web Developer creates a new IIS Web application.
  8. Type the name TemperatureWebService.
  9. Click Open.
    The New Web Site dialog box appears, with the name of the new Web site in the rightmost Location list. The location includes the protocol (http://) and location (localhost). This indicates that you are working with a local IIS Web site.
  10. In the Language list, click the programming language that you prefer to work in.
    The programming language that you choose will be the default for the Web site. However, you can use more than one language in the same Web application by creating pages and components in different programming languages. For more information about how to create components using different languages, see Shared Code Folders in ASP.NET Web Projects.
  11. Click OK.
    Visual Web Developer creates the new Web service and opens a new class named Service, which is the default Web service. However, in the following procedure you will create a new Web service with a specified name and you will not use the Service class.
  12. Close the Service class.

    Creating Webservices

    You will create a Web service that converts temperature from Fahrenheit to Celsius and vice versa.

    To create the Web service

    1. In Solution Explorer, right-click the Web site name (http://localhost/TemperatureWebService), and then click Add New Item.
    2. Under Visual Studio installed templates, click Web Service, and then in the Name box, type Convert.
    3. Make sure that the Place code in separate file check box is selected, and then click Add.
      Visual Web Developer creates a new Web service that is made up of two files. The Convert.asmx file is the file that can be invoked to call Web service methods, and it points to the code for the Web service. The code itself is in a class file (Convert.vb, Convert.cs, or Convert.jsl, depending on the programming language) in the App_Code folder. The code file contains a template for a Web service. The code file includes some code for a Web service method.
    You will create two methods in the Web service. The first method converts Fahrenheit temperatures to Celsius, and the second method converts Celsius temperatures to Fahrenheit.

    To create the conversion methods

    Add the following code inside the class, after the HelloWorld method:
[System.Web.Services.WebMethod()]
public double FahrenheitToCelsius(double Fahrenheit)
{
    return ((Fahrenheit - 32) * 5) / 9;
}

[System.Web.Services.WebMethod()]
public double CelsiusToFahrenheit(double Celsius)
{
    return ((Celsius * 9) / 5) + 32;
}
  1. Notice that the function names are preceded with an attribute ([System.Web.Services.WebMethod()] or <System.Web.Services.WebMethod()>) as part of the function declaration.
  2. After you have entered the functions, save the file.
Now, you can test the Web service in Visual Web Developer.

To test the Web service

  1. In Solution Explorer, click Convert.asmx, and then press CTRL+F5.
    The Web service is invoked and a page appears in the browser that shows the methods that are exposed by the Web service.
  2. Click CelsiusToFahrenheit, which invokes that method.
    A page appears that prompts you for parameter values for the CelsiusToFahrenheit method.
  3. In the Celsius box, type 100, and then click Invoke.
    A new window appears that displays the XML that the Web service returns when the CelsiusToFahrenheit method is invoked. The value 212 appears in the XML.
  4. Close the browser that contains the method results.
  5. In the original browser, click Back to return to the list of methods.
  6. Click FahrenheitToCelsius and test to make sure that the method is returning the results that you expect.
    If you type 212, the FahrenheitToCelsius method will return 100.
  7. Close the browser.
You have finished creating the Web service; the next step is to use it.

Using the webservices


Now that you have a Web service, you will create a Web site where you will reference and use the Web service that you created. For the walkthrough, you will create a separate Web site that has a page where you start the Web service methods that you just created.

To create a Web site to use the Web service

  1. On the File menu, click New Web Site.
  2. Under Visual Studio installed templates, click ASP.NET Web Site.
  3. Click Browse.
  4. Click Local IIS.
  5. Click Default Web Site.
  6. Click Create New Web Application.
    Visual Web Developer creates a new IIS Web application.
  7. Type the name TemperatureWeb.
  8. Click Open.
  9. In the Language list, click the programming language that you prefer to work in.
  10. Click OK.
    Visual Web Developer creates a new local IIS Web site and a new page named Default.aspx.
  Adding the Web Service as a Component

To create a reference to the Web service

  1. On the Web Site menu, click Add Web Reference.
    The Add Web Reference dialog box appears, as shown in the following screen shot.
    Add Web Reference dialog box

    Add Web Reference dialog box
  2. In the URL list, enter the following URL for the Web service, and then click Go:
    http://localhost/TemperatureWebService/Convert.asmx
    When Visual Web Developer finds the Web service, information about the Web service appears in the Add Web References dialog box.

    Note
     
    If you cannot add a reference to a Web service, it might be that the proxy server is not configured correctly. In Microsoft Internet Explorer, on the Tools menu, click Internet Options, click Connections, and then click LAN Settings. Select the Bypass proxy server for local addresses check box. Additionally, set the proxy server address to the exact name of the proxy server instead of allowing Internet Explorer to detect the proxy server. For more information, contact the network administrator.

  3. Click one of the method links.
    The test page for the method appears.
  4. Click Add Reference.
    Visual Web Developer creates an App_WebReferences folder and adds a folder to it for the new Web reference. By default, Web references are assigned a namespace corresponding to their server name (in this case, localhost). Make a note of the name for the Web reference namespace. In the folder, Visual Web Developer adds a .wsdl file that references the Web service. It also adds supporting files, such as discovery (.disco and .discomap) files, that include information about where the Web service is located.
Note Note
 
If the server name for the Web service contains characters that cannot be used for a class name, such as a hyphen (-), Visual Web Developer converts those characters to an underscore character (_). Therefore, the namespace in Visual Web Developer for the Web service might not match the server name exactly.
You can now use the Web service. In this walkthrough, you will add controls to Default.aspx, and then program the controls to convert a specified temperature to both Fahrenheit and Celsius. When the page is running, it will look something like the following illustration.


Temperature conversion page

VenusWebServicesWalkthroughConsumePage screenshot

To call the Web service methods

  1. Open the Default.aspx page and switch to Design view.
  2. From the Standard group in the Toolbox, drag the following controls onto the page and set their properties as indicated:
    Control
    Properties
    Textbox
    ID: TemperatureTextbox
    Text: (empty)
    Button
    ID: ConvertButton
    Text: Convert
    Label
    ID: FahrenheitLabel
    Text: (empty)
    Label
    ID: CelsiusLabel
    Text: (empty)
  3. Optionally, add text to the page for captions.
    For this walkthrough, the layout of the page is not important.
  4. Double-click ConvertButton to create an event handler for its Click event.
  5. Make sure your event handler code matches the code in the following example.
    protected void ConvertButton_Click(object sender, EventArgs e) { localhost.Convert wsConvert = new localhost.Convert(); double temperature = System.Convert.ToDouble(TemperatureTextbox.Text); FahrenheitLabel.Text = "Fahrenheit To Celsius = " + wsConvert.FahrenheitToCelsius(temperature).ToString(); CelsiusLabel.Text = "Celsius To Fahrenheit = " + wsConvert.CelsiusToFahrenheit(temperature).ToString(); }
  6. Press CTRL+F5 to run the page.
  7. In the text box, type a value, such as 100, and then click Convert.
    The page displays the result of converting the temperature value into both Fahrenheit and Celsius.
You can debug a Web service in the same way that you debug Web pages.

Note Note
 
Visual Web Developer Express and Visual Studio Standard do not support stepping into a Web service from a page that references it. If you are using Visual Web Developer Express or Visual Studio Standard, skip this section and the ones following. For more information about how to debug Web sites, see Walkthrough: Debugging Web Pages in Visual Web Developer.
To start, you must configure the Web site that contains the Web service to enable debugging.

To enable debugging in the Web services Web site

  1. On the File menu, click Open Web Site.
  2. Click Local IIS.
  3. Click TemperatureWebService, and then click Open.
  4. On the Website menu, click ASP.NET Configuration to open the Web Site Administration Tool.

    Note Note
     
    If this is the first time that you have run the Web Site Administration Tool, there might be some delay before it appears.
  5. Click Application, and then click Application Configuration.
  6. Under Debugging and Tracing, click Configure debugging and tracing.
  7. Select the Enable debugging check box.
    The Web Site Administration Tool creates a Web.config file for the Web site and sets a configuration option to enable debugging.

    Note Note
     
    To see the Web.config file in Solution Explorer, click the Web site name and then, on the Solution Explorer toolbar, click Refresh.
  8. Close the Web Site Administration Tool.
You must now enable debugging for the Web site that uses the Web service.

To enable debugging in the Web site

  1. Open the TemperatureWeb site.
  2. On the Website menu, click ASP.NET Configuration to open the Web Site Administration Tool.
  3. Click Application, click Application Configuration, under Debugging and Tracing, click Configure debugging and tracing, and then select the Enable debugging check box.
  4. Close the Web Site Administration Tool.

    Note Note
     
    To see the Web.config file in Solution Explorer, select the Web site name and then, on the Solution Explorer toolbar, click Refresh.
  5. In Solution Explorer, right-click Default.aspx, and then click View Code.
    Visual Web Developer opens the code file for the page.
  6. Position the pointer in the following line:
    double temperature =
        System.Convert.ToDouble(TemperatureTextbox.Text);
    
    
    
  7. Press F9 to set a breakpoint on the line.


Both the Web site and the Web service are configured for debugging, so that you can now try debugging. You will start in the Default.aspx page and step through the code until the code invokes the Web service. The debugger will switch to the Web service and continue stepping through the code.

To debug the page and Web service

  1. Press F5 to run the Default.aspx page with debugging.
    The page appears in the browser.
  2. In the box, type a value, such as 100, and then click Convert.
    Visual Web Developer starts running the code for the page, but stops and highlights the line with the breakpoint on it.
  3. Press F11 to step to the next line.
  4. Press F11 again.
    Because the next line invokes the Web service, the debugger steps into the Web service, stopping on the first line of the FahrenheitToCelsius method.
  5. Continue pressing F11.
    The debugger steps through the rest of the method, and then returns to the calling page. If you continue stepping, the debugger will step back into the Web service and into the CelsiusToFahrenheit method.
  6. Close the browser, which also closes the debugger.

Xml operation

****************Creating new xml file************************

XmlTextWriter newXmlFile = new   XmlTextWriter(Give path and name of xml file to create,System.Text.Encoding.Default);

newXmlFile.WriteStartDocument();

//creating the name of the main node
newXmlFile.WriteStartElement("Start","");

//creating child nodes
newXmlFile.WriteElementString("First");
newXmlFile.WriteFullEndElement();
       newXmlFile.WriteEndElement();
newXmlFile.Flush();
newXmlFile.Close();

************************Insert data to xml file************************

// Create xml dom
XmlDocument XMLDom = new XmlDocument();

//load your xml file
XMLDom.Load(@"path of your xml file");

//Select main node
XmlNode newXMLNode = XMLDom.SelectSingleNode("/Main node name");

//get the node where you want to insert the data
XmlNode childNode = XMLDom.CreateNode(XmlNodeType.Element,"your node name where you want to insert data","");

//In the below step "name" is your node name and "sree" is your data to insert
XmlAttribute newAttribute = XMLDom.CreateAttribute("name","sree","");
childNode.Attributes.Append(newAttribute);
newXMLNode.AppendChild(childNode);

************************Modify data in xml file************************
// Select childnode where you want to modify data. The following step "PNode" is parent node and "Node1" is the one which you want to update the data

XmlNodeList newXMLNodes = XMLDom.SelectNodes("/PNode/Node1");
foreach(XmlNode newXMLNode in newXMLNodes)

//Updating data where "Node1" is "sree" with "kambham"
if(newXMLNode.InnerText == "sree")
newXMLNode.InnerText = "kambham";
XMLDom.Save("Path of your XML file");
XMLDom = null;
//The above step updates xml file where ever it finds "sree" with "kambham"

************************Delete data in xml file************************

XmlNodeList newXMLNodes = XMLDom.SelectNodes("/PNode/Node1");
foreach(XmlNode newXMLNode in newXMLNodes)
if(newXMLNode.InnerText == "sree")
newXMLNode.ParentNode.RemoveChild(newXMLNode);
XMLDom.Save("Path of your XML file");
XMLDom = null;
//The above step deletes data in xml file where ever it finds "sree"