<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DU8NRHk-eSp7ImA9Wx5SEUs.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191</id><updated>2010-08-07T13:28:15.751+05:30</updated><title>ASP.NET, C# and .NET</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://sandblogaspnet.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>45</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/AspnetCAndnet" /><feedburner:info uri="aspnetcandnet" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;C0IHR3o9fip7ImA9WxFRFEs.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-703444565036044160</id><published>2010-04-20T20:20:00.001+05:30</published><updated>2010-04-28T19:02:16.466+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-28T19:02:16.466+05:30</app:edited><title>Calling web service using javascript. Part - 2</title><content type="html">&lt;p align="justify"&gt;In the first &lt;a href="http://sandblogaspnet.blogspot.com/2010/03/calling-web-service-using-javascript.html" target="_blank"&gt;part&lt;/a&gt; of the blog we saw how to invoke web methods using javascript. In this blog we will take a look at how to invoke web service methods using Webservice behavior (HTC file). WebService behavior is a component which helps in invoking web service methods using SOAP protocol. The HTC file can be freely downloaded from Microsoft’ &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=1A3F25CA-0387-49A9-8EDC-B8BC0309A2E1&amp;amp;displaylang=en" target="_blank"&gt;site&lt;/a&gt;. By default HTC file support asynchronous invocation of web service methods. One can enable synchronous way of invocation as well, but do keep in mind that synchronous invocation can freeze the screen (browser) till the web method execution is completed, which may not be desired in web application.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Making use of WebService behavior (HTC file)&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;To make use of WebService behavior to invoke web service methods we need to download webservice.htc file from &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=1A3F25CA-0387-49A9-8EDC-B8BC0309A2E1&amp;amp;displaylang=en" target="_blank"&gt;here&lt;/a&gt;. Once you have downloaded the HTC file save it in any folder in your website. Once the “webservice.htc” file has been downloaded and saved in your application the next thing is to hook up the “htc” file to any of the html control using the style attribute. Once you have attached the “htc” file to a control then you can use the various methods in the “htc” file to invoke web service methods. Sample javascript code and web service is pasted below. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;body&amp;gt;            &lt;br /&gt;&amp;lt;script language=&amp;quot;javascript&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;             &lt;br /&gt;function callWebService()             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var webSer = document.getElementById(&amp;quot;webServiceHTC&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; webSer.useService(“&lt;a href="http://localhost:3150/TestService.asmx?wsdl&amp;rdquo;"&gt;http://localhost:3150/TestService.asmx?wsdl”&lt;/a&gt;, &amp;quot;ServiceTest&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var uniqueID = webSer.ServiceTest.callService(handleResult, &amp;quot;HelloWorld&amp;quot;);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}             &lt;br /&gt;function handleResult(result)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; alert(result.value);             &lt;br /&gt;}             &lt;br /&gt;&amp;lt;/script&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;input type=”text” id=&amp;quot;webServiceHTC&amp;quot; style=&amp;quot;behavior:url(htc/webservice.htc)&amp;quot;/&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;javascripWSInvoke&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Invoke&amp;quot; OnClientClick=&amp;quot;javascript:callWebService(); return false;&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;/form&amp;gt;             &lt;br /&gt;&amp;lt;/body&amp;gt; &lt;/p&gt;          &lt;p&gt;//Web service method &lt;/p&gt;          &lt;p&gt;[WebMethod]           &lt;br /&gt;public string HelloWorld()            &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;Hello World&amp;quot;;            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the code pasted above, we are attaching the “webservice.htc” file using the html “style” property of an html control. Once the “webservice.htc” file is attached to a html control you can access the “htc” file by retrieving the control using the “getElementById” javascript method. As you can see, from the above code, we are retrieving the textbox control using the “getElementById” method and we are making use of the “useService” method defined in the “webservice.htc” file. Using the “useService” method one can register a web service and give it a friendly name. The method takes two arguments, first is the url of the web service and the second is the friendly name. The friendly name is then used along with the control to invoke web service methods, as pasted in the above code, where we are using the friendly name along with “callService” method to invoke web method. “callService” method has optional first argument which is the name of the javascript function which will handle the result returned from the web method, second argument is the name of the web method which needs to be invoked and then list of parameters separated by comma. &lt;/p&gt;  &lt;p align="justify"&gt;“handleResult” method handles the result returned from the web service method. The method takes an argument which will hold the value returned from the web service method. In the “handleResult” we are showing the output in a alert message. The result argument will have four properties namely “error”, “id”, “raw” and “value”. “error” is a boolean value which indicates whether there was any error, “id” holds the unique id returned while invoking the web method, “raw” will have the raw XML and “value” will have the actual value returned from the web service method. “value” property can have simple as well as complex objects. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Passing parameters to a web method.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;The above sample was a simple web method invocation without parameters. Now, lets see how to pass parameters to a web method.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;function passParameter()            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Getting the htc file attached object.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var txtCntl = document.getElementById(&amp;quot;divHTC&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Assigning the web service url.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; txtCntl.useService(&amp;quot;&lt;a href="http://localhost:3150/TestService.asmx?wsdl&amp;quot;"&gt;http://localhost:3150/TestService.asmx?wsdl&amp;quot;&lt;/a&gt;, &amp;quot;webServ&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Variables to pass as arguments to the web method.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var fName = &amp;quot;Sandeep&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var lName = &amp;quot;P R&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var mName = &amp;quot;&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Invoking the &amp;quot;CombineName&amp;quot; web method.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var uniqueID = txtCntl.webServ.callService(handleResult, &amp;quot;CombineName&amp;quot;, fName, mName, lName);             &lt;br /&gt;} &lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above code is pretty much the same as the one we have previously used to invoke a web method. The only difference here is that we are passing arguments as a comma separated list to “callService” method. As explained before the first two arguments of “callService” method take the name of the javascript function which handles the response and second argument is the name of the web method to be invoked. The next set of arguments starting from the third arguments are the parameters which needs to be passed to the web method. You can pass any number of arguments separated by comma. Once the web method returns the output the control is passed to “handleResult” javascript method which does nothing other than elegantly showing output in a alert box. &lt;/p&gt;  &lt;p align="justify"&gt;Lets see the code for the web method invoked in the above javascript method.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;[WebMethod]            &lt;br /&gt;public string CombineName(string firstName, string midleName, string lastName)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return string.Concat(string.Concat(firstName, &amp;quot; - &amp;quot;, midleName), &amp;quot; - &amp;quot;, lastName);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above web method is pretty straight forward. It concatenates all the arguments passed into it and returns it as a single string. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Sending complex objects to a web method&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;We have invoked web method without parameter, with parameters and next is what? Obviously, passing complex objects to a web service method. It is very much possible to pass complex objects to a web method using web service behavior. Below seeing the javascript code in action lets see the code for the web service method and complex object which forms the method argument.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Web service method which takes complex object &amp;quot;Car&amp;quot; as an argument.            &lt;br /&gt;[WebMethod]             &lt;br /&gt;public string GetCar(Car car)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;Model: &amp;quot; + car.Model + &amp;quot;, Cubic capacity: &amp;quot; + car.CC + &amp;quot;, Color: &amp;quot; + car.Color;             &lt;br /&gt;} &lt;/p&gt;          &lt;p&gt;//The &amp;quot;Car&amp;quot; class.            &lt;br /&gt;public class Car             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Model             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public int CC            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public string Color            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above web method is pretty straight forward, it takes a complex object (Car) as one of its argument and concatenates all the properties of the “Car” class and returns it as a string. Now lets see how to pass the complex “Car” object to the web method using javascript and webservice behavior.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Javascript method which sends complex &amp;quot;Car&amp;quot; object as an argument to web service method.            &lt;br /&gt;function sendComplexObject()             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var ccar = new Object();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ccar.Model = &amp;quot;Ferrari&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ccar.CC = 2345;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ccar.Color = &amp;quot;Ferrari Red&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ccar.Blah = &amp;quot;dlkjs&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var txtCntl = document.getElementById(&amp;quot;divHTC&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; txtCntl.useService(&amp;quot;&lt;a href="http://localhost:3150/TestService.asmx?wsdl&amp;quot;"&gt;http://localhost:3150/TestService.asmx?wsdl&amp;quot;&lt;/a&gt;, &amp;quot;webServ&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var uniqueID = txtCntl.webServ.callService(handleResult, &amp;quot;GetCar&amp;quot;, ccar);             &lt;br /&gt;}             &lt;br /&gt;//Method which handles the response.             &lt;br /&gt;function handleResult(result)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; alert(result.value);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above “sendComplexObject” javascript function we are first creating a javascript object of type “Object”. Once the object of type “Object” is created, we are dynamically adding properties to the object. One thing to note here is that the dynamically added properties should have the same name as that of the server side object properties and it is case sensitive. If you closely notice the above code we have added an extra property called “Blah” to the “Car” object. Now, you may be thinking how is this object of “Object” type getting converted to a strongly typed .NET object. When the web method is invoked using “callService” method, the HTC file has got the necessary code to read the schema and infer the type of argument the method takes and converts the properties into a XML and sends to the web service method. In the web service side the .NET runtime takes care of serializing the XML to respective .NET object. We have added an additional “Blah” property to the object which during serialization is not converted as there is no corresponding server side property.&lt;/p&gt;  &lt;p align="justify"&gt;Note: If you have any value type in your complex object (like “CC” property in the “Car” object) then make sure that you assign them a value (default) even if you are not going to use it in the server side, else while invoking the web method the server can throw “System.FormatException”. The details of the exception which was thrown while not assigning the “CC” property of the “Car” object is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&lt;font color="#ff0000"&gt;System.Web.Services.Protocols.SoapException: Server was unable to read request. ---&amp;gt; System.InvalidOperationException: There is an error in XML document (7, 22). ---&amp;gt; System.FormatException: Input string was not in a correct format.              &lt;br /&gt;&amp;#160;&amp;#160; at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&amp;amp; number, NumberFormatInfo info, Boolean parseDecimal)               &lt;br /&gt;&amp;#160;&amp;#160; at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)               &lt;br /&gt;&amp;#160;&amp;#160; at System.Xml.XmlConvert.ToInt32(String s)               &lt;br /&gt;&amp;#160;&amp;#160; at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read3_Car(Boolean isNullable, Boolean checkType)               &lt;br /&gt;&amp;#160;&amp;#160; at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read8_GetCar()               &lt;br /&gt;&amp;#160;&amp;#160; at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer8.Deserialize(XmlSerializationReader reader)               &lt;br /&gt;&amp;#160;&amp;#160; at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)               &lt;br /&gt;&amp;#160;&amp;#160; --- End of inner exception stack trace ---               &lt;br /&gt;&amp;#160;&amp;#160; at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)               &lt;br /&gt;&amp;#160;&amp;#160; at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)               &lt;br /&gt;&amp;#160;&amp;#160; at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()               &lt;br /&gt;&amp;#160;&amp;#160; --- End of inner exception stack trace ---               &lt;br /&gt;&amp;#160;&amp;#160; at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()               &lt;br /&gt;&amp;#160;&amp;#160; at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()&lt;/font&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;How to handle complex objects returned from the server?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;There can be scenarios where the server may return complex objects back to the caller. How to handle complex objects in javascript? Don’t worry with HTC file that is also a child’ play. Below is a web method which returns a complex “Car” object. We have slightly altered the “Car” class.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Method that returns a complex Car object.           &lt;br /&gt;[WebMethod]            &lt;br /&gt;public Car GetCarObject()            &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return new Car { Model = &amp;quot;Ferrari&amp;quot;, Color = &amp;quot;Ferrari Red&amp;quot;, CarEngine = new Engine { CC = 2500, Cylinders = 8 } };            &lt;br /&gt;} &lt;/p&gt;          &lt;p&gt;//Car class.           &lt;br /&gt;public class Car            &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160; public string Model            &lt;br /&gt;&amp;#160;&amp;#160; {&amp;#160;&amp;#160; get; set; }            &lt;br /&gt;&amp;#160;&amp;#160; public string Color            &lt;br /&gt;&amp;#160;&amp;#160; { get; set; }            &lt;br /&gt;&amp;#160;&amp;#160; public Engine CarEngine            &lt;br /&gt;&amp;#160;&amp;#160; { get; set; }            &lt;br /&gt;}            &lt;br /&gt;//Engine class which is a property in Car class            &lt;br /&gt;public class Engine            &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160; public int CC            &lt;br /&gt;&amp;#160;&amp;#160; { get; set; }            &lt;br /&gt;&amp;#160;&amp;#160; public int Cylinders            &lt;br /&gt;&amp;#160;&amp;#160; { get; set; }            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the web method we are creating an instance of the “Car” class and returning it. The “Car” class is quite simple, it has a number of properties and in those properties one of the property is again a complex object of type “Engine”. Below is the javascript code which handles the complex object returned from the web service.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;function receiveComplexObject()           &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var txtCntl = document.getElementById(&amp;quot;divHTC&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; txtCntl.useService(&amp;quot;&lt;a href="http://localhost:3150/TestService.asmx?wsdl&amp;quot;"&gt;http://localhost:3150/TestService.asmx?wsdl&amp;quot;&lt;/a&gt;, &amp;quot;webServ&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var uniqueID = txtCntl.webServ.callService(handleComplexObject, &amp;quot;GetCarObject&amp;quot;);            &lt;br /&gt;}            &lt;br /&gt;//Function handling the complex object.            &lt;br /&gt;function handleComplexObject(result)            &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var carObj = result.value;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var carProperties = &amp;quot;Model: &amp;quot; + carObj.model + &amp;quot;, Color: &amp;quot; + carObj.Color + &amp;quot;, CarEngine.CC: &amp;quot; + carObj.CarEngine.CC + &amp;quot;, CarEngine.Cylinders: &amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; + carObj.CarEngine.Cylinders;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; alert(carProperties);            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;“&lt;/strong&gt;handleComplexObject” function handles the complex “Car” object returned from the server. The “value” property of the result object holds the complex object. You can access the various properties and fields in a complex object by specifying the names of the properties or fields as done in the above e.g.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;How to handle server errors in client Side?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;We have seen the different ways of invoking a web method with parameters, without parameters and with complex parameters. As we all know everything doesn’t work according our plan, so what happens if the server throws an error? Handling errors from server is also pretty much simple.The method which is specified in “callService” method to handle the result takes an argument. The result object passed as an argument to the callback function which handles the request from the service has got the necessary information to handle errors. Lets see with e.g.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Javascript function which is supposed to throw error.            &lt;br /&gt;function sendComplexObject()            &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var ccar = new Object();            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ccar.Model = &amp;quot;Ferrari&amp;quot;;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ccar.Color = &amp;quot;Ferrari Red&amp;quot;;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var txtCntl = document.getElementById(&amp;quot;divHTC&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; txtCntl.useService(&amp;quot;&lt;a href="http://localhost:3150/TestService.asmx?wsdl&amp;quot;"&gt;http://localhost:3150/TestService.asmx?wsdl&amp;quot;&lt;/a&gt;, &amp;quot;webServ&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var uniqueID = txtCntl.webServ.callService(handleError, &amp;quot;GetCar&amp;quot;, ccar);            &lt;br /&gt;}            &lt;br /&gt;//Function which handles server side error.            &lt;br /&gt;function handleError(result)            &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (result.error)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var errorDetails = &amp;quot;Error code: &amp;quot; + result.errorDetail.code;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; errorDetails += &amp;quot;\nError string: &amp;quot; + result.errorDetail.string;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; errorDetails += &amp;quot;\nError soap format: &amp;quot; + result.errorDetail.raw.xml;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; alert(errorDetails);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; alert(result.value);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt; The above pasted code is pretty straight forward. The “handleError” function like other callback function in this blog takes a result object as argument. The first line of the function checks whether there is any error in the result returned from the server by checking the “error” property of the result object. If there is an error the next three lines of code retrieve the error details by accessing the “errorDetail”&amp;#160; property’ “code”, “string” and “raw.xml” properties. The code property returns the code for the error, “string” property returns the error message and “raw.xml” returns the whole SOAP xml. &lt;/p&gt;  &lt;p&gt;So that’ some of the key features of webservice behavior. In the next blog we will see how to make use of “ScriptManager” to invoke web methods. Till then try to know more.&lt;/p&gt;  &lt;p&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-703444565036044160?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ttccxRrn73tnlNZBiVtkcLAWYQU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ttccxRrn73tnlNZBiVtkcLAWYQU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ttccxRrn73tnlNZBiVtkcLAWYQU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ttccxRrn73tnlNZBiVtkcLAWYQU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/ARA6Ovsp0Zk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/703444565036044160/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2010/04/calling-web-service-using-javascript.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/703444565036044160?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/703444565036044160?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/ARA6Ovsp0Zk/calling-web-service-using-javascript.html" title="Calling web service using javascript. Part - 2" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2010/04/calling-web-service-using-javascript.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0MBR3s9eyp7ImA9WxFTFEg.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-5399035290786389948</id><published>2010-03-15T23:14:00.000+05:30</published><updated>2010-04-05T13:47:36.563+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-05T13:47:36.563+05:30</app:edited><title>Calling web service using javascript. Part - 1</title><content type="html">&lt;p align="justify"&gt;Many a times there was this pressing need where I had to call a web service method and get the output and use it in javascript. There were many options available like making use of javascript to call web service method or using some third party javascript library or by making use of webservice behavior (HTML Component (HTC) file) or making use of AJAX and calling an ASPX page which in turn calls a webservice in the code behind and returns the output as XML or if you are using ASP.NET 2.0 then you can make use of “ScriptManager”. We will see all the above ways of invoking a webservice one by one except making use of third party javascript library. As there are many third party javascript libraries available in the internet so we will not concentrate on any as each have different ways of implementation.&amp;#160; Lets see each of the methods in action one by one.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Using javascript to invoke web service methods&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;One of the simplest and easiest way is to invoke web service methods is by making use of “XMLHttpRequest” objects in javascript. Before invoking the web service method using javascript lets see the web service methods which we are going to be invoked using javascript. Below are two sample methods which we are going to be invoked using javascript.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public class TestService : System.Web.Services.WebService            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [WebMethod]             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string HelloWorld()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;Hello World&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [WebMethod]             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string CombineName(string firstName, string midleName, string lastName)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return string.Concat(string.Concat(firstName, &amp;quot; - &amp;quot;, midleName), &amp;quot; - &amp;quot;, lastName);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above two methods are pretty straight forward, one returns a string and the other takes three string arguments and combines them and returns as a string. We will see how to call a web service method without parameters and with parameters. Lets first see how to call the “HelloWorld” web method which doesn’t take any parameter. Below is the javascript code for invoking the “HelloWorld” web method.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;function webServiceCallWithoutParameters()            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var xmlHttpReq = new XMLHttpRequest();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xmlHttpReq.open(&amp;quot;POST&amp;quot;, &amp;quot;&lt;a href="http://localhost/TestService.asmx/HelloWorld&amp;quot;"&gt;http://localhost/TestService.asmx/HelloWorld&amp;quot;&lt;/a&gt;, false);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xmlHttpReq.setRequestHeader(&amp;quot;Content-Type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xmlHttpReq.send();&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; alert(xmlHttpReq.responseXML.childNodes[1].text);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above javascript function we are making use of “XMLHttpRequest” object to invoke web service method. We are making use of the “open” method which takes three arguments, first argument says whether the method used is “POST” or “GET”, second argument is the url for the web service and the third argument is to specify whether the “XMLHttpRequest” should be synchronous or asynchronous. The url for a .NET web method always ends with the name of the web method preceded by the url of the web service as shown in the above e.g. Once we have set the url and the method used to call the web service, in the next line we are setting the “Content-Type” for the request using the “setReqeustHeader” method of the “XMLHttpReqeust” object. Its very important to set the “Content-Type” of the request to “application/x-www-form-urlencoded” when you are trying to invoke a web service method, if the “Content-Type” is not set to “application/x-www-form-urlencoded” the following error will be thrown when you try to pass arguments to a web service method.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;status: 500            &lt;br /&gt;statusText: Internal Server Error             &lt;br /&gt;responseText: System.InvalidOperationException: Request format is invalid: . at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The status property of the “XMLHttpReqeust” object will have a value of 500, the “statusText” will be having “Internal Server Error” as the message and the “responseText” will have the type of error. To solve the problem set the “Content-Type” for the request as shown in the above e.g.&lt;/p&gt;  &lt;p align="justify"&gt;Now let’ see how to pass parameters to a web method. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;var xmlHttpReq = new XMLHttpRequest();            &lt;br /&gt;xmlHttpReq.open(&amp;quot;POST&amp;quot;, &amp;quot;&lt;a href="http://localhost:3150/TestService.asmx/CombineName&amp;quot;"&gt;http://localhost:3150/TestService.asmx/CombineName&amp;quot;&lt;/a&gt;, false);             &lt;br /&gt;xmlHttpReq.setRequestHeader(&amp;quot;Content-Type&amp;quot;,&amp;quot;application/x-www-form-urlencoded&amp;quot;);             &lt;br /&gt;xmlHttpReq.send(&amp;quot;firstName=Sandeep&amp;amp;midleName=mid&amp;amp;lastName=p r&amp;quot;);             &lt;br /&gt;alert(xmlHttpReq.responseXML); &lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above javascript function we are invoking the “CombineName” web method which takes three parameters. Everything else is the same as the previous javascript function except for the “send” method of the “XMLHttpReqeust” object. In the “send” method we are passing the parameters separated by the ampersand (&amp;amp;) symbol. To pass parameter to a web method you can use the “send” method of the “XMLHttpRequest” object. The parameters and their values should be separated by the “equal to” (=) symbol and each set of parameters should be separated by the ampersand symbol. &lt;/p&gt;  &lt;p align="justify"&gt;After this the next obvious question would be how to send complex objects to a web methods. There is no straight forward way of doing this, one can make use of Javascript Object Notations (JSON). You can find lots of e.g. on how to pass JSON over the net. Also you can use “WebService” behavior (HTC file) to send and receive complex objects or one can use “ScriptManager” to send and receive complex objects. We will see “HTC file” and “ScriptManager” usage in subsequent blogs. Receiving a complex object is pretty easy. When you invoke a web method and if that web method returns a complex object then the object will be serialized into a XML and the XML can be accessed through the “responseText” or “responseXML” property of the “XMLHttpRequest” object. Lets see with an e.g. Below is the web method code which returns a “Car” complex object.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;[WebMethod]            &lt;br /&gt;public Car GetCarObject()             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return new Car { Model = &amp;quot;Ferrari&amp;quot;, Color = &amp;quot;Ferrari Red&amp;quot;, CC = 2555 };             &lt;br /&gt;}&lt;/p&gt;          &lt;p&gt;//Car Class is pasted below.&lt;/p&gt;          &lt;p&gt;public class Car            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Model             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get;&amp;#160;&amp;#160; set;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int CC             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get;&amp;#160;&amp;#160; set;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Color             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get;&amp;#160;&amp;#160; set;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above web method is pretty straight forward. It creates an instance of a “Car” class and returns the same. The “Car” class is also pasted. The javascript method to invoke the above web method which returns “Car” complex object is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;function getComplexObject()            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var xmlHttpReq = new XMLHttpRequest();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; xmlHttpReq.open(&amp;quot;POST&amp;quot;, &amp;quot;&lt;a href="http://localhost:3150/TestService.asmx/GetCarObject&amp;quot;"&gt;http://localhost:3150/TestService.asmx/GetCarObject&amp;quot;&lt;/a&gt;, false);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; xmlHttpReq.setRequestHeader(&amp;quot;Content-Type&amp;quot;, &amp;quot;application/x-www-form-urlencoded&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; xmlHttpReq.send();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; alert(xmlHttpReq.responseText);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;The output returned in the form of the XML is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;            &lt;br /&gt;&amp;lt;Car xmlns:xsi=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema-instance&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/a&gt; xmlns:xsd=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/a&gt; xmlns=&amp;quot;&lt;a href="http://tempuri.org/&amp;quot;"&gt;http://tempuri.org/&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Model&amp;gt;Ferrari&amp;lt;/Model&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;CC&amp;gt;2555&amp;lt;/CC&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Color&amp;gt;Ferrari Red&amp;lt;/Color&amp;gt;             &lt;br /&gt;&amp;lt;/Car&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;As you can see in the above XML the complex object returned by the web method is serialized as XML. That's about invoking web methods using javascript. In my next blog we will see how to invoke web methods using “WebService behviour” or by using “HTC file”. Till then try to know more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-5399035290786389948?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/mNdLI2dudR4-YS-swC6gYf7jGC4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/mNdLI2dudR4-YS-swC6gYf7jGC4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/mNdLI2dudR4-YS-swC6gYf7jGC4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/mNdLI2dudR4-YS-swC6gYf7jGC4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/UHmBfAy0ZJg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/5399035290786389948/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2010/03/calling-web-service-using-javascript.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5399035290786389948?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5399035290786389948?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/UHmBfAy0ZJg/calling-web-service-using-javascript.html" title="Calling web service using javascript. Part - 1" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2010/03/calling-web-service-using-javascript.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUQERH4-cCp7ImA9WxBUE0o.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-7584434892310393355</id><published>2010-02-28T21:38:00.001+05:30</published><updated>2010-02-28T21:38:25.058+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-28T21:38:25.058+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS 2010" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio 2010" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio" /><title>Visual Studio 2010 features preview.</title><content type="html">&lt;p align="justify"&gt;Today I got an opportunity to attend the “Visual Studio 2010 Pit stop” in Chennai. If I am not wrong it is a pre launch event which Microsoft is conducting in different cities to generate interest and gauge developers reaction on the various features which is getting introduced in VS 2010. I thought why not blog on some of the features which were demoed. &lt;/p&gt;  &lt;p align="justify"&gt;With the release of VS 2010 Microsoft is trying to make Visual Studio capable of managing all the phases of Application Lifecycle Management (ALM). They want VS 2010 to target developers, architects and testers. Lets see what VS 2010 brings to the table for different audience it targets.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;For developers&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;The first feature which was demoed was simple yet powerful, which was targeted towards developers like me, who hate to drag and drop controls from the toolbox instead prefer writing the controls in HTML view. The feature is a kind of auto complete whereby when in the HTML view of an aspx page you need to type the name of the control and from the intellisense one need to select the control and press tab and VS 2010 will add the full tag with ID, Text and other properties. Though the beta release of VS 2010 which was used for the demo didn’t auto generate ID and other properties, it only auto generated “Text” property, but it was promised that the production version would generate most of the relevant properties like ID etc.&lt;/p&gt;  &lt;p align="justify"&gt;Another feature was a template kind of feature for creating web sites where you get pre-configured pages for login, user registration etc. One gets a full website with some color scheme applied and .mdf file in the “App_data” folder. The .mdf database has all the tables for user management and one can generate schema from this .mdf and recreate the tables in Sql Server 2005/2008. One can use the login, user registration pages without doing any changes or writing any code. Now creating a website is just few clicks away. Microsoft is making the life of a developer easy. Is it? I would say nah, with all these automatic features the developer will forget coding and he will become a lazy bum. Nowadays, when I do interviews many a developers don’t know many of the basics, with all these new changes the next generation developers job will have less of coding and more of configuration. &lt;/p&gt;  &lt;p align="justify"&gt;Next feature was the business layer code generation feature from the Entity framework. You need to follow some of the steps in a wizard and select the needed tables from the database and voila you have your Business Access Layer (BAL) auto generated code. If you tie this auto generated BAL layer to a grid and set some properties you have a grid which is capable of adding, deleting, updating, sorting records without writing a single line of code. One also gets search integration using new search extender control where you need set some properties and you can filter your records based on the search criteria. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;IntelliTrace&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;The IntelliTrace feature like Intellisense is going to become a huge hit with developers. With intellitrace you can exactly pinpoint which line of the code threw the exception with values which were passed to the function. Intellitrace feature in combination with Test and Lab management features of VS 2010 is going to rock. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;For Architects&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;For the architects visual studio supports UML diagram drawing capabilities. You can draw class diagrams, dependency diagrams, sequence diagrams etc which were earlier done in Visio or some other tool. The architects can have validation checks on dependency during compile time or whatever policies they have set. For e.g. take a simple e.g. where we have a Presentation layer, Business Access Layer and Data Access Layer. The architect doesn’t want the presentation layer to interact with DAL. He can set a constraint like this and during compile time the system will throw an error if the constraints is broken. Also you can restrict classes in a dll from interacting with other dlls. One can generate sequence diagram from methods by right clicking on the method and selecting some option from the context menu.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Visual Studio Test and Lab management&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;This feature of VS 2010 just blew my head away. This feature is aimed at the testers but I feel this feature will be a boon to the developers. In this feature the testers will have provision to add their manual test steps and record them and then replay them. The feature works like this. The testers will first enter all the manual test steps like the following in an UI.&lt;/p&gt;  &lt;p align="justify"&gt;1. “Open IE and type the following url: &lt;a href="http://someone.com"&gt;http://someone.com&lt;/a&gt;”.&lt;/p&gt;  &lt;p align="justify"&gt;2. Click register link from the UI.&lt;/p&gt;  &lt;p align="justify"&gt;3. Fill first name with “Sandeep”&lt;/p&gt;  &lt;p align="justify"&gt;4. Fill last name with “P.R”&lt;/p&gt;  &lt;p align="justify"&gt;5. Fill email field with &lt;a href="mailto:&amp;ldquo;sndppr@gmail.com"&gt;“sndppr@gmail.com&lt;/a&gt;”&lt;/p&gt;  &lt;p align="justify"&gt;6. Fill the password field with “123456”&lt;/p&gt;  &lt;p align="justify"&gt;7. Fill the confirm password field with “123456”&lt;/p&gt;  &lt;p align="justify"&gt;8. Click the “Register” button.&lt;/p&gt;  &lt;p align="justify"&gt;Once the steps are done the tester can now start recording the steps. The advantage of this feature is that if there is an error in step 8 the tester need not repeat all the steps manually. Once the recording is over he can replay the steps upto step 8 and he need not manually do all the above steps. Think of a situation where there are 50 steps and the error happens in the 45th step then once the bug is fixed the tester has to repeat all the 44 steps manually to reach the 45th step which threw error. Just by recording the steps he can play them back up to the 44th step and then carry on the recording from the 45th step. Can you imagine how much time will be saved on this? It will be huge. Hats off to Microsoft for this.&lt;/p&gt;  &lt;p align="justify"&gt;That’ not all. What happens when a tester posts a bug? Bang comes the reply from the developer, “it works on my system, so sorry boss its not a bug”. Some developer even go to the extend where they ask to reproduce the bug. Poor tester, he has to repeat the whole 44 steps to show the bug. Not any more. Once the tester finds a bug he can use the new features to post the bug while he is recording and can assign the bug to a developer. The developer when he gets the bug has got links to view the video of the recording where he can exactly see what the tester has done to reproduce the error. Now there will be no more tester developer acrimony. Also to top it all he will get the IntelliTrace link along with the link to view the video. When the developer clicks the IntelliTrace link, it goes to repository (Team Foundation Server [TFS]) and opens your code and shows you the exact place where the error occurred. Not only this, it gives you the exact values which were used by the tester and which were passed to the function. Just by clicking a link you can know the exact line number where the error has happened and the data which caused the problem. Isn’t it so cool. IntelliTrace is gonna ROCK. Developers, our life is going to be simple. :) With IntelliTrace, I think, the bug fixing time taken by the developers will be reduced to 1/4th of what a normal developer takes. Wow that’ really superb. After all time is money.&lt;/p&gt;  &lt;p align="justify"&gt;With the above features and many more, Visual Studio 2010 is going to &lt;strong&gt;&lt;font color="#ff0000"&gt;ROCK&lt;/font&gt;&lt;/strong&gt;. Waiting to get my hands dirty with VS 2010.&lt;/p&gt;  &lt;p align="justify"&gt;Try to know more….&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-7584434892310393355?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vs0Jx_xIRHiNXQuYSrWdcpQPB0M/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vs0Jx_xIRHiNXQuYSrWdcpQPB0M/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/vs0Jx_xIRHiNXQuYSrWdcpQPB0M/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vs0Jx_xIRHiNXQuYSrWdcpQPB0M/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/8vxiA89wZgw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/7584434892310393355/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2010/02/visual-studio-2010-features-preview.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/7584434892310393355?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/7584434892310393355?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/8vxiA89wZgw/visual-studio-2010-features-preview.html" title="Visual Studio 2010 features preview." /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>1</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2010/02/visual-studio-2010-features-preview.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEcEQ34-eCp7ImA9WxFREko.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-8342409441198246775</id><published>2010-02-07T22:52:00.001+05:30</published><updated>2010-04-26T14:23:22.050+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-26T14:23:22.050+05:30</app:edited><title>Extending the membership provider.</title><content type="html">&lt;p align="justify"&gt;Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider class but don’t give you what other class they have used in the backend. With this blog I would not only show how to extend MembershipProvider class but will give you the full source code of the web service which is used instead of a database as a backend. &lt;/p&gt;  &lt;p align="justify"&gt;I have made use of the MembershipProvider many a times but I would like to share my experience in one of my project where I had to extend the MembershipProvider class to make use of an already existing web service of the client to validate user, create user, change password etc. In this blog I will show how to extend MembershipProvider class and write your custom MembershipProvider class.&lt;/p&gt;  &lt;p align="justify"&gt;Since I cannot reveal what webserivce was used in the website developed for the client I will create my own webservice to mimic the exact behavior as in the website. So lets start by creating a webservice which will have methods to create a user, validate user credentials and finally change the user’ password. Once the webservice is done we will extend the membership class. Below is the code for the webservice.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;[WebService(Namespace = &amp;quot;&lt;a href="http://tempuri.org/&amp;quot;)]"&gt;http://tempuri.org/&amp;quot;)]&lt;/a&gt;             &lt;br /&gt;[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]             &lt;br /&gt;public class WebServiceAuthentication : System.Web.Services.WebService             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string xmlFilePath = string.Empty;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public WebServiceAuthentication()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xmlFilePath = Server.MapPath(&amp;quot;data/users.xml&amp;quot;);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; [WebMethod]            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public bool ChangePassword(string username, string newpassword)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XDocument users = XDocument.Load(xmlFilePath);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var user = from u in users.Elements(&amp;quot;user&amp;quot;)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where u.Element(&amp;quot;username&amp;quot;).Value.ToLower() == username.ToLower()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select u;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (user != null &amp;amp;&amp;amp; user.Count() &amp;gt; 0)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.First().Element(&amp;quot;password&amp;quot;).Value = newpassword;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; [WebMethod]            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public bool ValidateUser(string userName, string passWord)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XDocument users = XDocument.Load(xmlFilePath);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var user = from u in users.Element(&amp;quot;users&amp;quot;).Elements(&amp;quot;user&amp;quot;)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where u.Element(&amp;quot;username&amp;quot;).Value.ToLower() == userName.ToLower() &amp;amp;&amp;amp; u.Element(&amp;quot;password&amp;quot;).Value == passWord             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select u;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (user != null &amp;amp;&amp;amp; user.Count() &amp;gt; 0)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; [WebMethod]            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Customer CreateUser(string username, string password, string emailID)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XDocument users = XDocument.Load(xmlFilePath);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var user = from u in users.Elements(&amp;quot;user&amp;quot;)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where u.Attribute(&amp;quot;email&amp;quot;).Value.ToLower() == username.ToLower()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select u.Attribute(&amp;quot;email&amp;quot;).Value;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (user.Count() &amp;lt;= 0)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new Exception(&amp;quot;Email already in user&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; users.Element(&amp;quot;users&amp;quot;).Add(new XElement(&amp;quot;user&amp;quot;,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XAttribute(&amp;quot;email&amp;quot;, emailID),             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XElement(&amp;quot;userName&amp;quot;, username),             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XElement(&amp;quot;password&amp;quot;, password)));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; users.Save(xmlFilePath);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return new Customer { UserName = username };            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; [WebMethod]            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string ResetPassword(string username)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string newPass = string.Empty;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XDocument users = XDocument.Load(xmlFilePath);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var user = from u in users.Elements(&amp;quot;user&amp;quot;)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where u.Element(&amp;quot;username&amp;quot;).Value.ToLower() == username.ToLower()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select u;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (user != null &amp;amp;&amp;amp; user.Count() &amp;gt; 0)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newPass = Guid.NewGuid().ToString().Substring(0, 10);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.First().Element(&amp;quot;password&amp;quot;).Value = newPass;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new Exception(&amp;quot;User not found.&amp;quot;); &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return newPass;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; [WebMethod]            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public bool UpdateCustomer(Customer customerToBeUdpated)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XDocument users = XDocument.Load(xmlFilePath);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var userToBeUpdated = (from u in users.Elements(&amp;quot;user&amp;quot;)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; where u.Attribute(&amp;quot;email&amp;quot;).Value.ToLower() == customerToBeUdpated.UserName.ToLower()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select u);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (userToBeUpdated != null &amp;amp;&amp;amp; userToBeUpdated.Count() &amp;gt; 0)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XElement user = userToBeUpdated.First();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Element(&amp;quot;password&amp;quot;).Value = customerToBeUdpated.Password;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (user.Element(&amp;quot;firstname&amp;quot;) != null)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Element(&amp;quot;firstname&amp;quot;).Value = customerToBeUdpated.FirstName;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Add(new XElement(&amp;quot;firstname&amp;quot;, customerToBeUdpated.FirstName));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (user.Element(&amp;quot;lastname&amp;quot;) != null)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Element(&amp;quot;lastname&amp;quot;).Value = customerToBeUdpated.LastName;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Add(new XElement(&amp;quot;lastname&amp;quot;, customerToBeUdpated.LastName));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (user.Element(&amp;quot;address&amp;quot;) != null)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Element(&amp;quot;address&amp;quot;).Element(&amp;quot;street&amp;quot;).Value = customerToBeUdpated.Address.Street;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Element(&amp;quot;address&amp;quot;).Element(&amp;quot;city&amp;quot;).Value = customerToBeUdpated.Address.City;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Element(&amp;quot;address&amp;quot;).Element(&amp;quot;state&amp;quot;).Value = customerToBeUdpated.Address.State;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Element(&amp;quot;address&amp;quot;).Element(&amp;quot;country&amp;quot;).Value = customerToBeUdpated.Address.Country;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; user.Add(new XElement(&amp;quot;address&amp;quot;, new XElement(&amp;quot;street&amp;quot;, customerToBeUdpated.Address.Street),             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XElement(&amp;quot;city&amp;quot;, customerToBeUdpated.Address.City),             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XElement(&amp;quot;state&amp;quot;, customerToBeUdpated.Address.State),             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new XElement(&amp;quot;country&amp;quot;, customerToBeUdpated.Address.Country)));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;} &lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above web service we have methods to change the password (ChangePassword), to validate user credential based on user name and password (ValidateUser), method to create a user (CreateUser), method to change the password (ResetPassword) and finally method to update user details (UpdateCustomer). All these methods work on a XML file and make use of LINQ to XML to create user, change password etc. As mentioned before all the data is stored and retrieved from a XML file called Users.xml. If a new user has to be added or user details needs to be updated or password needs to be changed everything is done in the xml. The webservice makes use of a XML as a storage medium.The XML is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; ?&amp;gt;            &lt;br /&gt;&amp;lt;users&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;user email=&amp;quot;a@a.com&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;username&amp;gt;sandeep&amp;lt;/username&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;password&amp;gt;pass&amp;lt;/password&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;firstname&amp;gt;Sandeep&amp;lt;/firstname&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;lastname&amp;gt;P.R&amp;lt;/lastname&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;address&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;street&amp;gt;Blah blah&amp;lt;/street&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;city&amp;gt;City&amp;lt;/city&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;state&amp;gt;State&amp;lt;/state&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;country&amp;gt;Country&amp;lt;/country&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/address&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;/user&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;user email=&amp;quot;sndppr@gmail.com&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;username&amp;gt;sndppr@gmail.com&amp;lt;/username&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;password&amp;gt;pass&amp;lt;/password&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;firstname&amp;gt;Sandeep&amp;lt;/firstname&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;lastname&amp;gt;P.R&amp;lt;/lastname&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;address&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;street&amp;gt;Blah blah&amp;lt;/street&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;city&amp;gt;City&amp;lt;/city&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;state&amp;gt;State&amp;lt;/state&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;country&amp;gt;Country&amp;lt;/country&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/address&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;/user&amp;gt;             &lt;br /&gt;&amp;lt;/users&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The xml is pretty much straight forward. The XML stores the user name, password and other user related details like his address and email id. In the XML the password is saved as plain text which is not a good way of storing password. So while storing password in a physical file do use some sort of encryption.&lt;/p&gt;  &lt;p align="justify"&gt;Some of the methods of the web service return “Customer” as an object. The “Customer” class is also very straight forward. It derives from the “System.Web.Security.MembershipUser” class and adds some of its own properties. The “Customer” class code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public class Customer : System.Web.Security.MembershipUser            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Customer()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; private string userName = string.Empty; &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public string FirstName { get; set; }            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string LastName { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string UserName { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string EMail { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Password { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Address Address { get; set; }             &lt;br /&gt;} &lt;/p&gt;          &lt;p&gt;public class Address            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Street { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string City { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string State { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Country { get; set; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;That’s about web serivce and the XML file which form the backend, now lets try to extend System.Web.Security.MembershipProvider to work in conjunction with the above web service. Before that lets keep some things in mind. If you are not planning to make use of question and answer to reset the password then make sure that you return false from “RequiresQuestionAndAnswer” property. If you leave the default implementation then the system will throw “Sysmtem.NotImplementedException” with the following error message.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;The method or operation is not implemented. &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Now lets see the code where I have extended the System.Web.Security.MembershipProvider class. Some of the methods which have not been implemented have been removed for brevity.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public class CustomMembershipProvider : System.Web.Security.MembershipProvider            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public CustomMembershipProvider() : base()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override string ApplicationName            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;Authentication&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new NotImplementedException();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Non implemented methods have been removed for brevity.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public override bool ChangePassword(string username, string oldPassword, string newPassword)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WebServiceAuthentication wsa = new WebServiceAuthentication();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return wsa.ChangePassword(username, newPassword);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MembershipUser cust = this.GetUser(username, false);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (cust == null)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WebServiceAuthentication wsa = new WebServiceAuthentication();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cust = wsa.CreateUser(username, password, email);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; status = cust != null ? MembershipCreateStatus.Success : MembershipCreateStatus.UserRejected;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; status = MembershipCreateStatus.DuplicateUserName;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return cust;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override MembershipUser GetUser(string username, bool userIsOnline)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WebServiceAuthentication wsa = new WebServiceAuthentication();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; MembershipUser mu = wsa.GetUser(username);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return mu;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override int MinRequiredNonAlphanumericCharacters            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get { return 0; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override int MinRequiredPasswordLength            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get { return 2; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override bool RequiresQuestionAndAnswer            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get { return false; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override bool RequiresUniqueEmail            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get { return false; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Non implemented methods have been removed for brevity &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override void UpdateUser(MembershipUser user)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WebServiceAuthentication wsa = new WebServiceAuthentication();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wsa.UpdateCustomer(user);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public override bool ValidateUser(string username, string password)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; WebServiceAuthentication wsa = new WebServiceAuthentication();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (wsa.ValidateUser(username, password))             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), true, string.Empty);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; HttpContext.Current.Request.Cookies.Add(authCookie);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return true;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In above code I have extended the MembershipProvider class and overridden “ChangePassword”, “CreateUser”, “GetUser”, “UpdateUser” and “ValidateUser” methods. All these methods are pretty straight forward, the methods create an instance of our web service class and call the web methods to change password, create user etc. The before mentioned membership methods are called by the various login controls provided in ASP.NET. “ChangePassword” is called by the “ChangePassword” control when you click the “Change Password” button. “CreateUser” method of the MembershipProvider class is executed when you click the “Create User” button of “CreateUserWizard” control. “ValidateUser” method is called whenever the user request needs validation or when you click the “Log in” button of the “Log in” control.&lt;/p&gt;  &lt;p align="justify"&gt;Once you have extended the MembershipProvider class add the following tag in the web.config file of your website.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;membership defaultProvider=&amp;quot;CustomProvider&amp;quot; &amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;providers&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;add name=&amp;quot;CustomProvider&amp;quot; type=&amp;quot;CustomMembershipProvider&amp;quot; /&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/providers&amp;gt;&amp;#160; &lt;br /&gt;&amp;lt;/membership&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above markup the &amp;quot;defaultProvider&amp;quot; is an optional parameter. If not provided it will default to &amp;quot;AspNetSqlProvider&amp;quot;, the default provider provided by Microsoft. The &amp;quot;type&amp;quot; attribute has the name of the extended membership provider class. In our case, since the class is placed in the &amp;quot;App_Code&amp;quot; folder, we have only specified the class name. If your membership provider class lies in some different dll and you have added it as a reference to your website then one has to specify the full name preceded by the namespace as well. Also using the “add” tag in “providers” tag you can set the membership properties like &amp;quot;connectionStringName&amp;quot;, &amp;quot;enablePasswordRetrieval&amp;quot;, &amp;quot;enablePasswordReset&amp;quot; etc in the. Sample tag is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;connectionStrings&amp;gt;           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;add name=&amp;quot;connStr&amp;quot; connectionString=&amp;quot;Data Source=testserver;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa&amp;quot;/&amp;gt;            &lt;br /&gt;&amp;lt;/connectionStrings&amp;gt;            &lt;br /&gt;&amp;lt;!--Membership provider configuration with membership properties set in web.config. The connectionStringName should match with the one provided in you web.config.--&amp;gt;            &lt;br /&gt;&amp;lt;membership defaultProvider=&amp;quot;CustomProvider&amp;quot; &amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;providers&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;add name=&amp;quot;CustomProvider&amp;quot; type=&amp;quot;CustomMembershipProvider&amp;quot; applicationName=&amp;quot;TestApp&amp;quot; connectionStringName=&amp;quot;connStr&amp;quot; enablePasswordRetrieval=&amp;quot;false&amp;quot; enablePasswordReset=&amp;quot;true&amp;quot; maxInvalidPasswordAttempts=&amp;quot;5&amp;quot;&amp;#160; /&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/providers&amp;gt;&amp;#160; &lt;br /&gt;&amp;lt;/membership&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;With this done you can drag and drop the various ASP.NET login controls like the CreateUserWizard, ChangePassword, Login etc and you can create users, change password and login into the application without writing any code in the code behind files where these ASP.NET login controls are used. Isn’t it so easy to use the various ASP.NET login controls once you have extended the MembershipProvider class. &lt;/p&gt;  &lt;p align="justify"&gt;Try to know more&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-8342409441198246775?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/9mohrsXnD0Q0VV6dPzTGc_dLnK4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9mohrsXnD0Q0VV6dPzTGc_dLnK4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/9mohrsXnD0Q0VV6dPzTGc_dLnK4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9mohrsXnD0Q0VV6dPzTGc_dLnK4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/SblzdwKqaIU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/8342409441198246775/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2010/02/extending-membership-provider.html#comment-form" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/8342409441198246775?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/8342409441198246775?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/SblzdwKqaIU/extending-membership-provider.html" title="Extending the membership provider." /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>5</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2010/02/extending-membership-provider.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEEBRHg9eCp7ImA9WxBVFEQ.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-6082293817589361328</id><published>2010-01-19T21:44:00.001+05:30</published><updated>2010-02-18T18:07:35.660+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-18T18:07:35.660+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Serialization" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="XML Serialization" /><category scheme="http://www.blogger.com/atom/ns#" term="C# 3.0" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="IXmlSerializable" /><category scheme="http://www.blogger.com/atom/ns#" term="Custom Serialization" /><title>Serialization in .NET – 5</title><content type="html">&lt;p align="justify"&gt;In my previous blogs we have seen &lt;a href="http://sandblogaspnet.blogspot.com/2009/05/serialization-in-net-1.html" target="_blank"&gt;Binary serialization&lt;/a&gt;, &lt;a href="http://sandblogaspnet.blogspot.com/2009/05/serialization-in-net-2.html" target="_blank"&gt;XML serialization&lt;/a&gt;, &lt;a href="http://sandblogaspnet.blogspot.com/2009/07/serialization-in-net-3.html" target="_blank"&gt;SOAP serialization&lt;/a&gt; and &lt;a href="http://sandblogaspnet.blogspot.com/2009/12/serialization-in-net-4.html" target="_blank"&gt;Custom serialization&lt;/a&gt; using &lt;a href="http://sandblogaspnet.blogspot.com/2009/12/serialization-in-net-4.html" target="_blank"&gt;ISerilizable&lt;/a&gt; interface. This blog will talk about implementing custom XML serialization using the IXmlSerializable interface. We have seen in my previous blogs on serialization how to control XML and SOAP serialization using attributes. Through attributes we can take control to some extent but not to the fullest. If you want to take full control over the way your class is serialized to XML then you need to use IXmlSerializable interface. &lt;/p&gt;  &lt;p align="justify"&gt;Implementing IXmlSerializable interface is pretty straight forward. The interface has three methods namely “WriteXml (XmlWriter writer)”, “ReadXml (XmlReader reader)” and “GetSchema()”. WriteXml method is called when the object is serialized and ReadXML method is executed when the XmlSerializer deserializes the object. GetSchema method returns a XmlSchema object. As such the GetSchema method is not supposed to do anything other than returning null. The help documentation from MSDN says the following about the method.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p align="justify"&gt;“&lt;em&gt;When serializing or deserializing an object, the XmlSerializer class does not perform XML validation. For this reason, it is often safe to omit schema information by providing a trivial implementation of this method, for example by returning nullNothingnullptra null reference (Nothing in Visual Basic) (Nothing in Visual Basic).&lt;/em&gt;&lt;/p&gt;          &lt;p align="justify"&gt;&lt;em&gt;If providing schema information is necessary, you should use the &lt;/em&gt;&lt;a href="ms-help://ms.vscc.v90/145d2981-3a50-1535-de8e-25e7a8269bb2.htm"&gt;&lt;em&gt;XmlSchemaProviderAttribute&lt;/em&gt;&lt;/a&gt;&lt;em&gt; attribute. The GetSchema method should still return nullNothingnullptra null reference (Nothing in Visual Basic) (or Nothing).&lt;/em&gt;&lt;/p&gt;          &lt;p align="justify"&gt;&lt;em&gt;Some .NET Framework types as well as legacy custom types implementing the IXmlSerializable interface may be using GetSchema instead of XmlSchemaProvider. In this case, the method returns an accurate XML schema that describes the XML representation of the object generated by the WriteXml() method&lt;/em&gt;.”&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Lets dive into the code and implement the interface in our good old “Car” class. One thing to keep in mind before implementing IXmlSerializable interface is that the class which implements the interface should have a parameter less constructor. If the class which is implementing IXmlSerializable interface doesn’t have a parameter less constructor then on serialization the following error will be thrown.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;font color="#ff0000"&gt;Vehicles.Car cannot be serialized because it does not have a parameterless constructor.&lt;/font&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Below is the Car class with IXmlSerializable interface implementation.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;[System.Xml.Serialization.XmlRoot(Namespace=&amp;quot;&lt;a href="http://car/car&amp;quot;)]"&gt;http://car/car&amp;quot;)]&lt;/a&gt;             &lt;br /&gt;public class Car : System.Xml.Serialization.IXmlSerializable             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; #region Properties             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Color             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public string ModelName            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public int Price            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public double CubicCentimeter            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; #endregion &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; #region IXmlSerializable Members &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; System.Xml.Schema.XmlSchema System.Xml.Serialization.IXmlSerializable.GetSchema()            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; throw new NotImplementedException();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; void System.Xml.Serialization.IXmlSerializable.ReadXml (System.Xml.XmlReader reader)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string ns = &amp;quot;&lt;a href="http://car/car&amp;quot;;"&gt;http://car/car&amp;quot;;&lt;/a&gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (reader.NodeType == System.Xml.XmlNodeType.Element &amp;amp;&amp;amp; reader.Name == &amp;quot;Car&amp;quot;)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.MoveToAttribute(&amp;quot;Price&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Price = reader.ReadContentAsInt();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.MoveToElement();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; reader.Read();&lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (reader.NodeType == System.Xml.XmlNodeType.Element &amp;amp;&amp;amp; reader.Name == &amp;quot;ModelName&amp;quot;)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.ModelName = reader.ReadElementContentAsString();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (reader.NodeType == System.Xml.XmlNodeType.Element &amp;amp;&amp;amp; reader.Name == &amp;quot;CC&amp;quot;)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.CubicCentimeter = reader.ReadElementContentAsDouble() ;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (reader.NodeType == System.Xml.XmlNodeType.Element &amp;amp;&amp;amp; reader.Name == &amp;quot;Color&amp;quot;)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Color= reader.ReadElementContentAsString();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; void System.Xml.Serialization.IXmlSerializable.WriteXml (System.Xml.XmlWriter writer)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string ns = &amp;quot;&lt;a href="http://car/car&amp;quot;;"&gt;http://car/car&amp;quot;;&lt;/a&gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteStartAttribute(&amp;quot;Price&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteString(this.Price.ToString());             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteEndAttribute();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteStartElement(&amp;quot;ModelName&amp;quot;, ns);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteString(this.ModelName);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteEndElement();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteStartElement(&amp;quot;CC&amp;quot;, ns);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteString(this.CubicCentimeter.ToString());             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteEndElement();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteStartElement(&amp;quot;Color&amp;quot;, ns);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteString(this.Color);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; writer.WriteEndElement();&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; #endregion            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above code is pretty straight forward, in the WriteXml method we are making use of the XmlWriter class and serializing the Car class in our customized way. We are writing the Price property as an attribute to the root element by making use of the “WriteStartAttribute” method of the XmlWriter class.&amp;#160; Similarly we are making use of “WriteStartElement” method of the XmlWriter class to write the other properties as an element node. The XML generated by WriteXml is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;            &lt;br /&gt;&amp;lt;Car Price=&amp;quot;23423&amp;quot; xmlns=&amp;quot;&lt;a href="http://car/car&amp;quot;"&gt;http://car/car&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;ModelName&amp;gt;Beetle&amp;lt;/ModelName&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;CC&amp;gt;0&amp;lt;/CC&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Color&amp;gt;Yellow&amp;lt;/Color&amp;gt;             &lt;br /&gt;&amp;lt;/Car&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Seeing the XML output you will be thinking that in “WriteXml” method we have not written code to write the “Car” root node, then how come there is a root node? The reason is that when the XmlWriter object, which is passed as an argument to the WriteXml method, already has the root node. By default the name of the root node will be the class name itself. If you notice we have placed the “XmlRoot” attribute on the “Car” class. So if you want to control the root name then you have to make use of the “XmlRoot” attribute. &lt;/p&gt;  &lt;p align="justify"&gt;In the ReadXml method we are making use of XmlReader class to read the Xml serialized using the WriteXml method and reconstructing our Car class. When the Car class is deserialized the XML serializer executes the ReadXml method by passing the XmlReader class as an argument. When ReadXml is called the reader is positioned at the start of the document. The ReadXml function is&amp;#160; making use of various methods of the XmlReader class to read the content of the XML and reconstructing our serialized Car class. We are checking whether the reader is currently pointing to a attribute or element and then making use of appropriate methods to read the content. &lt;/p&gt;  &lt;p align="justify"&gt;Since there is no validation happening during serializing and deserializing we have left the “GetSchema” method empty. If you really want to return a schema then what should you do? &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Returning a schema from IXmlSerializable implemented class.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;In scenarios where you want to return a schema file one should apply “XmlSchemaProvider” attribute to the IXmlSerializable implemented class. “XmlSchemaProvider” contains the name of the method which returns the schema for the class. The method which is specified in the “XmlSchemaProvider” should be static. Also the method should return an object of type “XmlQualifiedName” and should take “XmlSchemaSet” object as an argument. The method should take the responsibility of filling the “XmlSchemaSet” object passed as an argument. If you are applying “XmlSchemaProvider” to a class then make sure the schema files don’t have any schema errors else error will thrown during the serialization process. &lt;/p&gt;  &lt;p align="justify"&gt;Everything apart lets see how to implement “XmlSchemaProvider” attribute on a IXmlSerialization implemented class. Below is the Car class with the “XmlSchemaProvider” attribute applied. I have pasted only the relevant code.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;[System.Xml.Serialization.XmlSchemaProvider(&amp;quot;ReturnSchema&amp;quot;)]            &lt;br /&gt;public class Car : System.Xml.Serialization.IXmlSerializable             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; /*Code has been trimmed for brevity*/ &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public static System.Xml.XmlQualifiedName ReturnSchema (System.Xml.Schema.XmlSchemaSet xs)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof (System.Xml.Schema.XmlSchema));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Xml.Schema.XmlSchema xmlSchema = (System.Xml.Schema.XmlSchema) xmlSer.Deserialize (new System.Xml.XmlTextReader(&amp;quot;CustomSerilize_app1.xsd&amp;quot;));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xs.XmlResolver = new System.Xml.XmlUrlResolver();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xs.Add(xmlSchema);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return new System.Xml.XmlQualifiedName (&amp;quot;Car&amp;quot;, “&lt;a href="http://car/car"&gt;http://car/car&lt;/a&gt;”);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;I don’t think the above code needs much of an explanation. In the first few lines of code we are pumping the XmlSchema object with the schema information by reading the schema file from disk. Once that is done we are returning a XmlQualifiedName object. &lt;/p&gt;  &lt;p align="justify"&gt;That’ about customizing XML serialization. Isn’t it so easy to write custom XML serialization. Whenever you find a situation where you want to serialize your class into XML and you don’t want to leave it to the system and want to take full control of the serialization, you can implement IXmlSerializable interface to your class. &lt;/p&gt;  &lt;p align="justify"&gt;Try to know more&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-6082293817589361328?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/jIejy8JOKqRcu6GACIjXYfoOi6g/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jIejy8JOKqRcu6GACIjXYfoOi6g/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/jIejy8JOKqRcu6GACIjXYfoOi6g/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jIejy8JOKqRcu6GACIjXYfoOi6g/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/e0s_EmS1Sfc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/6082293817589361328/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2010/01/serialization-in-net-5.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/6082293817589361328?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/6082293817589361328?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/e0s_EmS1Sfc/serialization-in-net-5.html" title="Serialization in .NET – 5" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2010/01/serialization-in-net-5.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEcCQHgyeip7ImA9WxBVFEQ.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-798708444544849037</id><published>2010-01-02T17:56:00.000+05:30</published><updated>2010-02-18T17:57:41.692+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-18T17:57:41.692+05:30</app:edited><title>Displaying trademark, copyright etc in HTML</title><content type="html">&lt;p align="justify"&gt;This blog talks about a simple trick on how to show the trademark, copyright and registered symbols in HTML. To display trademark, copyright and registered symbols one needs to use “&amp;amp;trade;”, “&amp;amp;copy;” and “&amp;amp;reg;” respectively with any of the html tags. Below are some html and ASP.NET code samples.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Trademark symbol displayed in the title window.            &lt;br /&gt;&amp;lt;title&amp;gt;My Company &amp;amp;trade;&amp;lt;/title&amp;gt;             &lt;br /&gt;//Copyright symbol in a table header             &lt;br /&gt;&amp;lt;table width=&amp;quot;100px&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;tr&amp;gt;&amp;lt;th&amp;gt; Heading &amp;amp;copy;&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;             &lt;br /&gt;&amp;lt;/table&amp;gt;             &lt;br /&gt;//Registered symbol in a ASP.NET textbox             &lt;br /&gt;&amp;lt;asp:TextBox ID=&amp;quot;TextBox1&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;&amp;amp;reg;&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;//Trademark, copyright and registered symbol in a ASP.NET label control             &lt;br /&gt;&amp;lt;asp:Label ID=&amp;quot;Label2&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;&amp;amp;trade; &amp;amp;copy; &amp;amp;reg;&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Try to know more.&lt;/p&gt;  &lt;p&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-798708444544849037?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/AK3dywuoZjY05av6u687ZyTOKcA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/AK3dywuoZjY05av6u687ZyTOKcA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/AK3dywuoZjY05av6u687ZyTOKcA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/AK3dywuoZjY05av6u687ZyTOKcA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/rpx9yo9wy6s" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/798708444544849037/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2010/01/displaying-trademark-copyright-etc-in.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/798708444544849037?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/798708444544849037?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/rpx9yo9wy6s/displaying-trademark-copyright-etc-in.html" title="Displaying trademark, copyright etc in HTML" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2010/01/displaying-trademark-copyright-etc-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEABQHY8eCp7ImA9WxBSFUo.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-5825949979877399007</id><published>2009-12-23T19:23:00.000+05:30</published><updated>2009-12-23T19:29:11.870+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-23T19:29:11.870+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Compare validator" /><category scheme="http://www.blogger.com/atom/ns#" term="Validators" /><category scheme="http://www.blogger.com/atom/ns#" term="Asp.Net" /><category scheme="http://www.blogger.com/atom/ns#" term="Data type checking" /><title>Data type validation using validation control</title><content type="html">&lt;p align="justify"&gt;While developing ASP.NET application you would have come across scenarios where you would like to validate whether the value entered is of a particular data type. The options available with an ASP.NET developer is to write javascript functions to check whether the entered value complies with a data type or make the page postback and check the data type in server side. These two options are time consuming. Is there any simple way of doing this? The answer is to the question is “YES”. One can make use of compare validator to accomplish the task very easily. Lets see how.&lt;/p&gt;  &lt;p align="justify"&gt;To compare whether the entered value is of a particular data type one should make sure that the “Operator” property of compare validator should be “DataTypeCheck” and the “Type” property should be the data type you want to check. These two things are the only requirement to validate the data type. Compare validator supports validation for Currency, Date, Double, Integer and String data type. Below is a sample code which tries to check all these data types.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;Enter string: &amp;lt;asp:TextBox ID=&amp;quot;txtString&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;           &lt;br /&gt;&lt;font color="#008000"&gt;&amp;lt;asp:CompareValidator ID=&amp;quot;cmpVldString&amp;quot; runat=&amp;quot;server&amp;quot; ErrorMessage=&amp;quot;Not a valid string.&amp;quot; ControlToValidate=&amp;quot;txtString&amp;quot;             &lt;br /&gt;Type=&amp;quot;String&amp;quot; Operator=&amp;quot;DataTypeCheck&amp;quot;&amp;gt; &amp;lt;/asp:CompareValidator&amp;gt;&lt;/font&gt;&amp;lt;br /&amp;gt;            &lt;br /&gt;Enter integer: &amp;lt;asp:TextBox ID=&amp;quot;txtInt&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt; &amp;lt;/asp:TextBox&amp;gt;            &lt;br /&gt;&lt;font color="#008000"&gt;&amp;lt;asp:CompareValidator ID=&amp;quot;CompareValidator1&amp;quot; runat=&amp;quot;server&amp;quot; ErrorMessage=&amp;quot;Not a valid Integer.&amp;quot; ControlToValidate=&amp;quot;txtInt&amp;quot;             &lt;br /&gt; Type=&amp;quot;Integer&amp;quot; Operator=&amp;quot;DataTypeCheck&amp;quot;&amp;gt; &amp;lt;/asp:CompareValidator&amp;gt;&lt;/font&gt;&amp;lt;br /&amp;gt;            &lt;br /&gt;Enter double: &amp;lt;asp:TextBox ID=&amp;quot;txtDouble&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt; &amp;lt;/asp:TextBox&amp;gt;            &lt;br /&gt;&lt;font color="#008000"&gt;&amp;lt;asp:CompareValidator ID=&amp;quot;CompareValidator2&amp;quot; runat=&amp;quot;server&amp;quot; ErrorMessage=&amp;quot;Not a valid double.&amp;quot; ControlToValidate=&amp;quot;txtDouble&amp;quot;             &lt;br /&gt; Type=&amp;quot;Double&amp;quot; Operator=&amp;quot;DataTypeCheck&amp;quot;&amp;gt; &amp;lt;/asp:CompareValidator&amp;gt;&lt;/font&gt;&amp;lt;br /&amp;gt;            &lt;br /&gt;Enter date: &amp;lt;asp:TextBox ID=&amp;quot;txtDate&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt; &amp;lt;/asp:TextBox&amp;gt;            &lt;br /&gt;&lt;font color="#008000"&gt;&amp;lt;asp:CompareValidator ID=&amp;quot;cmpVldDate&amp;quot; runat=&amp;quot;server&amp;quot; ErrorMessage=&amp;quot;Not a valid date.&amp;quot; ControlToValidate=&amp;quot;txtDate&amp;quot;             &lt;br /&gt; Type=&amp;quot;Date&amp;quot; Operator=&amp;quot;DataTypeCheck&amp;quot; &amp;gt; &amp;lt;/asp:CompareValidator&amp;gt;&lt;/font&gt;&amp;lt;br /&amp;gt;            &lt;br /&gt;Enter currency: &amp;lt;asp:TextBox ID=&amp;quot;txtCurrency&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt; &amp;lt;/asp:TextBox&amp;gt;            &lt;br /&gt;&lt;font color="#008000"&gt;&amp;lt;asp:CompareValidator ID=&amp;quot;cmpVldCurrency&amp;quot; runat=&amp;quot;server&amp;quot; ErrorMessage=&amp;quot;Not a valid currencys.&amp;quot; ControlToValidate=&amp;quot;txtCurrency&amp;quot; Type=&amp;quot;Currency&amp;quot; Operator=&amp;quot;DataTypeCheck&amp;quot; &amp;gt; &amp;lt;/asp:CompareValidator&amp;gt;&lt;/font&gt;&amp;lt;br /&amp;gt;            &lt;br /&gt;&amp;lt;asp:Button ID=&amp;quot;Button1&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Button&amp;quot; /&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code sample you can see that I am checking the datatype for string, Integer, date, double and finally currency. Isn’t it so easy to check the data type? &lt;/p&gt;  &lt;p align="justify"&gt;Now since we are validating date you would like to check against a particular format say “DD/MM/YYYY”. The default date format against which the compare validator checks the date is “YYYY/MM/DD”. One way specify the date format is to assign the page’ culture to the required culture which supports your date format. Say we want to check “DD/MM/YYYY” then we can set the following in the code behind.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;protected void Page_Load(object sender, EventArgs e)           &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;Page.Culture = &amp;quot;hi-IN&amp;quot;;&lt;/font&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code we are setting the culture for the page as Hindi-India which will accept the date in “DD/MM'/YYYY” format. Another way specifying the format in compare validator is to assign culture to the control which is specified in “ControlToValidate” property of the compare validator. For e.g. you have date picker assigned to a compare validator then assigning the culture or date format to the date picker will do the trick. As such there is no way to specify date format or culture to a compare validator. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you use compare validator to check currency data type keep in mind it doesn’t check for currency symbol compliance. &lt;/p&gt;  &lt;p align="justify"&gt;Try to know more…..&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-5825949979877399007?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/8jZQ6s2DOoeSO9AWvCYkwXd2VBI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8jZQ6s2DOoeSO9AWvCYkwXd2VBI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/8jZQ6s2DOoeSO9AWvCYkwXd2VBI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8jZQ6s2DOoeSO9AWvCYkwXd2VBI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/IT5hn1f0KK4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/5825949979877399007/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/12/data-type-validation-using-validation.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5825949979877399007?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5825949979877399007?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/IT5hn1f0KK4/data-type-validation-using-validation.html" title="Data type validation using validation control" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>1</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/12/data-type-validation-using-validation.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUYBQ3c6eyp7ImA9WxBSE0Q.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-7701753187293124289</id><published>2009-12-21T17:29:00.000+05:30</published><updated>2009-12-21T17:35:52.913+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-21T17:35:52.913+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="Asp.Net" /><category scheme="http://www.blogger.com/atom/ns#" term="GridView" /><title>Retrieving controls from an ASP.NET GridView control using javascript.</title><content type="html">&lt;p align="justify"&gt;In our day to day ASP.NET programming we would have used GridView for one or the other purpose. There would have been requirement to access the controls inside a GridView from javascript. Whenever I had to use the ASP.NET GridView control, nearly 95% of the time I had to access the controls inside the GridView using javascript for one or the other purpose. With this blog I wanted to show how easily you can retrieve controls rendered inside a GridView from javascript. I would like to highlight this by taking an age old design where you will have a checkbox in the header and another set of checkboxes in the column below. Checking the checkbox in the header would select all the checkboxes in the column and un-checking a single checkbox in the column would un-check the header checkbox. This strategy can be used to access any type of controls not only inside the GridView but other data rendering controls as well. Now lets see the code for the same.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:GridView ID=&amp;quot;gvChk&amp;quot; runat=&amp;quot;server&amp;quot; AutoGenerateColumns=&amp;quot;False&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BackColor=&amp;quot;White&amp;quot; BorderColor=&amp;quot;#DEDFDE&amp;quot; BorderStyle=&amp;quot;None&amp;quot; BorderWidth=&amp;quot;1px&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CellPadding=&amp;quot;4&amp;quot; ForeColor=&amp;quot;Black&amp;quot; GridLines=&amp;quot;Vertical&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; onrowdatabound=&amp;quot;gvChk_RowDataBound&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;RowStyle BackColor=&amp;quot;#F7F7DE&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Columns&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField HeaderText=&amp;quot;Sl. No&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EditItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;TextBox1&amp;quot; Text='&amp;lt;%#Eval(&amp;quot;No&amp;quot;) %&amp;gt;' runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/EditItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;Label1&amp;quot; runat=&amp;quot;server&amp;quot; Text='&amp;lt;%#Eval(&amp;quot;No&amp;quot;) %&amp;gt;'&amp;gt;&amp;lt;/asp:Label&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EditItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:CheckBox ID=&amp;quot;CheckBox1&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/EditItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:CheckBox ID=&amp;quot;CheckBox1&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:CheckBox ID=&amp;quot;chkHeader&amp;quot; onclick=&amp;quot;javascript:selectAllCheckboxInGrid(&amp;lt;% gvChk.ClientID; %&amp;gt;, this);&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; runat=&amp;quot;server&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/HeaderTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/Columns&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;FooterStyle BackColor=&amp;quot;#CCCC99&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;PagerStyle BackColor=&amp;quot;#F7F7DE&amp;quot; ForeColor=&amp;quot;Black&amp;quot; HorizontalAlign=&amp;quot;Right&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;SelectedRowStyle BackColor=&amp;quot;#CE5D5A&amp;quot; Font-Bold=&amp;quot;True&amp;quot; ForeColor=&amp;quot;White&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderStyle BackColor=&amp;quot;#6B696B&amp;quot; Font-Bold=&amp;quot;True&amp;quot; ForeColor=&amp;quot;White&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;AlternatingRowStyle BackColor=&amp;quot;White&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;lt;/asp:GridView&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The code is very simple with a GridView control having two columns. First column has a label control and second one has a checkbox control. The second column has a checkbox in the header as well. When checkbox in the header is clicked all the checkboxes in the column are selected using javascript. The javascript code to select all the checkboxes in the column are pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;function selectAllCheckboxInGrid(gridID, chk)            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var grid = document.getElementById(gridID);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Loop starts from 1 because the zeroth row is the header.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; for (var i=1; i&amp;lt; grid.rows.length; i++)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Getting the control in the second cell of the grid.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Second cell contains the checkbox.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var cntls = grid.rows[i].cells[1].getElementsByTagName('input');             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //If you are have only one control in the cell then             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //one can do away with the loop.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (var j=0; j &amp;lt; cntls.length; j++)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (cntls[j].type == &amp;quot;checkbox&amp;quot;)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cntls[j].checked = chk.checked;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above pasted javascript is straight forward. The javascript function takes two arguments, one is the GridView’ client id and the second is the checkbox in the GridView control’ header. The function starts by getting the grid control using the “getElementById” javascript method of the document object. After getting the GridView control, which is converted to a HTML table once rendered to the browser, we are looping through the rows collection starting from the first row. The zeroth row is the header so we don’t want the loop to start from the header. Starting from the 1st row we loop through the whole rows and get the controls in the second cell of the GridView using the “getElementsByTagName” method of the document object. Then we loop through the control array and check the type of the control and if it is a checkbox we set the checked property based on the checkbox control of the header cell passed as an argument to the javascript function. If you have only one control in the cell then you can directly use “cells[1].children[0].checked = chk.checked” so that you can avoid the loop. The above modified code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;function selectAllCheckboxInGrid(gridID, chk)            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var grid = document.getElementById(gridID);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Loop starts from 1 because the zeroth row is the header.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; for (var i=1; i&amp;lt; grid.rows.length; i++)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Getting the control in the second cell of the grid.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Second cell contains the checkbox.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#0000ff"&gt;var cntls = grid.rows[i].cells[1].children[0].checked = chk.checked             &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Next we have to have a function which will check the header checkbox if all the checkbox in the column are checked and if out of all the checked checkboxes anyone is unchecked then we need to uncheck the header checkbox. The simple function is fired when any of the checkboxes in the column are clicked. The javascript function is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;function checkUncheckHeaderCheckBox(gridID)           &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var grid = document.getElementById(gridID);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var checked = true;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Loop starts from 1 because the zeroth row is the header.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; for (var i=1; i&amp;lt; grid.rows.length; i++)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Getting the control in the second cell of the grid.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Second cell contains the checkbox.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var cntls = grid.rows[i].cells[1].getElementsByTagName('input');            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //If you are have only one control in the cell then             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //one can do away with the loop.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (var j=0; j &amp;lt; cntls.length; j++)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (cntls[j].type == &amp;quot;checkbox&amp;quot; &amp;amp;&amp;amp; !cntls[j].checked)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; checked = cntls[j].checked;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (!checked)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Get the header checkbox and assign its checked property.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; grid.rows[0].cells[1].children[0].checked = checked;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above function is pretty straight forward. The “GridView” control id is passed as argument. The function gets the “GridView” control using the “getElementById” function of the document object and loops through the rows starting from the first row leaving the zeroth row. Inside the loop the function retrieves the controls inside the second cell of each row and checks whether any one of the checkbox is unchecked. If so it assigns the “checked” variable as false and breaks the loop. Finally it sets the checked property of the checkbox in the header (zeroth row) to the value of “checked” variable. This function will take care of checking or unchecking the checkbox if all the checkbox in the column are checked or if any one checkbox in the checkbox column is unchecked respectively. &lt;/p&gt;  &lt;p align="justify"&gt;To fire the javascript function on the click of the checkboxes we need to add the javascript function to the “onclick” javascript event. To do this add the following in the “RowDataBound” event of the GridView control. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;protected void gvChk_RowDataBound(object sender, GridViewRowEventArgs e)           &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Check if the row is of header type.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (e.Row.RowType == DataControlRowType.Header)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Add the javascript function which gets called             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //when the checkbox in the header is clicked.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (e.Row.FindControl(&amp;quot;chkHeader&amp;quot;) as CheckBox).Attributes.Add(&amp;quot;onclick&amp;quot;, &amp;quot;javascript:selectAllCheckboxInGrid('&amp;quot; + gvChk.ClientID + &amp;quot;', this);&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; else if (e.Row.RowType != DataControlRowType.Footer)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Add the javascript function call which gets called            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //when the checkboxes in the column are clicked.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (e.Row.FindControl(&amp;quot;CheckBox1&amp;quot;) as CheckBox).Attributes.Add(&amp;quot;onclick&amp;quot;, &amp;quot;javascript:checkUncheckHeaderCheckBox('&amp;quot; + gvChk.ClientID + &amp;quot;');&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;With the above code we are ready to go. &lt;/p&gt;  &lt;p align="justify"&gt;The above technique using javascript can be used to retrieve any type of controls (textbox, dropdown, radio button, button, links, images, labels etc) from within the GridView control or any other ASP.NET data rendering controls. You need to make some small javascript changes according to your data bound controls layout. So tweak the code wherever necessary and start retrieving controls. &lt;/p&gt;  &lt;p align="justify"&gt;Try to know more…&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-7701753187293124289?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ytXzoUZ_RjUWsna-UUxn8dVMJfs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ytXzoUZ_RjUWsna-UUxn8dVMJfs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ytXzoUZ_RjUWsna-UUxn8dVMJfs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ytXzoUZ_RjUWsna-UUxn8dVMJfs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/VTnAhycS0ZY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/7701753187293124289/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/12/retrieving-control-in-aspnet-gridview.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/7701753187293124289?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/7701753187293124289?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/VTnAhycS0ZY/retrieving-control-in-aspnet-gridview.html" title="Retrieving controls from an ASP.NET GridView control using javascript." /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/12/retrieving-control-in-aspnet-gridview.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQASH06cSp7ImA9WxBTGEU.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-534952193493618713</id><published>2009-12-15T19:38:00.000+05:30</published><updated>2009-12-15T19:42:29.319+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-15T19:42:29.319+05:30</app:edited><title>Serialization in .NET - 4</title><content type="html">&lt;p align="justify"&gt;In my previous blogs on serialization we had looked at the different ways of serializing an objects in .NET. The links to my previous blogs are&lt;/p&gt;  &lt;p align="justify"&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/07/serialization-in-net-3.html" target="_blank"&gt;Soap Serialization&lt;/a&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/05/serialization-in-net-2.html" target="_blank"&gt;XML serialization&lt;/a&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/05/serialization-in-net-1.html" target="_blank"&gt;Binary serialization&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p align="justify"&gt;In this blog we will have a look at how to implement custom serialization using ISerializable interface. In my previous blogs on serialization we looked at ways of controlling serialization by way of applying various attributes to the class and its member variables. Applying attributes gives us some control over serialization but doesn’t provide full fledge control. Most of your day to day serialization requirement can be met with the attributes discussed in my previous articles. But if you are the one who wants to take full control over serialization, then ISerializable interface is there for you. Lets first see what is needed to implement ISerializable interface.&lt;/p&gt;  &lt;p align="justify"&gt;The ISerializable interface has got only one method, GetObjectData. In addition to implementing the GetObjectData the implementer should also take care to implement a special constructor to classes implementing ISerializable interface. The constructor is called by the system during the de-serialization process to reconstruct the serialized object. If by chance you forgot to implement the constructor, runtime error will be thrown when one tries to deserialize a class implementing ISerializable interface without the special constructor. The “System.Runtime.Serialization.SerializationException” exception message which is thrown at runtime is pasted below.&lt;/p&gt;  &lt;table bordercolor="#000000" bordercolordark="#ffffff" width="400" bgcolor="#ffffff" bordercolorlight="#ffffff" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p align="justify"&gt;&lt;font color="#ff0000"&gt;The constructor to deserialize an object of type 'ISerializableImplementation.[CLASSNAME]' was not found.&lt;/font&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Note: &lt;/strong&gt;“GetObjectData” will be executed only in the case of binary serialization. For custom serialization in XML and SOAP serialization you need to implement IXmlSerializable interface.&lt;/p&gt;  &lt;p align="justify"&gt;Now lets see and try to understand the GetObjectData and the special constructor.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;GetObjectData: &lt;/strong&gt;The ISerializable method has only one method to be implemented and the signature looks like this “&lt;font color="#004000"&gt;GetObjectData((SerializationInfo info, StreamingContext context)&lt;/font&gt;”. The method takes SerializationInfo object as the first argument. SerializationInfo class can be used to store the required data to serialize and deserialize an object. Its the responsibility of the implementer to fill the SerializationInfo class with the necessary data. The implementer can decide on which member variables to store and which ones he doesn’t want to save. While doing this care should be taken to see that sufficient information is serialized for the objects to be deserialized. You can use the “AddValue” overloaded methods to add values to the SerializationInfo class. StreamingContext, the second argument in the method can be used to check the source from which the serialization request has come. Use the StreamingContext.State to check the source. Usage of StreamingContext is given at the end of the blog.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Special constructor: &lt;/strong&gt;The signature of the special constructor is as follows “(SerializationInfo info, StreamingContext context)”. The paremeters for the constructor are also the same. So I will not spend time explaining these. The special constructor is executed when an object (whose class has implemented ISerializable interface) is deserialized. If the special constructor is not implemented the system will not throw any compile time error but it will throw a runtime exception. So make sure a class which implements ISerializable interface has an explicit implementation of the special constructor which takes SerializationInfo and StreaminContext as its constructor argument. The special constructor should be private or protected if you intent to inherit the class.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Things to note before you implement ISerializable interface&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Keep the following things in mind before you implement the ISerializable Interface.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div align="justify"&gt;The class should have the Serializable attribute applied to it. If the class is not marked with the Serializable attribute an exception of type “System.Runtime.Serialization.SerializationException” will be thrown with the following error message.&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;table bordercolor="#000000" bordercolordark="#ffffff" width="400" bgcolor="#ffffff" bordercolorlight="#ffffff" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p align="justify"&gt;&lt;font color="#ff0000"&gt;Type 'ISerializableImplementation.[CLASSNAME]' in Assembly 'ISerializableImplementation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.&lt;/font&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div align="justify"&gt;NonSerializable attribute should not be used as it will not have any effect.&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;Special constructor with the same argument as that of GetObjectData method must be implemented and should be private or protected.&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;If the ISerializable implemented class is derived from a base class which also implements ISerializable interface then the parent class’ GetObjectData and special constructor should be called before the child class’ function and special constructor.&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p align="justify"&gt;That’ enough about the ISerializable interface implementation, now you will be eager to see the real implementation. The implementation is also pretty easy and straight forward. As usual lets take the Car class and implement ISerializable interface to it. The code sample is pasted below.&lt;/p&gt;  &lt;table bordercolor="#000000" bordercolordark="#ffffff" width="400" bgcolor="#ffffff" bordercolorlight="#ffffff" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;using System;            &lt;br /&gt;using System.Collections.Generic;             &lt;br /&gt;using System.Linq;             &lt;br /&gt;using System.Text;             &lt;br /&gt;using System.Runtime.Serialization; &lt;/p&gt;          &lt;p&gt;namespace ISerializableImplementation            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[Serializable]              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; public class Car : ISerializable             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public Car()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {} &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;public Car(SerializationInfo info, StreamingContext context)              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Color = info.GetString(&amp;quot;CarColor&amp;quot;);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.CubicCentimeter = info.GetDouble(&amp;quot;CC&amp;quot;);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.ModelName = info.GetValue(&amp;quot;Model&amp;quot;, typeof(string)).ToString();               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string Color            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string ModelName             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public int Price             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public double CubicCentimeter             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #region ISerializable Members &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.AddValue(&amp;quot;CarColor&amp;quot;, this.Color);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.AddValue(&amp;quot;Model&amp;quot;, this.ModelName);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.AddValue(&amp;quot;CC&amp;quot;, this.CubicCentimeter);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #endregion            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code we have a special constructor and the GetObjectData method needed to implement custom serialization. GetObjectData method is called when a Car object is serialized and the special constructor is executed when the de-serialization of the Car object happens. Also if you notice, inside, GetObjectData method we are changing the property names, “Color” is changed to “CarColor”, “ModelName” is changed to “Model” and “CubicCentimeter” to CC. These are simple e.g. where we are taking control of serialization. Similar to this you can take control based on different scenarios. If you notice we have not included the “Price” in the serialization and deserialization process i.e. in GetObjectData and special constructor respectively. &lt;/p&gt;  &lt;p align="justify"&gt;Isn’t it so simple to implement custom serialization. Now lets see how to use the StreamingContext object in serialization and deserialization. “StreamingContext” has only two properties which can be of interest, they are “Context” and “State”. Context property can hold any type of object as the underlining type is an “System.Object” class. Context property can be used when you expect some sort of object to come from the caller when serialization is happening. State property tells us from which source the serialization request has been raised. Lets understand with an e.g.&lt;/p&gt;  &lt;table bordercolor="#000000" bordercolordark="#ffffff" width="400" bgcolor="#ffffff" bordercolorlight="#ffffff" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.AddValue(&amp;quot;CarColor&amp;quot;, this.Color);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.AddValue(&amp;quot;Model&amp;quot;, this.ModelName);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.AddValue(&amp;quot;CC&amp;quot;, this.CubicCentimeter);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //If the request comes from other sources then retrieve the price from the HashTable in the &amp;quot;Context&amp;quot;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // property of StreamingContext class.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff8000"&gt;if (context.State == StreamingContextStates.Other)              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.AddValue(&amp;quot;Price&amp;quot;, ((System.Collections.Hashtable)context.Context)[&amp;quot;Ferari Mondeno&amp;quot;].ToString());               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code I have pasted the modified “GetObjectData” method as the other structure of the Car class is same. What we have done in the method is we have added a checking to check whether the serialization request is coming from other sources, if it is then get the price value from the “Context” property of the StreamingContext object. If you see the code I am casting the object to Hashtable and retrieving the price by passing “Ferari Mondeno” string as key. Lets see how the serialization code for the above implementation should be called.&lt;/p&gt;  &lt;table bordercolor="#000000" bordercolordark="#ffffff" width="400" bgcolor="#ffffff" bordercolorlight="#ffffff" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Create a Hashtable object and add the necessary data.            &lt;br /&gt;&lt;font color="#ff8000"&gt;System.Collections.Hashtable ht = new System.Collections.Hashtable();              &lt;br /&gt;ht.Add(&amp;quot;Ferari Mondeno&amp;quot;, 324324);               &lt;br /&gt;ht.Add(&amp;quot;Lamborgini&amp;quot;, 56677);               &lt;br /&gt;&lt;/font&gt;//Create StreamingContext instance by passing the Hashtable             &lt;br /&gt;//as one of the arguments. Also we are setting the             &lt;br /&gt;//StreaminContextStates as Other.             &lt;br /&gt;&lt;font color="#ff8000"&gt;StreamingContext sc = new StreamingContext(StreamingContextStates.Other, ht);              &lt;br /&gt;&lt;/font&gt;//Pass the StreamingContext instance as one of the parameters             &lt;br /&gt;//of the binary formatter class.             &lt;br /&gt;&lt;font color="#ff8000"&gt;BinaryFormatter bf = new BinaryFormatter(null, sc);&lt;/font&gt;             &lt;br /&gt;using (System.IO.FileStream fs = new System.IO.FileStream(&amp;quot;Serialized.txt&amp;quot;, System.IO.FileMode.Create))             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Car car = new Car { ModelName = &amp;quot;Ferari Mondeno&amp;quot;, Color = &amp;quot;Ferari Red&amp;quot;, CubicCentimeter = 23584.33, Price = 234656 };             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //bf.Serialize(Console.OpenStandardOutput(), car);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; bf.Serialize(fs, car);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code you can see that we are first creating a Hashtable with the necessary data. Once that is done we are creating a StreamingContext instance by passing the Hashtable instance and setting the “StreamingConstextstate as “Other”. After this we are passing the StreamingContext instance as one of the arguments to the “BinaryFormatter” class’ constructor. As you would have noticed we are passing Hashtable instance into the “Context” property of the StreamingContext object. Using the Hashtable we are retrieving the price in “GetObjectData” method. &lt;/p&gt;  &lt;p align="justify"&gt;Now lets see how to deserialize object by making use of the StreamingContext instance. Based on the above changes we need to first change the constructor. Below is the constructor code.&lt;/p&gt;  &lt;table bordercolor="#000000" bordercolordark="#ffffff" width="400" bgcolor="#ffffff" bordercolorlight="#ffffff" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public Car(SerializationInfo info, StreamingContext context)            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.Color = info.GetString(&amp;quot;CarColor&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.CubicCentimeter = info.GetDouble(&amp;quot;CC&amp;quot;);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; this.ModelName = info.GetValue(&amp;quot;Model&amp;quot;, typeof(string)).ToString();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Checking whether the serialization request is             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //coming from file or from other sources.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (context.State == StreamingContextStates.File || context.State == StreamingContextStates.Other)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; this.Price = int.Parse(((System.Collections.Hashtable)context.Context)[&amp;quot;Ferari Mondeno&amp;quot;].ToString());             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code we have just added a if checking to check whether the request is coming from a file or from other sources, If so read the price value from the StreamingContext instance’ Context property. The “Context” property has a Hashatable having prices.&lt;/p&gt;  &lt;p align="justify"&gt;Now lets see the deserialization code for the above.&lt;/p&gt;  &lt;table bordercolor="#000000" bordercolordark="#ffffff" width="400" bgcolor="#ffffff" bordercolorlight="#ffffff" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;using (System.IO.FileStream fs = new System.IO.FileStream(&amp;quot;Serialized.txt&amp;quot;, System.IO.FileMode.Open))            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Create a Hashtable object and add the necessary data.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff8000"&gt;System.Collections.Hashtable ht = new System.Collections.Hashtable();              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ht.Add(&amp;quot;Ferari Mondeno&amp;quot;, 324324);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ht.Add(&amp;quot;Lamborgini&amp;quot;, 56677);               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; //Create StreamingContext instance by passing the Hashtable             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //as one of the arguments. Also we are setting the             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //StreaminContextStates as Other.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff8000"&gt;StreamingContext sc = new StreamingContext(StreamingContextStates.File, ht);              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; //Pass the StreamingContext instance as one of the parameters             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //of the binary formatter class.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff8000"&gt;BinaryFormatter bf = new BinaryFormatter(null, sc);              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; Car deserializedCar = (Car)bf.Deserialize(fs);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string nl = Environment.NewLine;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(nl + &amp;quot;Model name: {0}, &amp;quot; + nl + &amp;quot;envi Color: {1}, &amp;quot; + nl + &amp;quot;Cubic centimeter: {2}, &amp;quot; + nl + &amp;quot;Price: {3}&amp;quot;,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; deserializedCar.ModelName, deserializedCar.Color, deserializedCar.CubicCentimeter, deserializedCar.Price);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;There is nothing much to explain about the above code. Its pretty much straight forward and is similar to&amp;#160; the code where we have serialized the Car class. That’ it about custom serialization using ISerializable interface. One very important point which I would like to reiterate again is that the “GetObjectData” and the special constructor will be executed only for binary serialization. For XML and SOAP serialization “GetObjectData” and the special constructor will not be executed. To implement custom serialization in XML and SOAP serialization you have to implement IXmlSerializable interface. We will have a look at IXmlSerializable interface implementation in my next blog. Till then try to know more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-534952193493618713?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/l5vN3skViKt8_v3WarlE9q2xPq8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/l5vN3skViKt8_v3WarlE9q2xPq8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/l5vN3skViKt8_v3WarlE9q2xPq8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/l5vN3skViKt8_v3WarlE9q2xPq8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/csmJphhHPDg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/534952193493618713/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/12/serialization-in-net-4.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/534952193493618713?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/534952193493618713?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/csmJphhHPDg/serialization-in-net-4.html" title="Serialization in .NET - 4" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/12/serialization-in-net-4.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ak8CRXc4eyp7ImA9WxBTF0s.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-5794150866877277081</id><published>2009-12-11T22:18:00.003+05:30</published><updated>2009-12-14T11:04:24.933+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-14T11:04:24.933+05:30</app:edited><title>Showing image when validation fails using validation control</title><content type="html">&lt;p align="justify"&gt;As they say .NET is a big ocean and each day you come across something new. Even after working for six years on .NET every other day I learn something new. One such experience I would like to share with this blog. In this blog we will see how to show an image when there is a validation error.&lt;/p&gt;  &lt;p align="justify"&gt;Anyone who is a ASP.NET developer would have used validation control at some point or the other. So with this blog I would like to show a small technique whereby you can show an image next to a control whenever a validation fails using a validation control. The great thing about this approach is that, it is in-built into all the validation controls and to do this one need not write any server side code or client side code. Just placing an image tag in the “ErrorMessage” property of the validation control will do. Lets see the code.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;Required Numeric:            &lt;br /&gt;&amp;lt;asp:TextBox ID=&amp;quot;TextBox1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;lt;asp:RequiredFieldValidator ID=&amp;quot;RequiredFieldValidator1&amp;quot; runat=&amp;quot;server&amp;quot; ControlToValidate=&amp;quot;TextBox1&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff8000"&gt;ErrorMessage='&amp;lt;img src=images/error.gif /&amp;gt;'&lt;/font&gt; Display=&amp;quot;Dynamic&amp;quot;&amp;gt;&amp;lt;/asp:RequiredFieldValidator&amp;gt;             &lt;br /&gt;&amp;lt;asp:Button ID=&amp;quot;btnSubmit&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Submit&amp;quot; CausesValidation=&amp;quot;true&amp;quot; CommandName=&amp;quot;Edit&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;lt;asp:Button ID=&amp;quot;btnCancel&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Cancel&amp;quot; CommandName=&amp;quot;&amp;quot; CausesValidation=&amp;quot;False&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OnClick=&amp;quot;btnCancel_Click&amp;quot; /&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;If you see the above code I have placed a html image tag in the “ErrorMessage” property of the required field validator control. So when you click the submit button without entering any value in “TextBox1” an error image will pop next to the textbox. The screenshot is shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a href="http://lh3.ggpht.com/_QY7fSC4VqiA/SyXORhMTYWI/AAAAAAAAECM/ZDF-7JJ1IFg/s1600-h/image15.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_QY7fSC4VqiA/SyXOTqRprzI/AAAAAAAAECQ/ha_gH19Pv8M/image_thumb11.png?imgmax=800" width="384" height="74" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above image you can see an image, adjacent to the textbox, which was assigned to the “ErrorMessage” property of the validation control. In the above screenshot one thing which is missing is the description about the error. Wouldn’t it be nice to have a description about the error alongside the image? To achieve just that just place the error message before the html image tag as shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:RequiredFieldValidator ID=&amp;quot;RequiredFieldValidator1&amp;quot; runat=&amp;quot;server&amp;quot; ControlToValidate=&amp;quot;TextBox1&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff8000"&gt;ErrorMessage=&amp;quot;Required.&amp;lt;img src=images/imagesCAJD7BX6.jpg /&amp;gt;&amp;quot; &lt;/font&gt;Display=&amp;quot;Dynamic&amp;quot;&amp;gt;&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above you can notice that we have inserted “Required.” text before the image tag. The output of this is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a href="http://lh6.ggpht.com/_QY7fSC4VqiA/SyXOUn6yauI/AAAAAAAAECU/gnoaH4SjIRc/s1600-h/image19.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_QY7fSC4VqiA/SyXOWO37EdI/AAAAAAAAECY/x3PNRerVHHU/image_thumb13.png?imgmax=800" width="384" height="75" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Now we have a message alongside the image which gives as the detail of what went wrong. Now all this is fine, but some others would like to make use of the validation summary and show the error messages in the validation summary and error images along side the controls. Hmm, is that tricky? Not at all, its very easy. Lets see how?&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Error image along with validation summary control&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;To show error image alongside the control and validation message in validation summary you have to make some small adjustments in the above code. First thing, obviously, is to insert a validation summary control. Next, Place the image tag in between the starting and ending tags of the validation control. I know you will be little confused, lets clear your confusion with some code. Here is what you need to do. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:RequiredFieldValidator ID=&amp;quot;RequiredFieldValidator1&amp;quot; runat=&amp;quot;server&amp;quot; ControlToValidate=&amp;quot;TextBox1&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff8000"&gt;ErrorMessage=&amp;quot;Required.&amp;quot;&lt;/font&gt; Display=&amp;quot;Dynamic&amp;quot;&amp;gt;&lt;font color="#ff8000"&gt;&amp;lt;img src=images/imagesCAJD7BX6.jpg /&amp;gt;&lt;/font&gt;&amp;lt;/asp:RequiredFieldValidator&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code I have purposefully not included the validation summary control. You can find the whole code at the end of the blog. If you notice, we have placed the image tag in between the starting and ending tag of the required field validator. Also we have the error message in the “ErrorMessage” property. The output of the above code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a href="http://lh4.ggpht.com/_QY7fSC4VqiA/SyXOXeO9brI/AAAAAAAAECc/ntL2gbbbOcc/s1600-h/image23.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_QY7fSC4VqiA/SyXOX5rpBNI/AAAAAAAAECg/Vt-Trpbve8o/image_thumb15.png?imgmax=800" width="384" height="105" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;One thing to note here is that you have to set the “Display” property of the validation control to “Dynamic”.&lt;/strong&gt;&lt;/font&gt;&amp;#160; If you set the “Display” property to “None”, which we usually use with validation summary control, then the image will not be displayed. So its very important to set the “Display” property to “Dynamic'” for this to work. The whole code along with the validation summary is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:ValidationSummary ID=&amp;quot;ValidationSummary1&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;            &lt;br /&gt;Required Numeric:             &lt;br /&gt;&amp;lt;asp:TextBox ID=&amp;quot;TextBox1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;lt;asp:RequiredFieldValidator ID=&amp;quot;RequiredFieldValidator1&amp;quot; runat=&amp;quot;server&amp;quot; ControlToValidate=&amp;quot;TextBox1&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#ff8000"&gt;ErrorMessage=&amp;quot;Required.&amp;quot;&lt;/font&gt; Display=&amp;quot;Dynamic&amp;quot;&amp;gt;&lt;font color="#ff8000"&gt;&amp;lt;img src=images/imagesCAJD7BX6.jpg /&amp;gt;&lt;/font&gt;&amp;lt;/asp:RequiredFieldValidator&amp;gt;             &lt;br /&gt;&amp;lt;asp:Button ID=&amp;quot;btnSubmit&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Submit&amp;quot; CausesValidation=&amp;quot;true&amp;quot; CommandName=&amp;quot;Edit&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;lt;asp:Button ID=&amp;quot;btnCancel&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Cancel&amp;quot; CommandName=&amp;quot;&amp;quot; CausesValidation=&amp;quot;False&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OnClick=&amp;quot;btnCancel_Click&amp;quot; /&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Try to learn more&lt;/p&gt;  &lt;p&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-5794150866877277081?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XDyN591viPKBMiVvI4U5FqeQBGQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XDyN591viPKBMiVvI4U5FqeQBGQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XDyN591viPKBMiVvI4U5FqeQBGQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XDyN591viPKBMiVvI4U5FqeQBGQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/InIjj0hsRrY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/5794150866877277081/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/12/showing-image-when-validation-fails.html#comment-form" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5794150866877277081?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5794150866877277081?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/InIjj0hsRrY/showing-image-when-validation-fails.html" title="Showing image when validation fails using validation control" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>4</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/12/showing-image-when-validation-fails.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UGQ3o4eCp7ImA9WxBTF0w.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-1006437943871474175</id><published>2009-12-11T22:18:00.001+05:30</published><updated>2009-12-13T20:10:22.430+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-13T20:10:22.430+05:30</app:edited><title>Weird, CausesValidation not working.</title><content type="html">&lt;p align="justify"&gt;Recently I faced a weird situation where I had some validation controls, a cancel and submit buttons in a page . Submit button would validate the page controls for errors and cancel button would just postback and execute some code on the server without validating the page. The weird thing was once submit button is clicked and if there are any validation errors in the page and then one clicks the cancel button it won’t clear postback. If the user clicks the cancel button for the second time it would trigger postback. This happened even after setting the “CausesValidation” property of the cancel button to “false'”. The cancel button code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:Button ID=&amp;quot;btnRemove&amp;quot; CssClass=&amp;quot;buttonEnabled&amp;quot; CommandName=&amp;quot;Delete&amp;quot; runat=&amp;quot;server&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; meta:resourcekey=&amp;quot;btnRemove&amp;quot; CausesValidation=&amp;quot;false&amp;quot; /&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;One thing to note here is that the cancel button was inside a ListView control’ ItemTemplate section. So on click of the button&amp;#160; postback should happen and in the server some logic would be carried. The cancel would not work only when the user clicks the submit button and if there are any error and the user wishes to cancel the action then the cancel button would not post back. Only when the user clicks the cancel button for the second time, it would post back. If the user clicks the cancel button immediately after clicking the submit button and there are any validation errors displayed through the validation controls then the cancel button would not postback. This was weird. The solution was also simple. I need to set “Page_ValidationActive” javascript variable to false on the “OnClientClick” property of the cancel button. The modified cancel button HTML is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:Button ID=&amp;quot;btnRemove&amp;quot; CssClass=&amp;quot;buttonEnabled&amp;quot; CommandName=&amp;quot;Delete&amp;quot; runat=&amp;quot;server&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; meta:resourcekey=&amp;quot;btnRemove&amp;quot; CausesValidation=&amp;quot;false&amp;quot; &lt;font color="#ff8000"&gt;OnClientClick=&amp;quot;javascript:Page_ValidationActive = false;&amp;quot;&lt;/font&gt; /&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Setting the “Page_ValidationActive” to false disables all the validations in the page. So please do keep this in mind. Anyway this is what I wanted and my problem was solved.&lt;/p&gt;  &lt;p align="justify"&gt;Try to know more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-1006437943871474175?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/D64X-8Wv7I5spnNV6uw9yZT7XAU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/D64X-8Wv7I5spnNV6uw9yZT7XAU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/D64X-8Wv7I5spnNV6uw9yZT7XAU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/D64X-8Wv7I5spnNV6uw9yZT7XAU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/sAakXhuCy4w" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/1006437943871474175/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/12/weird-causesvalidation-not-working.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/1006437943871474175?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/1006437943871474175?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/sAakXhuCy4w/weird-causesvalidation-not-working.html" title="Weird, CausesValidation not working." /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/12/weird-causesvalidation-not-working.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0ABSH86fip7ImA9WxNaFUQ.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-619919607031521357</id><published>2009-11-30T21:12:00.001+05:30</published><updated>2009-11-30T21:12:39.116+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-11-30T21:12:39.116+05:30</app:edited><title>Updating a resource file.</title><content type="html">&lt;p align="justify"&gt;Recently one of our client had this requirement where they wanted to customize the texts appearing in the website as and when needed i.e. suppose they have a heading text in the login page which says “Login” and at some point they would like to change the text to something like “Login using email and password” without tinkering with the code. This is just an e.g., the client wanted each and every text appearing in the UI to be customizable. &lt;/p&gt;  &lt;p align="justify"&gt;The above requirement can be easily achieved using resource files. The text which are displayed can be placed in the resource files and whenever there is a change in the text just editing the resource file will take care of displaying the new text. Now you might be thinking what is the big deal in this, yes there is no rocket science involved in editing a resource file, just open any text editor and make the necessary modification. Asking the client to do that will be too much in the asking. As resource files are stored as XML, any syntax problem can bring down the whole application. So the next obvious solution is to provide a web page to edit the existing resource files. &lt;/p&gt;  &lt;p align="justify"&gt;By itself .NET doesn’t provide any class to edit and update resource files. There are classes to read and create resource files but nothing as such to update a resource files. If you want to simply read the content of a resource file you can make use of “System.Resources.ResourceReader” class and if you want to dynamically create a resource file then make use of “System.Resource.Resources.ResourceWriter” class. One of the disadvantages of using these classes is that they don’t have facility to fetch the comment part of a resource item.&lt;/p&gt;  &lt;p align="justify"&gt;My problem was not reading or creation of resource file but updating the content. Also I had to update the comments as well. So some googling helped me and gave me hints. People suggested that, treat a resource file as an XML and use the XML classes to read/update the resource file. With this hint and little bit of LINQ to XML I found the solution. Lets dive into the solution.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Reading resource file using LINQ&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Following is the HTML content for displaying the resource file content.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:GridView ID=&amp;quot;gvResourceEditor&amp;quot; runat=&amp;quot;server&amp;quot; AutoGenerateColumns=&amp;quot;False&amp;quot; OnRowCancelingEdit=&amp;quot;gvResourceEditor_RowCancelingEdit&amp;quot;           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OnRowEditing=&amp;quot;gvResourceEditor_RowEditing&amp;quot; OnRowUpdating=&amp;quot;gvResourceEditor_RowUpdating&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CellPadding=&amp;quot;0&amp;quot; BorderWidth=&amp;quot;0px&amp;quot; Width=&amp;quot;100%&amp;quot; meta:resourcekey=&amp;quot;gvResourceEditorResource1&amp;quot;&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Columns&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField HeaderText=&amp;quot;Key&amp;quot; meta:resourcekey=&amp;quot;TemplateFieldResource1&amp;quot;&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EditItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;lblKey&amp;quot; runat=&amp;quot;server&amp;quot; Text='&amp;lt;%# Eval(&amp;quot;Key&amp;quot;) %&amp;gt;' meta:resourcekey=&amp;quot;lblKeyResource1&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/EditItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;lblKey&amp;quot; runat=&amp;quot;server&amp;quot; Text='&amp;lt;%# Eval(&amp;quot;Key&amp;quot;) %&amp;gt;' meta:resourcekey=&amp;quot;lblKeyResource2&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle CssClass=&amp;quot;gridText tenXPadding bottomBorder rightBorder&amp;quot; Wrap=&amp;quot;True&amp;quot; VerticalAlign=&amp;quot;Top&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Width=&amp;quot;10%&amp;quot; /&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField HeaderText=&amp;quot;TEXT&amp;quot; meta:resourcekey=&amp;quot;TemplateFieldResource2&amp;quot;&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderStyle BorderWidth=&amp;quot;0&amp;quot; BorderColor=&amp;quot;White&amp;quot; BorderStyle=&amp;quot;None&amp;quot; /&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EditItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;txtValue&amp;quot; TextMode=&amp;quot;MultiLine&amp;quot; CssClass=&amp;quot;textBoxEnabled&amp;quot; runat=&amp;quot;server&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Text='&amp;lt;%# Bind(&amp;quot;Value&amp;quot;) %&amp;gt;' Width=&amp;quot;100%&amp;quot; Height=&amp;quot;150px&amp;quot; meta:resourcekey=&amp;quot;txtValueResource1&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/EditItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;lblValue&amp;quot; runat=&amp;quot;server&amp;quot; Text='&amp;lt;%# Bind(&amp;quot;Value&amp;quot;) %&amp;gt;' meta:resourcekey=&amp;quot;lblValueResource1&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle CssClass=&amp;quot;gridText tenXPadding rightBorder bottomBorder&amp;quot; Wrap=&amp;quot;True&amp;quot; Width=&amp;quot;35%&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; VerticalAlign=&amp;quot;Top&amp;quot; /&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField HeaderText=&amp;quot;COMMENTS&amp;quot; meta:resourcekey=&amp;quot;TemplateFieldResource3&amp;quot;&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderStyle BorderWidth=&amp;quot;0&amp;quot; BorderColor=&amp;quot;White&amp;quot; BorderStyle=&amp;quot;None&amp;quot; /&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EditItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;txtComment&amp;quot; TextMode=&amp;quot;MultiLine&amp;quot; CssClass=&amp;quot;textBoxEnabled&amp;quot; runat=&amp;quot;server&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Text='&amp;lt;%# Bind(&amp;quot;Comment&amp;quot;) %&amp;gt;' Width=&amp;quot;100%&amp;quot; Height=&amp;quot;150px&amp;quot; meta:resourcekey=&amp;quot;txtDescriptionResource1&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/EditItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;lblDescription&amp;quot; runat=&amp;quot;server&amp;quot; Text='&amp;lt;%# Bind(&amp;quot;Comment&amp;quot;) %&amp;gt;' meta:resourcekey=&amp;quot;lblDescriptionResource1&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle CssClass=&amp;quot;gridText tenXPadding rightBorder bottomBorder&amp;quot; Wrap=&amp;quot;True&amp;quot; Width=&amp;quot;35%&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; VerticalAlign=&amp;quot;Top&amp;quot; /&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:CommandField ButtonType=&amp;quot;Button&amp;quot; ItemStyle-CssClass=&amp;quot;tenXPadding bottomBorder&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ShowEditButton=&amp;quot;True&amp;quot; ControlStyle-CssClass=&amp;quot;buttonEnabled&amp;quot; ItemStyle-VerticalAlign=&amp;quot;Top&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; HeaderStyle-BorderColor=&amp;quot;White&amp;quot; HeaderStyle-BorderWidth=&amp;quot;0&amp;quot; HeaderStyle-BorderStyle=&amp;quot;None&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ItemStyle-Width=&amp;quot;20%&amp;quot; meta:resourcekey=&amp;quot;CommandFieldResource1&amp;quot;&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ControlStyle CssClass=&amp;quot;buttonEnabled&amp;quot;&amp;gt;&amp;lt;/ControlStyle&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderStyle BorderColor=&amp;quot;White&amp;quot; BorderWidth=&amp;quot;0px&amp;quot; BorderStyle=&amp;quot;None&amp;quot;&amp;gt;&amp;lt;/HeaderStyle&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle VerticalAlign=&amp;quot;Top&amp;quot; CssClass=&amp;quot;tenXPadding bottomBorder&amp;quot; Width=&amp;quot;20%&amp;quot;&amp;gt;&amp;lt;/ItemStyle&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:CommandField&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/Columns&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:GridView&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code you can see there is a GridView control which has its columns bound to various properties like “Key”, “Value” and “Comment”. This is a simple grid with “Edit”, “Update” and “Cancel” facility. In the above HTML make sure you have the “Key” field. “Key” field is used to search the resource file for the record to be updated. Make sure the “Key” field is not editable.&lt;/p&gt;  &lt;p align="justify"&gt; In the Page_Load event in the code behind the following code is used to read the content of a resource file and data bind the same to a GridView control.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;protected void Page_Load(object sender, EventArgs e)           &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160; if (!IsPostBack)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; LoadData();            &lt;br /&gt;} &lt;/p&gt;          &lt;p&gt;private void LoadData()           &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160; string path = Server.MapPath(&amp;quot;App_LocalResources/Default.aspx.resx&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160; //Loading the XML file using the Load method of XElement.            &lt;br /&gt;&amp;#160;&amp;#160; XElement resourceElements = XElement.Load(path);            &lt;br /&gt;&amp;#160;&amp;#160; //LINQ to load the resource items             &lt;br /&gt;&amp;#160;&amp;#160; gvResourceEditor.DataSource = (from resElem in resourceElements.Elements(&amp;quot;data&amp;quot;)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; select new            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Key = resElem.Attribute(&amp;quot;name&amp;quot;).Value,            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Value = HttpUtility.HtmlEncode(resElem.Element(&amp;quot;value&amp;quot;).Value),            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Comment = resElem.Element(&amp;quot;comment&amp;quot;) != null ?            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; HttpUtility.HtmlEncode(resElem.Element(&amp;quot;comment&amp;quot;).Value) : string.Empty            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; gvResourceEditor.DataBind();            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code you can see we are making use of XML to LINQ to read the contents from a resource file (Default.aspx.resx). Once we have loaded the resource file using the Load method of XElement class, in the next line we are making use of LINQ and creating &lt;a href="http://sandblogaspnet.blogspot.com/2009/06/anonymous-types-features-of-c-30-part-5.html" target="_blank"&gt;anonymous types&lt;/a&gt; and assigning the same as DataSource to the GridView control. &lt;/p&gt;  &lt;p align="justify"&gt;With that simple code we have read the contents from a resource file and displayed the same in a GridView with facility to “Edit”, “Update” and “Cancel”. Now lets see how to update a particular value in a resource file.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Updating a resource file&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;In the “RowUpdating” event of the GridView control we are doing the following to update the resource file.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;protected void gvResourceEditor_RowUpdating(object sender, GridViewUpdateEventArgs e)           &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string theFileName = Server.MapPath(&amp;quot;App_LocalResources/Default.aspx.resx&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Label lblKey = gvResourceEditor.Rows[e.RowIndex].FindControl(&amp;quot;lblKey&amp;quot;) as Label;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TextBox txtValue = gvResourceEditor.Rows[e.RowIndex].FindControl(&amp;quot;txtValue&amp;quot;) as TextBox;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TextBox txtComment = gvResourceEditor.Rows[e.RowIndex].FindControl(&amp;quot;txtComment&amp;quot;) as TextBox;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Loading the Resource file as an XML.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; XDocument xDoc = XDocument.Load(theFileName);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Filtering out key which matches currently updated record.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var elementToBeEdited = from xle in xDoc.Element(&amp;quot;root&amp;quot;).Elements(&amp;quot;data&amp;quot;) where xle.Attribute(&amp;quot;name&amp;quot;).Value == lblKey.Text select xle; &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; if (elementToBeEdited != null &amp;amp;&amp;amp; elementToBeEdited.Count() &amp;gt; 0)           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Updating the new value             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; elementToBeEdited.First().SetElementValue(&amp;quot;value&amp;quot;, HttpUtility.HtmlDecode(txtValue.Text)); &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Checking if there is a comment node, if no then adding a new comment node.           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (elementToBeEdited.First().Element(&amp;quot;comment&amp;quot;) == null &amp;amp;&amp;amp; !string.IsNullOrEmpty(txtComment.Text))            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; elementToBeEdited.First().Add(new XElement(&amp;quot;comment&amp;quot;, HttpUtility.HtmlDecode(txtComment.Text)));            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; else if (elementToBeEdited.First().Element(&amp;quot;comment&amp;quot;) != null &amp;amp;&amp;amp; !string.IsNullOrEmpty(txtComment.Text))            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //If comment node is present then updating the comment node.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; elementToBeEdited.First().SetElementValue(&amp;quot;comment&amp;quot;, HttpUtility.HtmlDecode(txtComment.Text));            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Finally saving the Resource file.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xDoc.Save(theFileName);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; gvResourceEditor.EditIndex = -1;           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; LoadData();            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code, first we are finding controls holding the values and then we are loading the resource file into the XDoc object. Once the resource file is loaded, in the next line we are making use of LINQ statement to select the element whose name attribute matches the “Key” field of the GridView. Once the element to be edited is filtered next we are updating the “value” node with the newly updated value. The value node in the resource file holds the text. &lt;/p&gt;  &lt;p align="justify"&gt;Then in the next line we are checking whether there is a comment node available, if not we are creating a new comment node by calling the “Add” method and passing a new “XElement” object as the parameter. If there is a comment node then we are updating the comment by calling the “SetElementValue” method and passing the necessary text as argument. Finally the updated resource file is saved by calling the “Save” method of the XDoc class.&lt;/p&gt;  &lt;p align="justify"&gt;Isn’t it so simple to update the resource file. With XML to LINQ the code has become terse and very easy. So with this you have seen how to read and update a resource file using LINQ. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Note: &lt;/strong&gt;If you are updating a resource file in a live system then keep in mind that all the logged in users session will expire and they will be logged out. &lt;/p&gt;  &lt;p align="justify"&gt;Try to konw more…&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-619919607031521357?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/J9CwQAXJJrIZHjKaXBuo5NCdX9Q/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/J9CwQAXJJrIZHjKaXBuo5NCdX9Q/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/J9CwQAXJJrIZHjKaXBuo5NCdX9Q/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/J9CwQAXJJrIZHjKaXBuo5NCdX9Q/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/jyzVri5WLzw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/619919607031521357/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/11/updating-resource-file.html#comment-form" title="11 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/619919607031521357?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/619919607031521357?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/jyzVri5WLzw/updating-resource-file.html" title="Updating a resource file." /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>11</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/11/updating-resource-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0cASHc5fyp7ImA9WxNXEEk.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-8142898011487724495</id><published>2009-09-27T14:14:00.001+05:30</published><updated>2009-09-27T14:14:09.927+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-27T14:14:09.927+05:30</app:edited><title>Calling multiple validation group</title><content type="html">&lt;p align="justify"&gt;Recently I had this requirement where in a single button click I need to call multiple Validation groups. The scenario is something like this. I had a ASP.NET page where there were 5 user controls and each had their own validation controls embedded in them. Each user control had its own validation controls and they had their own Validation group defined. All these user controls were placed in a single page which only had two button “Submit” and “Canel”. On click of the “Submit” button all the validation controls in the form should execute. The problem here is that there are five different validation groups in the same page. To top it all you can only assign one validation group to a button’ ValidationGroup property. If you don’t assign any validation group then on click of the submit button the validation controls which don’t have any validation group defined for them will only be fired. Validation controls which have validation group property defined will not get fired. This is the default behavior of validation controls. So how to call the validation controls of different groups? The answer lies in calling the “Page_ClientValidate” javascript method with the validation group name. &lt;/p&gt;  &lt;p align="justify"&gt;Lets try to understand how this all words with some code.The HTML code of the page looks something like this.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;table width=&amp;quot;100%&amp;quot;&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;TextBox1&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:RequiredFieldValidator             &lt;br /&gt;ID=&amp;quot;RequiredFieldValidator1&amp;quot; runat=&amp;quot;server&amp;quot;             &lt;br /&gt;ErrorMessage=&amp;quot;RequiredFieldValidator&amp;quot;             &lt;br /&gt;ValidationGroup=&amp;quot;Group1&amp;quot; ControlToValidate=&amp;quot;TextBox1&amp;quot;&amp;gt;             &lt;br /&gt;&amp;lt;/asp:RequiredFieldValidator&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/td&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;TextBox2&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:RequiredFieldValidator             &lt;br /&gt;ID=&amp;quot;RequiredFieldValidator2&amp;quot; runat=&amp;quot;server&amp;quot;             &lt;br /&gt;ErrorMessage=&amp;quot;RequiredFieldValidator&amp;quot;             &lt;br /&gt;ValidationGroup=&amp;quot;Group2&amp;quot; ControlToValidate=&amp;quot;TextBox2&amp;quot;&amp;gt;             &lt;br /&gt;&amp;lt;/asp:RequiredFieldValidator&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;TextBox3&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:RequiredFieldValidator ID=&amp;quot;RequiredFieldValidator3&amp;quot; runat=&amp;quot;server&amp;quot; ErrorMessage=&amp;quot;RequiredFieldValidator&amp;quot;             &lt;br /&gt;ValidationGroup=&amp;quot;Group3&amp;quot; ControlToValidate=&amp;quot;TextBox3&amp;quot;&amp;gt;             &lt;br /&gt;&amp;lt;/asp:RequiredFieldValidator&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;TextBox4&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:RequiredFieldValidator             &lt;br /&gt;ID=&amp;quot;RequiredFieldValidator4&amp;quot; runat=&amp;quot;server&amp;quot; ErrorMessage=&amp;quot;RequiredFieldValidator&amp;quot; ControlToValidate=&amp;quot;TextBox4&amp;quot;&amp;gt;             &lt;br /&gt;&amp;lt;/asp:RequiredFieldValidator&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;tr&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;btnSubmit&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Submit&amp;quot; OnClientClick=&amp;quot;javascript:return             &lt;br /&gt;validatePage();&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;btnCancel&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Cancel&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/td&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/tr&amp;gt;             &lt;br /&gt;&amp;lt;/table&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above markup you can make out that there are four required field validators and out of the four validators three have validation group property defined. In such a scenario when you click the submit button only the validator which doesn’t have validation group will be executed i.e. RequiredFiedlValidator4 will only be executed. Since there are more than one validation group assigning button’ ValidationGroup wont work as it will execute only validator controls which belong to the assigned validaiton group. Other validator controls belonging to other validation group won’t execute. Also there is no way to specify multiple validaiton group using the ValidationGroup property of the button control. &lt;/p&gt;  &lt;p align="justify"&gt;The way to solve this problem is to call Page_ClientValidate javascript function. Page_ClientValidate is a javascript function generated by ASP.NET. The function takes validation group name as an argument. The javascript function which gets called when the submit button is clicked is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;script language=&amp;quot;javascript&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;            &lt;br /&gt;function validatePage()             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Executes all the validation controls associated with group1 validaiton Group1.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var flag = Page_ClientValidate('Group1');             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (flag)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Executes all the validation controls associated with group1 validaiton Group2.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; flag = Page_ClientValidate('Group2');             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (flag)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Executes all the validation controls associated with group1 validaiton Group3.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; flag = Page_ClientValidate('Group3');             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (flag)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Executes all the validation controls which are not associated with any validation group.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; flag = Page_ClientValidate();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return flag;             &lt;br /&gt;}             &lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code you can see the Page_ClientValidate function is called four times, first by passing the first validation group name, second time passing on the second validation group name and so on. If you see the last call to Page_ClientValidate, it is called with an empty argument. When call Page_ClientValidate with no argume/validation group name it executes all the validation controls in the page which don’t have a validation group assigned to them. So if one wants to execute validation controls which are not having any validation group associated with them you can call the Page_ClientValidate without any parameter.&lt;/p&gt;  &lt;p align="justify"&gt;So using Page_ClientValidate javascript function one can execute all the validation controls in the page by passing the validation group name or without any validation group name.&lt;/p&gt;  &lt;p align="justify"&gt;Try to know more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-8142898011487724495?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/-T-hYtdql-8wly9fJYBq20sZVD8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-T-hYtdql-8wly9fJYBq20sZVD8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/-T-hYtdql-8wly9fJYBq20sZVD8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-T-hYtdql-8wly9fJYBq20sZVD8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/J2gDx_uc_xQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/8142898011487724495/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/09/calling-multiple-validation-group.html#comment-form" title="11 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/8142898011487724495?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/8142898011487724495?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/J2gDx_uc_xQ/calling-multiple-validation-group.html" title="Calling multiple validation group" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>11</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/09/calling-multiple-validation-group.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0IFSXc7fSp7ImA9WxNQFEk.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-7365977729599917220</id><published>2009-09-19T22:37:00.001+05:30</published><updated>2009-09-20T16:48:38.905+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-20T16:48:38.905+05:30</app:edited><title>Some important .NET functions. Part – 1</title><content type="html">&lt;p align="justify"&gt;.NET is a big ocean. Each day when I work I find one or the other function which are very useful in day to day programming. So I thought I will start a series of blog on some .NET function which I feel&amp;#160; are very useful. Here is the first list of function.&lt;/p&gt;  &lt;p align="justify"&gt;Recently while working in a ASP.NET project I wanted to make use of colors given by the designers in C# code behind file and wanted to change the color of ASP.NET controls. You will be thinking what is so hard in this. There is nothing much other than the color was given in hexadecimal value, like #657468, #AA3212 etc, and .NET controls only understand System.Drawing.Color structure values. So there is my problem. I need to convert the hexadecimal values to System.Drawing.Color values. Luckily I didn’t had to break my head for solving this problem, .NET provides a class just to solve my problem. The class is called ColorTranslator. The method which helped me is FromHtml. Code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;DropDownList1.BackColor = System.Drawing.ColorTranslator.FromHtml(&amp;quot;#345678&amp;quot;);            &lt;br /&gt;DropDownList1.BorderColor = System.Drawing.ColorTranslator.FromHtml(&amp;quot;Yellow&amp;quot;);&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The FromHtml method can take the name of the color as well as the hexadecimal value/HTML value. Some other methods of the class are listed below.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;ColorTranslator.ToHtml(Color theColor): &lt;/strong&gt;This is just the opposite of FromHtml i.e. it converts the color to HTML values. So when one wants to convert color to HTML color values/Hexadecimal color value one can make use of the ToHtml method. Sample code is pasted below. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;DropDownList1.BackColor =            &lt;br /&gt;System.Drawing.ColorTranslator.FromHtml(&amp;quot;#ff5678&amp;quot;);            &lt;br /&gt;DropDownList1.BorderColor =             &lt;br /&gt;System.Drawing.ColorTranslator.FromHtml(&amp;quot;Yellow&amp;quot;);            &lt;br /&gt;Console.WriteLIne(System.Drawing.ColorTranslator.ToHtml            &lt;br /&gt;(DropDownList1.BackColor));//Output - #ff5678            &lt;br /&gt;Console.WriteLine(System.Drawing.ColorTranslator.ToHtml            &lt;br /&gt;(DropDownList1.BorderColor));//Output - Yellow&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;ColorTranslator.ToWin32(Color c) and ColorTranslator.FromWin32(int w32): &lt;/strong&gt;ToWin32 converts the Color object to the equivalent windows color value whereas FromWin32 does just the opposite i.e. converts a Win32 color value to a System.Drawing.Color object. Sample codes are pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;Console.WriteLine(System.Drawing.ColorTranslator.ToWin32           &lt;br /&gt;(System.Drawing.Color.Red));//Output – 255.            &lt;br /&gt;System.Drawing.Color color =             &lt;br /&gt;System.Drawing.ColorTranslator.FromWin32(255);&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;ColorTranslator.ToOLE(Color col) and ColorTranslator.FromOLE(int oleValue): ToOLE method converts Color object to OLE equivalent color value and FromOLE converts OLE color value to System.Drawing.Color object.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;My ever useful System.IO.Path class&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Over my career the class which is mostly used by me is the System.IO.Path class. Though the class has got only 15 or so static methods I use this class a lot while working with file paths. Let me first start with my favorite Combine method of the Path class which I use the most.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.Combine(string path1, string path2): &lt;/strong&gt;The combine method combines two paths and returns you the combined path. The method takes care to add the necessary directory separator before combining the two paths. Also the combine method can be used to combine paths with wild characters like *.pdf or sa*.txt etc. If wild characters are used the combine method takes them as search criteria and doesn’t throw any error. E.g pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;System.IO.Path.Combine(Server.MapPath(&amp;quot;.&amp;quot;), &amp;quot;images&amp;quot;)           &lt;br /&gt;//Output: &amp;quot;C:\\aaa\\bb\\WebSite1\\images&amp;quot;            &lt;br /&gt;System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath            &lt;br /&gt;(&amp;quot;.&amp;quot;), &amp;quot;images&amp;quot;), &amp;quot;*.gif&amp;quot;)            &lt;br /&gt;//Output: &amp;quot;C:\\aaa\\bb\\WebSite1\\images\\*.gif&amp;quot;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.GetExtension(string thePath): &lt;/strong&gt;The GetExtension method returns the extension along with “.” from the path argument passed. So if you want to retrieve only extension from a file path just pass the file path to the GetExtension method and there you go you will have your extension. GetExtension method returns an empty string if the path doesn’t have any extension, also it returns null if the path argument is null. E.g. pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath           &lt;br /&gt;(&amp;quot;.&amp;quot;),&amp;#160; &amp;quot;images&amp;quot;), &amp;quot;*.gif&amp;quot;);            &lt;br /&gt;Console.WriteLine(System.IO.Path.GetExtension(filePath));            &lt;br /&gt;//Output: .gif&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.GetFileName(string thePath): &lt;/strong&gt;This method returns the filename with extension from the specified path argument. Returns an empty string if there are no file name. Sample code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath         &lt;br /&gt;(&amp;quot;.&amp;quot;), &amp;quot;images&amp;quot;), &amp;quot;tick.gif&amp;quot;);          &lt;br /&gt;Console.WriteLine(System.IO.Path.GetFileName          &lt;br /&gt;(filePath));//Output: tick.gif&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.ChangeExtension(string filePath, string newExt): &lt;/strong&gt;This method changes the extension of the file. The method takes two arguments, filename with extension along with/without path&amp;#160; and second one is the new extension with “.” which needs to be applied. If period is not there in the new extension argument then the method adds one. If you just wants to remove the extension then just pass a null value as the new extension parameter. Sample code is given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath         &lt;br /&gt;(&amp;quot;.&amp;quot;), &amp;quot;images&amp;quot;), &amp;quot;tick.gif&amp;quot;);          &lt;br /&gt;Console.WriteLine(System.IO.Path.ChangeExtension          &lt;br /&gt;(filePath, &amp;quot;jpg&amp;quot;));//Output: C:\\aaa\\bb\\WebSite1          &lt;br /&gt;\\images\\tick.jpg&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.GetFileNameWithoutExtension(string filePath): &lt;/strong&gt;Function returns the file name without extension. E.g. is shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;string filePath = System.IO.Path.Combine(System.IO.Path.Combine(Server.MapPath         &lt;br /&gt;(&amp;quot;.&amp;quot;), &amp;quot;images&amp;quot;), &amp;quot;tick.gif&amp;quot;);          &lt;br /&gt;Console.WriteLine(System.IO.Path.ChangeExtension          &lt;br /&gt;(filePath, &amp;quot;jpg&amp;quot;));//Output: tick&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.GetRandomFileName(): &lt;/strong&gt;Returns a random string. The string is generated using cryptographic principles. The string can be used as folder or file name. Sample code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;Console.WriteLine(System.IO.Path.GetRandomFileName())//Output: x4zxg224.hva&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.GetTempFileName(): &lt;/strong&gt;Function returns a string with a file name along with temporary folder path. The filename returned has a .tmp extension. The method creates a file with .tmp in the temporary folder and then returns the filename along with the temprorary folder path where the file is created. Sample code with output is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;Console.WriteLine(System.IO.Path.GetTempFileName())//Output: C:\\Documents and Settings\\sandeep\\Local Settings\\Temp\\tmp20C.tmp&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.GetTempPath(): &lt;/strong&gt;GetTempPath function returns the system’ temporary folder path. Sample code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;Console.WriteLine(System.IO.Path.GetTempPath());//Output: C:\\Documents and Settings\\sandeep\\Local Settings\\Temp\\&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Path.HasExtension(string filePath): &lt;/strong&gt;This function checks whether the file path passed has a file extension. If extension is found the function returns true else false.&lt;/p&gt;  &lt;p align="justify"&gt;So these are some of my favorite and useful functions. Will publish some more over the course of time till then try to know more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-7365977729599917220?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zz9bA_UZLD5VZK8AWfXX4u7jUoA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zz9bA_UZLD5VZK8AWfXX4u7jUoA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zz9bA_UZLD5VZK8AWfXX4u7jUoA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zz9bA_UZLD5VZK8AWfXX4u7jUoA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/zWwRA4mdX9s" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/7365977729599917220/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/09/some-important-net-functions-part-1.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/7365977729599917220?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/7365977729599917220?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/zWwRA4mdX9s/some-important-net-functions-part-1.html" title="Some important .NET functions. Part – 1" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/09/some-important-net-functions-part-1.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0AHQn09eyp7ImA9WxNSF00.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-7672541349818865325</id><published>2009-08-31T14:37:00.001+05:30</published><updated>2009-08-31T14:38:53.363+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-31T14:38:53.363+05:30</app:edited><title>Saving files from one website to another website.</title><content type="html">&lt;p align="justify"&gt;This blog is based on my recent requirement where I had to save some image files from one website to another website. Lets see the requirement and the solution for the requirement.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Requirement&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;The requirement is as follows. The user will select a file which needs to be uploaded using a website (UploadFilesWebsite) and these selected files will be saved in another website (SaveFilesWebsite). Cross website posting of data. We all know it’ very easy to save files within the same site by making use of a file upload control and calling its SaveAs method. E.g. is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;fileUploadCntl.SaveAs(System.IO.Path.Combine(Server.MapPath(&amp;quot;.&amp;quot;), fileUploadCntl.FileName))&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;But in my case it was different, the file needs to be saved in a different website from the one from where file upload is done.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Solution&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;The problem was solved by making use of HttpWebRequest and HttpWebResponse object. Below is steps which are needed to solve this problem.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div align="justify"&gt;First create an ASPX file which will be capable of receiving any type of file stream and saving this stream to the required folder. This file should be created/placed in the website where you want to save the files, in my case it is “SaveFiles” website.&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;Create a HttpWebRequest object and pump the file content to the RequestStream of the HttpWebRequest object.&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;Get the response using the GetResponse method of the HttpWebRequest object. GetResponse will return a HttpWebResponse object.&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;Get the response stream from the HttpWebResponse object by executing the GetResponseStream method.&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p align="justify"&gt;These are the steps which you need to follow to post file contents from one website to another. Lets see each one of these one by one.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Create an aspx file capable of saving request stream.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;The first step is to create an aspx file which will be capable of saving a request stream. This file should be created and saved in the web site where you plan to save your file. This file will receive requests from other website for saving files in the website. So we will create this file in “SaveFiles” website and we will name it as SaveFile.asxp. After creating and saving the file go to the HTML source view of the file. It will look something like this.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;%@ Page Language=&amp;quot;C#&amp;quot; AutoEventWireup=&amp;quot;true&amp;quot;&amp;#160; CodeFile=&amp;quot;SaveFile.aspx.cs&amp;quot; Inherits=&amp;quot;SaveFile&amp;quot; %&amp;gt; &lt;/p&gt;          &lt;p&gt;&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;&lt;a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;"&gt;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;&lt;/a&gt;&amp;gt; &lt;/p&gt;          &lt;p&gt;&amp;lt;html xmlns=&amp;quot;&lt;a href="http://www.w3.org/1999/xhtml&amp;quot;"&gt;http://www.w3.org/1999/xhtml&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;lt;head runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;             &lt;br /&gt;&amp;lt;/head&amp;gt;             &lt;br /&gt;&amp;lt;body&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/div&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/form&amp;gt;             &lt;br /&gt;&amp;lt;/body&amp;gt;             &lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Remove the HTML source from the file leaving only the Page attribute. The page should look something like this.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&amp;lt;%@ Page Language=&amp;quot;C#&amp;quot; AutoEventWireup=&amp;quot;true&amp;quot;&amp;#160; CodeFile=&amp;quot;SaveFile.aspx.cs&amp;quot; Inherits=&amp;quot;SaveFile&amp;quot; %&amp;gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Now lets go to the codebehind and do some coding to save the request stream. The codebehind (SaveFile.aspx.cs) file of SaveFile looks something like this.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;using System;            &lt;br /&gt;using System.Configuration;             &lt;br /&gt;using System.Data;             &lt;br /&gt;using System.Linq;             &lt;br /&gt;using System.Web;             &lt;br /&gt;using System.Web.Security;             &lt;br /&gt;using System.Web.UI;             &lt;br /&gt;using System.Web.UI.HtmlControls;             &lt;br /&gt;using System.Web.UI.WebControls;             &lt;br /&gt;using System.Web.UI.WebControls.WebParts;             &lt;br /&gt;using System.Xml.Linq; &lt;/p&gt;          &lt;p&gt;public partial class SaveFile : System.Web.UI.Page            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; protected void Page_Load(object sender, EventArgs e)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;try              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Retrieving the file name from the headers in the request. */               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string fileName = System.IO.Path.Combine(Server.MapPath(&amp;quot;.&amp;quot;), Request.Headers[&amp;quot;FileName&amp;quot;].ToString());               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; using (System.IO.FileStream fileStream = System.IO.File.Create(fileName))               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Getting stream from the Request object.*/               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; using (System.IO.Stream stream = Request.InputStream)               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int byteStreamLength = (int)stream.Length;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; byte[] byteStream = new byte[byteStreamLength];               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Reading the stream to a byte array.*/               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; stream.Read(byteStream, 0, byteStreamLength);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Writing the byte array to the harddisk.*/               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fileStream.Write(byteStream, 0, byteStreamLength);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Response.Clear();               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Writing the status to the response.*/               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Response.Write(&amp;quot;File successfully saved&amp;quot;);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch (Exception ex)               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Writing the error message to the response stream.*/               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Response.Clear();               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Response.Write(&amp;quot;Error: &amp;quot; + ex.Message);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above code one can see we are first retrieving the filename from Request object’ Headers property. We will shortly see how to send info in headers of a request. After getting the filename we are opening a file stream to flush the content to the hard disk. Once the file stream is successfully created, next, we are creating a stream by getting the InputStream of the Request object. The InputStream property of the Request object will hold the content which needs to be saved. In the next line we are reading the stream content to a byte array and finally passing the byte array to the FileStream object and writing/saving to a file. Once the file is successfully saved we are writing the status to the Response after clearing the Response. Also if there is any exception thrown while the file is saved the same is written to the Response.&lt;/p&gt;  &lt;p align="justify"&gt;So that’ about the page which will save the content in the desired filename. So now we will move to our next steps like creating the HttpWebRequest object and passing the file content to the SaveFile.aspx file to save it in the website.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Create an aspx page to upload the images.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;The file which we will be creating will have the capability to upload files from the user’ hard disk and will send these file contents to the SaveFile.aspx page. The UploadFile.aspx will be created in a separate website/virtual directory called “UploadFiles” website. This aspx page will have a FileUpload control and a save button. So here is the HTML source of UploadFile.aspx.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;%@ Page Language=&amp;quot;C#&amp;quot; AutoEventWireup=&amp;quot;true&amp;quot;&amp;#160; CodeFile=&amp;quot;FileUpload.aspx.cs&amp;quot; Inherits=&amp;quot;FileUpload&amp;quot; %&amp;gt; &lt;/p&gt;          &lt;p&gt;&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;&lt;a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;"&gt;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;&lt;/a&gt;&amp;gt; &lt;/p&gt;          &lt;p&gt;&amp;lt;html xmlns=&amp;quot;&lt;a href="http://www.w3.org/1999/xhtml&amp;quot;"&gt;http://www.w3.org/1999/xhtml&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;lt;head runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;             &lt;br /&gt;&amp;lt;/head&amp;gt;             &lt;br /&gt;&amp;lt;body&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/div&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;&amp;lt;asp:Label ID=&amp;quot;lblMessage&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;&amp;lt;br /&amp;gt;              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:FileUpload ID=&amp;quot;fileUpload&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;&amp;lt;br /&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;btnSave&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Save Files&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; onclick=&amp;quot;btnSave_Click&amp;quot; /&amp;gt;               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/form&amp;gt;             &lt;br /&gt;&amp;lt;/body&amp;gt;             &lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the HTML you can see that we have an FileUpload control and a save button with an OnClick event handler. So that’ our simple UploadFile page. Now lets see the real action i.e. the place where the code to upload files and sending the content of these files to the SaveFile.aspx in “SaveFiles” website happen. So here is the click event of the save button where all the action takes place.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;protected void btnSave_Click(object sender, EventArgs e)            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Creating the WebRequest object using the URL of SaveFile.aspx.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Net.HttpWebRequest webRequest =             &lt;br /&gt;(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create            &lt;br /&gt;(&amp;quot;&lt;a href="Http://Localhost/SaveFiles/SaveFile.aspx&amp;quot;);"&gt;Http://Localhost/SaveFiles/SaveFile.aspx&amp;quot;);&lt;/a&gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; webRequest.Method = &amp;quot;POST&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; webRequest.KeepAlive = false;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Assigning the content type from the FileUpload control.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; webRequest.ContentType = fileUpload.PostedFile.ContentType;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Creating the Header object.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; headers.Add(&amp;quot;FileName&amp;quot;, fileUpload.FileName);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Adding the header to the WebRequest object.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; webRequest.Headers = headers;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Getting the request stream by making use of the GetRequestStream method of WebRequest object.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; using (System.IO.Stream stream = webRequest.GetRequestStream())//Filling request stream with image stream.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Writing the FileUpload content to the request stream.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; stream.Write(fileUpload.FileBytes, 0, fileUpload.FileBytes.Length);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Creating a WebResponse object by calling the GetResponse method of WebRequest object.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; using (System.Net.HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse())             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /*Retrieving the response stream from the WebResponse object by calling the GetResponseStream method.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; using (System.IO.StreamReader sr = new System.IO.StreamReader(webResponse.GetResponseStream()))             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lblMessage.Text = sr.ReadToEnd();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code first I am creating a WebRequest object by passing the URL of the page which will handle the saving mechanism. Once the WebRequest object is created next I am assigning the Method as POST. This is a very important step. If you don’t assign the method property it will default to “GET”. If you use “GET” as the method to send data then you will receive System.Net.ProtocolViolationException with the following message.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;font color="#008000"&gt;&amp;quot;Cannot send a content-body with this verb-type.&amp;quot;&lt;/font&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Once the method is set to “POST” we are setting the KeepAlive property to false. KeepAlive property will be responsible for maintaining a live connection. The next line we are setting the ContentType of the request. The content type can be easily retrieved by calling the ContentType property from PostedFile property of the FileUpload Control. Next line we are creating a WebHeaderCollection object to pass additional information in the headers of the request. You can see we are passing the file name as one of the header value. These header details are read in SaveFile.aspx. So if you want send some extra information you can do that using WebHeaderCollection. In the next few lines we are retrieving the request stream using the GetRequestStream method and then writing the contents of the selected file to request stream using the Write method of the Stream object. To pump the content of the chosen file we are making use of the FileBytes property of the FileUpload control. One can note that we are making use of the “&lt;a href="http://sandblogaspnet.blogspot.com/2009/04/using-statement-in-net.html" target="_blank"&gt;using&lt;/a&gt;” statement quite a lot. To know more about the usefulness of the “&lt;a href="http://sandblogaspnet.blogspot.com/2009/04/using-statement-in-net.html" target="_blank"&gt;using&lt;/a&gt;” statement you can refer my blog on using statement &lt;a href="http://sandblogaspnet.blogspot.com/2009/04/using-statement-in-net.html" target="_blank"&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p align="justify"&gt;Once the FileUpload contents are pumped to the request stream of the HttpRequest object in the next line I am creating the HttpWebResponse using the GetResponse method of the HttpWebRequest object. So what GetResponse does is that it sends a request to the Url (savefile.aspx) specified while creating the HttpWebRequest object. When the HttpWebRequest object sends request to the url its similar to the way we access url through the browser. So the whole life cycle of the page executes and response is saved in the form of HttpWebResponse object. After getting the HttpWebResponse object we are reading what output was returned by the page using the StreamReader object’ ReadToEnd method. You can retrieve the StreamReader object by calling the GetResponseStream method of the WebRequest object. The response which I am getting is directly assigned to the Text property of a label control. What we get in the ResponseStream object is what we have written to the response using the Write method of the Response property in the SaveImage.aspx page. So if the image is successfully saved we get “File Successfully saved” as the response else we get error as the response.&lt;/p&gt;  &lt;p align="justify"&gt;That’ it, we are done. With this technique we can save files or contents from one website to another site i.e. cross website posting of data. This technique is very simple but can be very dangerous. You should make sure that you implement some sort of authentication so that hackers can be evaded from saving some malicious files. &lt;/p&gt;  &lt;p align="justify"&gt;Try to know more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-7672541349818865325?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/r3IIeYDxbM8JSD3c_rbyDKJx5w8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/r3IIeYDxbM8JSD3c_rbyDKJx5w8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/r3IIeYDxbM8JSD3c_rbyDKJx5w8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/r3IIeYDxbM8JSD3c_rbyDKJx5w8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/I3M2qt1CI4A" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/7672541349818865325/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/08/saving-files-from-one-website-to.html#comment-form" title="8 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/7672541349818865325?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/7672541349818865325?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/I3M2qt1CI4A/saving-files-from-one-website-to.html" title="Saving files from one website to another website." /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>8</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/08/saving-files-from-one-website-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08FSH85eyp7ImA9WxNSEEk.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-2475339634892008010</id><published>2009-08-10T19:48:00.001+05:30</published><updated>2009-08-23T21:06:59.123+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-23T21:06:59.123+05:30</app:edited><title>Creating RESTful web services in .NET</title><content type="html">&lt;p align="justify"&gt;This blog is about creating RESTful web services using Windows Communicaiton Foundation (WCF) in .NET. The intention of this blog is not to teach what is Representation State Transfer (REST) but how to create web services which support REST by making use of WCF. So lets start by having a look at what was the requirement which prompted me to create a RESTful web service and how we did it.&lt;/p&gt;  &lt;p align="justify"&gt;As with any change in software, this change, to have RESTful web service was also raised by the client. The client in our case had some Business Access Layer (BAL) and he wanted to expose some of the methods to the outside world through RESTful web service. So what we did was we created a Facade layer to expose the required methods of the BAL and called exposed this Facade layer with the help of WCF web service. Thats the simple solution to our requirement. Now lets see the code wise implementation of the solution.&lt;/p&gt;  &lt;p align="justify"&gt;As per company rules and other client agreement I may not be able to publish the same implementation but will try to take another e.g. and try to replicate the same scenario and create RESTful web services. We will see two ways of creating WCF, one is implementing the interface in class file and then exposing the class methods using a .SVC file and second approach is the default way i.e. implementing an interface to a .SVC file. Lets see the first way (this was the solution implemented to solve our problem).&lt;/p&gt;  &lt;p align="justify"&gt;For creating a WCF service first thing you need is a contract. A contract is nothing but an Interface with the methods you want to expose. So lets create our contract (interface) which will be implemented by the WCF service. Any WCF webservice (RESTful or non RESTful) needs a contract, if no contract is defined then the class should be have the ServiceContract attribute to it and this is mandatory. Below is the interface definition.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;using System;            &lt;br /&gt;using System.Collections.Generic;             &lt;br /&gt;using System.Linq;             &lt;br /&gt;using System.Text; &lt;/p&gt;          &lt;p&gt;namespace CarService            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.ServiceModel.ServiceContract]              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; public interface ICarService             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.ServiceModel.OperationContract]&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [System.ServiceModel.Web.WebGet(UriTemplate=&amp;quot;cars&amp;quot;,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Xml)]               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Car&amp;gt; GetCars(); &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.ServiceModel.OperationContract]&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [System.ServiceModel.Web.WebGet(UriTemplate=&amp;quot;cars/search?carName={theCarName}&amp;quot;)]               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Car GetCar(string theCarName);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above interface definition one can see that we are using some attributes. Lets see each of these attributes one by one. First up is the ServiceContract attribute.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;ServiceContract: &lt;/strong&gt;This attribute tells the system that the interface defines a service contract for a WCF application. If an interface which is being inherited by a WCF web service doesn’t have a ServiceContract attribute then the following error will thrown by the system.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;i&gt;&lt;font color="#ff0000"&gt;Service 'CarService' implements multiple ServiceContract types, and no endpoints are defined in the configuration file. WebServiceHost can set up default endpoints, but only if the service implements only a single ServiceContract. Either change the service to only implement a single ServiceContract, or else define endpoints for the service explicitly in the configuration file.&lt;/font&gt;&lt;/i&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Just applying the ServiceContract attribute above the interface will solve the above error. Now lets see the various properties associated with the ServiceContract attribute &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td width="115"&gt;&lt;strong&gt;Property&lt;/strong&gt;&lt;/td&gt;        &lt;td width="272"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="115"&gt;ConfigurationName&lt;/td&gt;        &lt;td width="272"&gt;         &lt;p align="justify"&gt;The element name in the configuration file which defines the service.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="115"&gt;Name&lt;/td&gt;        &lt;td width="272"&gt;         &lt;p align="justify"&gt;Using the property you can provide a different name to the interface. &lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="115"&gt;Namespace&lt;/td&gt;        &lt;td width="272"&gt;Can be used to provide namespace.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="115"&gt;ProtectionLevel&lt;/td&gt;        &lt;td width="272"&gt;         &lt;p align="justify"&gt;enum value specifies what type of protection should be applied to the service. The enum values are ProtectionLevel.None, ProtectionLevel.Sign and ProtectionLevel.EncryptAndSign&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="115"&gt;SessionMode&lt;/td&gt;        &lt;td width="272"&gt;         &lt;p align="justify"&gt;enum specifies whether sessions are allowed or not. The enum values are SessionMode.Allowed, SessionMode.Required and SessionMode.NotAllowed.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;That’ about ServiceContract attribute. The next attribute which we have used in all the methods is the OperationContract attribute. Lets see what does OperationContract do.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;OperationContract: &lt;/strong&gt;OperationContract attribute tells the system that the method needs to be exposed as part of a service contract. A method which is not having OperationContract will not be exposed. So its imperative to have OperationContract attribute to expose a method. The properties which can be used along with OperationContract are.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Property&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Action&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;Using the action property you can specify a url to which the incoming messages needs to be posted. Action uniquely identifies a method with a Url.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;AsyncPattern&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;Specifies whether the method can be called asynchronously or not.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;IsOneWay&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;Specifies whether the operation returns a value or not.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;IsInitiating&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;Specifies whether the method needs to be called when session is started.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;IsTerminating&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;Tells the system to execute the method when session is completed.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Name&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;If you want to give some other name to the method then you can make use of the name property.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;ProtectionLevel&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;enum value specifies what type of protection should be applied to the service. The enum values are ProtectionLevel.None, ProtectionLevel.Sign and ProtectionLevel.EncryptAndSign&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Next attribute which you will notice is the WebGet. Here is the explanation of WebGet attribute.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;WebGet:&lt;/strong&gt; If you would have worked on ASP.NET application then you know that the forms in ASP.NET post data in two ways, one is the GET method and the other one is the POST method. WebGet attribute tells the system that the method is a pure data retrieval method to which HTTP GET method is associated. The properties associated with WebGet are listed below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Property&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;BodyStyle&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;enum specifies how messages should be sent. Enum values are WebMessageBodyStyle.Bare, WebMessageBodyStyle.Wrapped, WebMessageBodyStyle.WrappedRequest and WebMessageBodyStyle.WrappedResponse.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;RequestFormat&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;enum specifies what is the request format. Enum values are WebMessageFormat.Xml and WebMessageFormat.Json.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;ResponseFormat&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;Same as RequestFormat. Specifies the type of the response.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;UriTemplate&lt;/td&gt;        &lt;td&gt;         &lt;p align="justify"&gt;The Uri to access the method/operation.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;UriTemplate here is the endpoint. Here endpoint means a unique Url to access the method in a restful webservice. This is one of the requirement of a RESTful method which says there should be unique endpoint to access the methods.&lt;/p&gt;  &lt;p align="justify"&gt;One thing to note in UriTemplate property is that whatever you specify inside a curly brace, {}, and if the same name is used in the parameter of the method then the value which comes in the querystring is passed on as the parameter for the method. In our second method in the interface we have given the UriTemplate as something like this “&lt;font color="#008000"&gt;cars/search?carName={theCarName}&lt;/font&gt; ” which means the querystring value in carName will be passed as the parameter to the method, as the parameter name and value inside the curly brace are the same. If you want more parameters you can very well do this by declaring more parameters in the querystring. A hypothetical method is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;[System.ServiceModel.OperationContract]            &lt;br /&gt;[System.ServiceModel.Web.WebGet(UriTemplate = &amp;quot;cars/Carsearch?carName={theCarName}&amp;amp;color={theColor}&amp;amp;price={thePrice}&amp;quot;)]             &lt;br /&gt;Car SearchCar(string theCarName, string theColor, string thePrice);&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above e.g. theCarName, theColor and thePrice will be passed as parameter to the method SearchCar. The url may look something like this “http://localhost:2333/cars/carsearch?carName=ferrari&amp;amp;color=red&amp;amp;price=432544”. The values ferrari, red and 432544 will be passed as parameters to the SearchCar method. So if you want to pass parameters to a RESTful web method then you need to just follow the above approach. &lt;/p&gt;  &lt;p align="justify"&gt;Now that was about making use of HTTP GET method. The next obvious question would be how to make use of HTTP POST method. To make use of HTTP POST method one need to apply another attribute called “WebInvoke” All the properties are same as in WebGet except for an extra property called Method. Using the method property you can specify the HTTP method like POST, PUT, DELETE and HEAD. When to use each of these HTTP method is given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td width="84"&gt;&lt;strong&gt;HTTP Method&lt;/strong&gt;&lt;/td&gt;        &lt;td width="303"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="84"&gt;GET&lt;/td&gt;        &lt;td width="303"&gt;Whenever you want to retrieve some information you will make use of get.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="84"&gt;POST&lt;/td&gt;        &lt;td width="303"&gt;When you want to submit data which will be processed by the service.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="84"&gt;PUT&lt;/td&gt;        &lt;td width="303"&gt;When you want create or update a resource you will make use of PUT.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="84"&gt;DELETE&lt;/td&gt;        &lt;td width="303"&gt;As the name suggest whenever you want to delete a resource you will make use of DELETE.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Now we have our contract defined. So the first step is over. Now we need to implement the interface and that is what we are going to do next. We will be implementing the interface in a class called CarService. The code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;namespace CarService            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public class CarService : ICarService             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Car&amp;gt; carList;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public CarService()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; carList = new List&amp;lt;Car&amp;gt; {new Car{ModelName=&amp;quot;Ferrari&amp;quot;, Color=&amp;quot;Ferrari Red&amp;quot;, CubicCentimeter= &amp;quot;2345&amp;quot;,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Price=34324324}, new Car{ModelName=&amp;quot;BMW 3 Series&amp;quot;, Color=&amp;quot;Blue&amp;quot;, CubicCentimeter= &amp;quot;2200&amp;quot;,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Price = 32423}, new Car{ModelName=&amp;quot;Benz SLR&amp;quot;, Color=&amp;quot;Milky White&amp;quot;, CubicCentimeter = &amp;quot;3200&amp;quot;,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Price = 32424}};//Storing the car objects in a collection             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Car&amp;gt; ICarService.GetCars()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return carList;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Car ICarService.GetCar(string theCarName)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var car = from c in carList where c.ModelName == theCarName select c;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Car cs = null;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach (var c in car)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cs = c as Car;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return cs;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;You can see from the above code we have implemented ICarService interface into a class called CarService. The first method returns the Car collection and the second method searches the carList collection object based on the name of the car and returns the matching Car object. No rocket science here. The Car class is discussed in detail at the end of the blog. So we will move on to the next step.&lt;/p&gt;  &lt;p align="justify"&gt;We have defined the base requirement and now we have to expose the methods to the outside world using WCF service. For that you can either add a new WCF Service Applicaiton project from the file menu or you can right click on your ASP.NET or web service project and from the popup menu select Add—&amp;gt;New Item option. From the Add New Item dialog box select WCF Service and give a name to your service. I have named it CarService. The screenshot of Add New Item dialog is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a href="http://lh3.ggpht.com/_QY7fSC4VqiA/SomAexo-coI/AAAAAAAADVc/JUoTYM8xEjk/s1600-h/image9.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_QY7fSC4VqiA/SomAf-uieoI/AAAAAAAADVg/x-atBWt-wKw/image_thumb7.png?imgmax=800" width="395" height="244" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;You can do the same from file menu as well. &lt;/p&gt;  &lt;p align="justify"&gt;When you add WCF Web service application or a SVC file the system will automatically add an interface by the name ICarService or I followed by whatever name given to the project or SVC file. Delete the interface, as we have already defined the contract. Also delete the SVC.cs file. Now open the SVC file, in my case CarService.svc. You can see a markup something like this.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&amp;lt;%@ ServiceHost Language=&amp;quot;C#&amp;quot; Debug=&amp;quot;true&amp;quot; Service=&amp;quot;CarService.CarService&amp;quot; &lt;font color="#008000"&gt;CodeBehind=&amp;quot;Service1.svc.cs&amp;quot;&lt;/font&gt; %&amp;gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Delete the CodeBehind attribute and add Factory attribute as shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;%@ ServiceHost Language=&amp;quot;C#&amp;quot; Debug=&amp;quot;true&amp;quot;            &lt;br /&gt;Service=&amp;quot;CarService.CarService&amp;quot; &lt;font color="#008000"&gt;Factory=&amp;quot;System.ServiceModel.Activation.WebServiceHostFactory&amp;quot;&lt;/font&gt; %&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;You may get an error something like the one pasted below. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;i&gt;&lt;font color="#ff0000"&gt;Service 'CarService.CarService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.&lt;/font&gt;&lt;/i&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;If you come across an error like this then the possible reason could be you will be missing the Factory attribute in the ServiceHost tag. Just by adding the Factory attribute will solve the prob.&lt;/p&gt;  &lt;p&gt;Visual studio also embeds the following tag in Web.config file. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;system.serviceModel&amp;gt;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;behaviors&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160; &amp;lt;serviceBehaviors&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;behavior name=&amp;quot;RestFulWebservice.CarServiceBehavior&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;serviceMetadata httpGetEnabled=&amp;quot;true&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;serviceDebug includeExceptionDetailInFaults=&amp;quot;false&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/behavior&amp;gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; &amp;lt;/serviceBehaviors&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;/behaviors&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;services&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160; &amp;lt;service behaviorConfiguration=&amp;quot;RestFulWebservice.CarServiceBehavior&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; name=&amp;quot;RestFulWebservice.CarService&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;endpoint address=&amp;quot;&amp;quot; binding=&amp;quot;wsHttpBinding&amp;quot; contract=&amp;quot;CarService.ICarService&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;identity&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;dns value=&amp;quot;localhost&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/identity&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/endpoint&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;endpoint address=&amp;quot;&amp;quot; binding=&amp;quot;mexHttpBinding&amp;quot; contract=&amp;quot;IMetadataExchange&amp;quot; /&amp;gt;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; &amp;lt;/service&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;/services&amp;gt;             &lt;br /&gt;&amp;lt;/system.serviceModel&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Now you are ready to go. Just hit F5 and see the output. If you see a message saying there is no endpoint defined you need not panic. Just type one of the endpoints defined in the contract (interface) you can see the output. In my case the two url which I used are as follows.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a title="http://localhost:3891/CarService.svc/cars" href="http://localhost:3891/CarService.svc/cars"&gt;&lt;font color="#008000"&gt;http://localhost:3891/CarService.svc/cars&lt;/font&gt;&lt;/a&gt;           &lt;br /&gt;Ouput of the above url           &lt;br /&gt;          &lt;p&gt;&amp;lt;GetCarsResponse xmlns=&amp;quot;&lt;a href="http://tempuri.org/&amp;quot;"&gt;http://tempuri.org/&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;GetCarsResult xmlns:a=&amp;quot;&lt;a href="http://schemas.datacontract.org/2004/07/CarService&amp;quot;"&gt;http://schemas.datacontract.org/2004/07/CarService&amp;quot;&lt;/a&gt; xmlns:i=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema-instance&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Car&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Color&amp;gt;Ferrari Red&amp;lt;/a:Color&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:CubicCentimeter&amp;gt;2345&amp;lt;/a:CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:ModelName&amp;gt;Ferrari&amp;lt;/a:ModelName&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Price&amp;gt;34324324&amp;lt;/a:Price&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/a:Car&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Car&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Color&amp;gt;Blue&amp;lt;/a:Color&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:CubicCentimeter&amp;gt;2200&amp;lt;/a:CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:ModelName&amp;gt;BMW 3 Series&amp;lt;/a:ModelName&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Price&amp;gt;32423&amp;lt;/a:Price&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/a:Car&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Car&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Color&amp;gt;Milky White&amp;lt;/a:Color&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:CubicCentimeter&amp;gt;3200&amp;lt;/a:CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:ModelName&amp;gt;Benz SLR&amp;lt;/a:ModelName&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a:Price&amp;gt;32424&amp;lt;/a:Price&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/a:Car&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/GetCarsResult&amp;gt;             &lt;br /&gt;&amp;lt;/GetCarsResponse&amp;gt;&lt;/p&gt;          &lt;p&gt;&lt;a title="http://localhost:3891/CarService.svc/cars/search?carName=ferrari" href="http://localhost:3891/CarService.svc/cars/search?carName=Ferrari"&gt;&lt;font color="#008000"&gt;http://localhost:3891/CarService.svc/cars/search?carName=Ferrari&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;          &lt;p&gt;Output of the above url.&lt;/p&gt;          &lt;p&gt;&amp;lt;Car xmlns=&amp;quot;&lt;a href="http://schemas.datacontract.org/2004/07/CarService&amp;quot;"&gt;http://schemas.datacontract.org/2004/07/CarService&amp;quot;&lt;/a&gt; xmlns:i=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema-instance&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Color&amp;gt;Ferrari Red&amp;lt;/Color&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;CubicCentimeter&amp;gt;2345&amp;lt;/CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;ModelName&amp;gt;Ferrari&amp;lt;/ModelName&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Price&amp;gt;34324324&amp;lt;/Price&amp;gt;             &lt;br /&gt;&amp;lt;/Car&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;The car class which is getting serialized is shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;[System.Runtime.Serialization.DataContract]            &lt;br /&gt;public class Car             &lt;br /&gt;{&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [System.Runtime.Serialization.DataMember]             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string Color             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [System.Runtime.Serialization.DataMember]             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string ModelName             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [System.Runtime.Serialization.DataMember]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public int Price             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [System.Runtime.Serialization.DataMember]             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string CubicCentimeter             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The objects used in WCF should be qualified by DataContract attribute. Some of the properties of DataContract are listed below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Property&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Name&lt;/td&gt;        &lt;td&gt;If you want to give a different name to the class when it is serialized then you can make use of the name property.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Namespace&lt;/td&gt;        &lt;td&gt;To provide a namespace to the xml attribute you can make use of the “Namespace” property. When you provide namespace it has abide by w3 guidelines. As a general practice we provide urls as namespace.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The other attribute is the DataMember. DataMember attribute tells the serializer that the property/field is part of a DataContract and it can be serilizable. Some of the properties are explained below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Property&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;EmitDefaultValue&lt;/td&gt;        &lt;td&gt;Property which tells the serializer whether to emit default value if the property is not having any value.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;IsRequired&lt;/td&gt;        &lt;td&gt;This property can be set when you want the property/field to be present while serializing and deserializing the object.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Name&lt;/td&gt;        &lt;td&gt;If you want to give some other name other than name of the property/field then you can make use the Name property.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;Order&lt;/td&gt;        &lt;td&gt;When the object is serialized in what order the property/field needs to be serialized.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Now lets see the second way of implementing the WCF webservice. This is the most common way which everyone will be aware off. Open VS 2008 and add a new project of type WCF Service Application from File—&amp;gt;New—&amp;gt;Project menu. This will automatically add a contract (interface) and the necessary .SVC files (a .svc and a .svc.cs file inheriting from the interface). Rest we need to follow the same steps for defining various methods in the Interface as explained above.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-2475339634892008010?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/uR2WL2l79v_7xFtbKPWj8NJBlnY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uR2WL2l79v_7xFtbKPWj8NJBlnY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/uR2WL2l79v_7xFtbKPWj8NJBlnY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uR2WL2l79v_7xFtbKPWj8NJBlnY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/Hslsz8rqblk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/2475339634892008010/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/08/creating-restful-web-services-in-net.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/2475339634892008010?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/2475339634892008010?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/Hslsz8rqblk/creating-restful-web-services-in-net.html" title="Creating RESTful web services in .NET" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>2</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/08/creating-restful-web-services-in-net.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEEGSH07fyp7ImA9WxJaF04.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-4262806325229371431</id><published>2009-08-08T17:50:00.001+05:30</published><updated>2009-08-08T18:33:49.307+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-08T18:33:49.307+05:30</app:edited><title>Formatting numeric data types in .NET</title><content type="html">&lt;p align="justify"&gt;Recently I was discussing with one of my friend and he informed me that he was breaking his head on creating a utility to format decimals, float and other datatypes. Out of curiosity I asked him how are you doing these, for e.g. displaying a decimal datatype with only two decimal place value. Bang came the reply that he is doing some string manipulation. I was surprised and informed him that he need not take so much of a pain instead he can use ToString methods of most of the datatype to format the data types. In this blog I will highlight some of the commonly needed number formatting techniques.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;First up formatting for decimal places.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;This is one of the common requirement where we always come across situation to format float, decimal and double datatypes to some number of decimal places. One of the easiest way to format is given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double doub = 8457.3299;            &lt;br /&gt;float fl = 28398.39843289f;             &lt;br /&gt;decimal dec = 982389.348093m;             &lt;br /&gt;Console.WriteLine(doub.ToString(&amp;quot;.00&amp;quot;));//Output: 8467.33             &lt;br /&gt;Console.WriteLine(fl.ToString(&amp;quot;.000&amp;quot;));//Output: 28398.400             &lt;br /&gt;Console.WriteLine(dec.ToString(&amp;quot;.0000&amp;quot;));//Output: 982389.3481&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above e.g. I have used zero in combination with decimal symbol to format the numbers according to the required precision required. If the need is to format to the max of two decimal places I have used “.” (dot) followed by 2 zeroes. Similarly how many precision points you want you can use that number of zeroes along with dot. Isn’t it so simple? One thing to note here is that the system will automatically round off the numbers if there are more decimal places than what is specified in the format string as you can see in the above e.g.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Formatting numbers&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Some number formatting e.g. are given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double doub = 8457.3299;            &lt;br /&gt;float fl = 28398.39843289f;             &lt;br /&gt;decimal dec = 982389.348093m;             &lt;br /&gt;Console.WriteLine(doub.ToString(&amp;quot;000&amp;quot;));//Ouput 8457             &lt;br /&gt;Console.WriteLine(fl.ToString(&amp;quot;00000000&amp;quot;));//Ouput 00028398             &lt;br /&gt;Console.WriteLine(dec.ToString(&amp;quot;00000.000&amp;quot;));//Ouput 982389.348             &lt;br /&gt;Console.WriteLine(dec.ToString(&amp;quot;&lt;font color="#800000"&gt;00 - 00 - 0.000&lt;/font&gt;&amp;quot;));//Ouput 982 - 38 - 9.348             &lt;br /&gt;Console.WriteLine(dec.ToString(&amp;quot;&lt;font color="#800000"&gt;(00000) (00) (0)&lt;/font&gt;&amp;quot;));//Ouput (00982) (38) (9)&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above e.gs we have used zeroes to format numbers. First formatting using 3 zeroes doesn’t do much. It just displays the numbers as it is. The second one with 8 zeroes formats numbers and adds extra zeroes if the number doesn’t have the required digits i.e. in the above e.g. there are only 5 digits and because of formatting applied it adds extra zeroes preceeding the number. Use of zero to format numbers tells the system that even if zero is not a significant number it should display zero. One can use other characters along with zero to format the numbers. Few e.g. have been highlighted above.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Formatting numbers using # symbol&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;One of the problem while using 0 to format numbers is that it will show the insignificant numbers or club zeroes to fill the gap. To avoid just this you can make use of # symbol in place of 0. To show the difference between # and 0 I have pasted below same e.g. with both the format string.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double twoDeci = 1234.34;           &lt;br /&gt;int flo = 2343243;            &lt;br /&gt;decimal deci = 23434355.6749m;            &lt;br /&gt;Console.WriteLine(twoDeci.ToString(&amp;quot;######.####&amp;quot;));\\Output 1234.34            &lt;br /&gt;Console.WriteLine(twoDeci.ToString(&amp;quot;000000.0000&amp;quot;));\\Ouput 001234.3400            &lt;br /&gt;Console.WriteLine(flo.ToString(&amp;quot;(####) - (###) - (##)&amp;quot;));\\Output (23) - (432) - (43)            &lt;br /&gt;Console.WriteLine(flo.ToString(&amp;quot;(0000) - (000) - (00)&amp;quot;));\\Output (0023) - (432) - (43)            &lt;br /&gt;Console.WriteLine(deci.ToString(&amp;quot;.##&amp;quot;));\\Output 23434355.67            &lt;br /&gt;Console.WriteLine(deci.ToString(&amp;quot;.00&amp;quot;));\\Output 23434355.67&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;From the above e.gs one can make out that if you are using 0 as the format string then it adds insignificant zeroes preceding or leading the numbers, whereas # will ignore insignificant zeroes. Also one thing to note is that if you use # with numbers with insignificant zeroes then it will ignore those zeroes whereas 0 will consider the zeroes as significant. The difference is shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;float preecedingZeroes = 00034556;           &lt;br /&gt;double leadingZeroes = 34345.983000;            &lt;br /&gt;Console.WriteLine(preecedingZeroes.ToString(&amp;quot;########&amp;quot;));\\Ouput 34556            &lt;br /&gt;Console.WriteLine(preecedingZeroes.ToString(&amp;quot;00000000&amp;quot;));\\Ouput 00034556            &lt;br /&gt;Console.WriteLine(leadingZeroes.ToString(&amp;quot;#####.######&amp;quot;));\\Ouput 34345.983            &lt;br /&gt;Console.WriteLine(leadingZeroes.ToString(&amp;quot;00000.000000&amp;quot;));\\Ouput 34345.983000&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;From the above e.g. differences are clear that # never takes into consideration the insignificant zeroes even if there is one in the datatype. So if you want the insignificant zeroes to be considered then you need to use 0 instead of #.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Formatting for thousand separator.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Many a times we get numbers from the database without any formatting applied and we may require the numbers to be formatting with thousand separators. Its very easy to apply thousand separators to a number. E.g. are pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double preecedingZeroes = 345563434;           &lt;br /&gt;Console.WriteLine(preecedingZeroes.ToString(&amp;quot;#,#&amp;quot;));\\Output 345,563,434            &lt;br /&gt;Console.WriteLine(preecedingZeroes.ToString(&amp;quot;0,0&amp;quot;));\\Output 345,563,434&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above e.g. makes use of both # and 0 along with “,” sign for thousand formatting. Comma (,) can be used along with other format strings to apply thousand formatting. One thing to note here is that when you apply thousand separator formatting then the system makes use of the system’ culture and thousand separator associated with the culture. If you want to use a specific culture’ thousand separator then you need to provide the culture you want to use along with the “ToString” method. Here are e.g. in Danish and Indian format.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Applies Hindi-India culture and formats the number based on Indian notations.           &lt;br /&gt;&lt;font color="#008000"&gt;Console.WriteLine(preecedingZeroes.ToString(&amp;quot;#,#&amp;quot;, System.Globalization.CultureInfo.CreateSpecificCulture(&amp;quot;hi-IN&amp;quot;)));&lt;/font&gt;//Ouput 34,55,63,434            &lt;br /&gt;//Applies Danish culture and formats the number based on Danish culture.            &lt;br /&gt;&lt;font color="#008000"&gt;Console.WriteLine(preecedingZeroes.ToString(&amp;quot;0,0&amp;quot;, System.Globalization.CultureInfo.CreateSpecificCulture(&amp;quot;da-DK&amp;quot;)));&lt;/font&gt;//Ouput 345.563.434&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;So you can see from the above e.g. that in the Indian culture the thousand separator used is comma (,) whereas in Danish culture the thousand separator is represented by a dot (.)&lt;/p&gt;  &lt;p align="justify"&gt;One thing to note here is that if you use “,” one after the other without any number formatting characters in between them, then the meaning of comma completely changes. The system consider each comma as 1000 and divides the number to which formatting is applied by 1000. Confused, let me explain with e.g.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double dou = 23432432423;           &lt;br /&gt;&lt;font color="#008000"&gt;Console.WriteLine(dou.ToString(&amp;quot;#,,&amp;quot;));&lt;/font&gt;//Output 23432            &lt;br /&gt;&lt;font color="#008000"&gt;Console.WriteLine(dou.ToString(&amp;quot;0,,,&amp;quot;));&lt;/font&gt;//Output 23&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above e.g. if you see in the first formatting we have used two commas which means the number “23432432423” will be divided by thousand two times as the number of commas occur two times in the formatting string. Similarly in the second sample code the commas occur thrice so the number will be divided by 1000 three times.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Formatting for positive and negative numbers.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;There may situation where you don’t know what type of number you are getting from the database, whether the number is a positive or a negative one. Even the number can be a zero. You would like to apply different formatting for positive number and different formatting for negative number. Also you would like to show zero in a different way. I have faced a similar prob in one of my projects where the client wanted to display a positive symbol enclosed in brackets (+) in front of a positive number, a negative symbol enclosed in brackets (-) in case of negative numbers and a zero enclosed in brackets (0) in the case of zero. If you also face a similar kind of situation you can make use “;” (semi colon) to separate the formatting. Sample formatting code is given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double positiveNo = 3746;           &lt;br /&gt;double negativeNo = -3435;            &lt;br /&gt;double zero = 0;            &lt;br /&gt;string formatString = &amp;quot;(+)#;(-)#;(0)&amp;quot;;            &lt;br /&gt;Console.WriteLine(positiveNo.ToString(formatString));//Output (+)3746            &lt;br /&gt;Console.WriteLine(negativeNo.ToString(formatString));//Output (-)3435            &lt;br /&gt;Console.WriteLine(zero.ToString(formatString));//Output (0)&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above e.g. if you notice I have defined the formatting string for positive, negative and zero in the variable “formatString”. Semi colon (;) is used as the separator for the different number format. This is how this works, starting from left in the formatString variable the format string to the left of the first semi colon is considered as the formatting string for positive numbers, and the format string in between the first semi colon and the second semi colon is considered the format string for negative values and the format string to the right of second semi colon is taken as the format string for zeroes. &lt;/p&gt;  &lt;p align="justify"&gt;If you don’t want to give separate formatting for zero then you can just leave the format string after the second semi colon empty i.e. you need to only specify format strings for positive and negative values. In such situation the format string for positive value will be applied to format zeroes. The sample is given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double positiveNo = 3746;           &lt;br /&gt;double negativeNo = -3435;            &lt;br /&gt;double zero = 0;            &lt;br /&gt;string formatString = &amp;quot;(+)#;(-)#&amp;quot;;            &lt;br /&gt;Console.WriteLine(positiveNo.ToString(formatString));//Output (+)3746            &lt;br /&gt;Console.WriteLine(negativeNo.ToString(formatString));//Output (-)3435            &lt;br /&gt;Console.WriteLine(zero.ToString(formatString));//Output (+)&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Multiplying a number by 100.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;If you want to multiply a number by 100 one can make use of the % symbol in the format string along with other format strings. Sample code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double dd = .459;           &lt;br /&gt;double ddd = 849;            &lt;br /&gt;Console.WriteLine(dd.ToString(&amp;quot;#%&amp;quot;));//Output 46%            &lt;br /&gt;Console.WriteLine(dd.ToString(&amp;quot;.#%&amp;quot;));//Output 45.9%            &lt;br /&gt;Console.WriteLine(ddd.ToString(&amp;quot;#%&amp;quot;));//Output 84900%            &lt;br /&gt;Console.WriteLine(dd.ToString(&amp;quot;#.#%&amp;quot;));//Output 45.9%            &lt;br /&gt;Console.WriteLine(ddd.ToString(&amp;quot;0%&amp;quot;));//Output 84900%&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;You can see from the above e.g. that by using the % sign along with other format strings the number gets multiplied by 100 and % symbol is appended at the end of the number. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Appending currency symbol to a number.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Another common requirement is to append the currency symbol along with numbers. To do just that one can make use of “C” or “c” character in the format string. The e.g. is given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double curFor = 329.5943;           &lt;br /&gt;Console.WriteLine(&lt;font color="#800000"&gt;curFor.ToString(&amp;quot;C&amp;quot;)&lt;/font&gt;);//Output $329.59            &lt;br /&gt;Console.WriteLine(&lt;font color="#800000"&gt;curFor.ToString(&amp;quot;C3&amp;quot;)&lt;/font&gt;);//Output $329.594&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The currency symbol and precision points will be taken from the current culture set for the system. If you want to specify precision points for decimal value then you can specify the number of precision points after the “C” symbol as shown in the above e.g. If you want to change the current culture and apply some other culture then you can do that as shown in the below e.g.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;Console.WriteLine(curFor.ToString(&amp;quot;C3&amp;quot;, &lt;font color="#800000"&gt;System.Globalization.CultureInfo.CreateSpecificCulture(&amp;quot;en-GB&amp;quot;)&lt;/font&gt;));//Output £329.594&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above e.g. we are using currency format specifier with 3 decimal point precision and also we are applying Great Britain’ i.e. UK’ culture. This formats the number and adds the pound symbol.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Displaying numbers in exponential form&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;If you want to display numbers in exponential format you can make use of “E” or “e”. Sample code is pasted below. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double exp = 43895.04390;           &lt;br /&gt;Console.WriteLine(exp.ToString(&amp;quot;e&amp;quot;));//Output 4.389504e+004&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;If you want you can specify the number of precision decimal points to be displayed. If the precision points are not specified then the system by default will add 6 precision points after the decimal point. Sample code with precision point is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;double exp = 43895.04390;           &lt;br /&gt;Console.WriteLine(exp.ToString(&amp;quot;E9&amp;quot;));//Output 4.389504390E+004&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;So with that I have covered some of the common number formatting requirements. &lt;/p&gt;  &lt;p align="justify"&gt;Try to know more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-4262806325229371431?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ksSMFHLfqXQrGnLliRIrjNJLO00/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ksSMFHLfqXQrGnLliRIrjNJLO00/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ksSMFHLfqXQrGnLliRIrjNJLO00/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ksSMFHLfqXQrGnLliRIrjNJLO00/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/rjJ8LzcVLR8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/4262806325229371431/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/08/formatting-numeric-data-types-in-net.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/4262806325229371431?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/4262806325229371431?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/rjJ8LzcVLR8/formatting-numeric-data-types-in-net.html" title="Formatting numeric data types in .NET" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/08/formatting-numeric-data-types-in-net.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0UMQn87cCp7ImA9WxNSFE4.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-5065578739373884391</id><published>2009-07-09T18:05:00.001+05:30</published><updated>2009-08-28T11:31:23.108+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-28T11:31:23.108+05:30</app:edited><title>Lambda Expressions – Features of C# 3.0 (Part – 6)</title><content type="html">&lt;p align="justify"&gt;In my earlier article we have seen the following new features of C# 3.0.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/02/extension-methods-features-of-c-30-part.html"&gt;Extension Methods&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/03/automatic-properties-features-of-c-30.html"&gt;Automatic Properties&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/04/implicitly-typed-variable.html"&gt;Implicitly Typed variables&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/04/object-initializers-and-collection.html"&gt;Object Initializers and Collection Initializers&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/06/anonymous-types-features-of-c-30-part-5.html" target="_blank"&gt;Anonymous Types&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p align="justify"&gt;In this blog we will try to understand the concept of Lambda expressions. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;What is Lambda Expression?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Lambda expressions are nothing but anonymous functions. Lambda expressions can be used to write expressions or statements. To write Lambda expression one has to make use of the Lambda operator which is read as “goes to”. Lambda operator is represented as “=&amp;gt;” i.e. an equal to sign followed by a greater than symbol. Lambda expressions normally have input parameters on the left side of the Lambda operator and expressions/statements on the right side. The lambda operator is used to separate the input arguments on the left side from the expression/statements on the right side. Lambda expressions are extensively used in LINQ. An e.g. of Lambda expression is given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public class LambdaExpressionDemo            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; delegate int Add(int a, int b);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void PrintAddResult(int num1, int num2)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Add addResult = &lt;font color="#008000"&gt;(a, b) =&amp;gt; a + b&lt;/font&gt;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(addResult(num1, num2).ToString());             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; public double CountEmp()           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Collections.Generic.List&amp;lt;Person&amp;gt; perColl = new List&amp;lt;Person&amp;gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Person {FName=&amp;quot;first&amp;quot;, LName=&amp;quot;lastFirs&amp;quot;, Age=30},            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Person{FName=&amp;quot;second&amp;quot;, LName=&amp;quot;lastSecond&amp;quot;, Age=24},            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new Person{FName=&amp;quot;third&amp;quot;, LName=&amp;quot;Third&amp;quot;, Age=44}};            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; double avgAge = perColl.Count(&lt;font color="#008000"&gt;p =&amp;gt; p.LName.StartsWith(&amp;quot;la&amp;quot;)&lt;/font&gt;);&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return avgAge;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code one can see we have declared a delegate which returns an integer value and takes two integers as input parameters. Also you can see I have declared a delegate variable (addResult) and assigned an anonymous function using our Lambda expression. One thing to note here is that there is no need to declare the type of the input arguments for the anonymous functions. The compiler will infer the type. What the above lambda expressions means is that we have an anonymous function with two input parameters (left side of the Lambda operators are input parameters) which needs to be added and the result returned. Don’t you thing this is a very simple way of declaring a function. In the second e.g. I have made use of Lambda expression along with Count method. The lambda expression evaluates and checks whether the person’ LName starts with “la” and returns true or false. If it returns true then that record is counted. So the count will be 2. &lt;/p&gt;  &lt;p align="justify"&gt;To better understand the advantages/usefullness of Lambda expression we need to know how we use to implement these things in the earlier versions of C#. Lets see how we would do this in C# 2.0 using anonymous methods.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;delegate int Add(int a, int b);            &lt;br /&gt;public void PrintAddResult(int num1, int num2)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Using anonymous methods of C#2.0.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Add addResult = &lt;font color="#008000"&gt;delegate(int aa, int bb) { return aa + bb; };              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(addResult(num1, num2));             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above e.g. we are making use of Anonymous method feature of C# 2.0 where we are making use of delegates to declare an Anonymous methods (highlighted in green). Wherever you thing creating a method is redundant or will create overhead you can go ahead and make use of Anonymous methods. Also one can use anonymous methods in cases where a method or property or constructor expects a delegate. In the above code we have declared a delegate with the signature of the method and then again using delegate variable, addResult, we are declaring an Anonymous method. One can write any number of statements inside Anonymous methods. The scope of the variables declared in Anonymous methods are within the limits of Anonymous methods. &lt;/p&gt;  &lt;p align="justify"&gt;Now lets see how we would have done this in the good olden days of C#1.0.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;delegate int Add(int a, int b);            &lt;br /&gt;public void PrintAddResult(int num1, int num2)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //The good olden days of C#1.0             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Add additionDel = Addition;             &lt;br /&gt;} &lt;/p&gt;          &lt;p&gt;public int Addition(int a, int b)            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return a + b;             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code you can see the good olden days of delegate declaration where one has to create a method (Addition) and then create a delegate (Add) representing the method. Once you have declared the delegate one can assign the method. So after seeing the olden ways of declaring the same functionality which can be achieved using Lambda expression one can see the advantages.&lt;/p&gt;  &lt;p align="justify"&gt;One can define two types of lambda, an Expression Lambda and a Statement Lambda. Lets see what each one of it means.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Expression Lambda: &lt;/strong&gt;Expression lambda is nothing but an expression on the right side of the Lambda Operator. E.g are given below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;(a, b) =&amp;gt;&amp;#160; (a + b);          &lt;br /&gt;(int a, int b) =&amp;gt; a &amp;gt; b;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;An expressions is any piece of code which can be evaluated to a single value, object etc. So the above code are just e.g. of expressions, even an assignment of a value to a variable is also an expression.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Statement Lambdas: &lt;/strong&gt;As the name suggests statement lambdas are ones which can have more than one line of code/statement. In expression lambda you normally have only one line of statement which executes and produces the output in the form of a single value or object etc whereas in statement lambda we have more than one line of statements enclosed in a curly brace ({}).&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;delegate int Add(int a, int b);            &lt;br /&gt;public void StatementLambda(int operand1, int operan2)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;Add result = (a, b) =&amp;gt;              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int c = a + b;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(c.ToString());               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return c;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; };               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; result(operand1, operan2);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code you can see we have written some three lines of code and this is an e.g. of statement Lambda. There are no restriction to the number of lines of code that can be written in a Lambda expression. As a good practice it will be nice if you can restrict the number of lines between 2 and 3 lines.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Some points about Lambda expressions&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div align="justify"&gt;Lambda expressions are anonymous functions.&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;No need to declare the types of the variables used in the input parameter of the Lambda expression. The types will be inferred by the compiler. &lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;If there are situation when the compiler cannot infer the type of the variable used, in those kind of scenarios we can explicitly declare the types for the input parameters as shown below.&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;delegate int Add(int a, int b);            &lt;br /&gt;public void PrintAddResult(int num1, int num2)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Add result = (int a, int b) =&amp;gt; a + b;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(result(num1, num2).ToString());             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div align="justify"&gt;Variables declared in a Lambda expressions are scoped inside the Lambda blocks.&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;Lambda declaration should match the signature of the delegate i.e. the number of parameters and return type.&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div align="justify"&gt;Parameter less Lambdas can also be declared just by having a empty parentheses as shown below.&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;delegate void Print();            &lt;br /&gt;public void ParameterLessLambda()             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Print print = &lt;font color="#008000"&gt;() =&amp;gt; Console.WriteLine(&amp;quot;Parameter less lambda executed.&amp;quot;);              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; print();             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;ul&gt;   &lt;li&gt;You can pass Lambda expression where a delegate is expected. E.g. is shown below. &lt;/li&gt; &lt;/ul&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;/*Lambda expression passed as an argument where delegate was expected.*/            &lt;br /&gt;System.Threading.Thread thh = new System.Threading.Thread(             &lt;br /&gt;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;() =&amp;gt; Console.WriteLine(&amp;quot;hi&amp;quot;)&lt;/font&gt;);&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div align="justify"&gt;When you are making use of Lambda expressions behind the scene the compiler does the extra work of creating an anonymous function for the Lambda expression. Lets see with an e.g.: the code and generated IL are pasted below.&lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;/*Class having a delegate and a Lambda expression.*/            &lt;br /&gt;class LambdaExpression             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; delegate int Add(int operand1, int operand2);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void LambdaExp(int operan1, int operan2)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //Lambda expression.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Add result = &lt;font color="#008000"&gt;(a, b) =&amp;gt; a + b;              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(result(operan1, operan2));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;          &lt;p&gt;/*Below is the MSIL generated for the above method. */            &lt;br /&gt;.method public hidebysig instance void LambdaExp(int32 operan1,             &lt;br /&gt;int32 operan2) cil managed             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; .maxstack 3             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; .locals init (             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [0] class             &lt;br /&gt;LambdaExpressions.LambdaExpression/Add result)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0000: nop             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0001: ldsfld class             &lt;br /&gt;LambdaExpressions.LambdaExpression/Add             &lt;br /&gt;LambdaExpressions.LambdaExpression::CS$&amp;lt;&amp;gt;9__Cach             &lt;br /&gt;edAnonymousMethodDelegate1             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0006: brtrue.s L_001b             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0008: ldnull             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0009: ldftn int32             &lt;br /&gt;LambdaExpressions.LambdaExpression::&amp;lt;LambdaExp&amp;gt;b             &lt;br /&gt;__0(int32, int32)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_000f: newobj instance void             &lt;br /&gt;LambdaExpressions.LambdaExpression/Add::.ctor             &lt;br /&gt;(object, native int)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0014: stsfld class LambdaExpressions.LambdaExpression/Add             &lt;br /&gt;LambdaExpressions.LambdaExpression::CS$&amp;lt;&amp;gt;9__Cach             &lt;br /&gt;edAnonymousMethodDelegate1             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0019: br.s L_001b             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_001b: ldsfld class             &lt;br /&gt;LambdaExpressions.LambdaExpression/Add             &lt;br /&gt;LambdaExpressions.LambdaExpression::CS$&amp;lt;&amp;gt;9__Cach             &lt;br /&gt;edAnonymousMethodDelegate1             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0020: stloc.0             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0021: ldloc.0             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0022: ldarg.1             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0023: ldarg.2             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0024: callvirt instance int32             &lt;br /&gt;LambdaExpressions.LambdaExpression/Add::Invoke(i             &lt;br /&gt;nt32, int32)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_0029: call void [mscorlib]System.Console::WriteLine(int32)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_002e: nop             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; L_002f: ret             &lt;br /&gt;}&lt;/p&gt;          &lt;p&gt;/*C# code decompiled from MSIL using .NET Reflector*/            &lt;br /&gt;internal class LambdaExpression             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Methods             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public void LambdaExp(int operan1, int operan2)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Add result = &lt;font color="#008000"&gt;delegate (int a, int b) {              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return a + b;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(result(operan1, operan2));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; } &lt;/p&gt;          &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; // Nested Types            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private delegate int Add(int operand1, int operand2);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above pasted code you can see the MSIL generated is making use of AnonymousMethodDelegate i.e. the compiler is converting Lambda expressions into Anonymous functions. This is proven from the decompiled C# code from the MSIL pasted above. &lt;/p&gt;  &lt;p align="justify"&gt;As with other features of C# 3.0 Lambda expression is also a way of writing terse code where the compiler does the extra work of generating the extra codes. In my next&amp;#160; we will have a look at Language Integrated Query (LINQ) for which all these features were incorporated in C#. Till then write terse code and try to learn more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-5065578739373884391?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/hFFkubV_avPtFwHGFSTsCctr3qg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hFFkubV_avPtFwHGFSTsCctr3qg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/hFFkubV_avPtFwHGFSTsCctr3qg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hFFkubV_avPtFwHGFSTsCctr3qg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/k0vz1J7Dwt0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/5065578739373884391/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/07/lambda-expressions-features-of-c-30.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5065578739373884391?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5065578739373884391?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/k0vz1J7Dwt0/lambda-expressions-features-of-c-30.html" title="Lambda Expressions – Features of C# 3.0 (Part – 6)" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>0</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/07/lambda-expressions-features-of-c-30.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkcDR3c9fSp7ImA9WxJVGEo.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-1854500197317652648</id><published>2009-07-03T14:26:00.001+05:30</published><updated>2009-07-06T15:24:36.965+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-07-06T15:24:36.965+05:30</app:edited><title>Working with Modal Popup extender control.</title><content type="html">&lt;p align="justify"&gt;This blog discusses about the ASP.NET AJAX ModalPopupExtender control. In this blog we will see how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, showing/displaying/calling the modal popup from a hyper link control within the gridview control and finally some important javascript methods of the ASP.NET AJAX ModalPopupExtender control. So first lets see how to use the ModalPopupExtender control. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;How to use the ASP.NET AJAX ModalPopupExtender control?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Drag and drop a ASP.NET button/LinkButton/ImageButton or any control to an aspx page. To add a ModalPopupExtender control, drag and drop the control from the toolbox or Select the control (button/LinkButton/ImageButton or any other control) and click the button with a greater than symbol as shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a href="http://lh5.ggpht.com/_QY7fSC4VqiA/Sk3HrYCaMMI/AAAAAAAAC40/baIjKjgb8UI/s1600-h/image17.png"&gt;&lt;img style="display: inline" title="image" alt="image" src="http://lh6.ggpht.com/_QY7fSC4VqiA/Sk3Hs-_pz9I/AAAAAAAAC44/jVSpu-2Q8R0/image_thumb11.png?imgmax=800" width="388" height="79" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;When you click the arrow you will get a menu with an “Add Extender” option as shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a href="http://lh6.ggpht.com/_QY7fSC4VqiA/Sk3Ht1n5_iI/AAAAAAAAC48/PWpV3ouvkh0/s1600-h/image16.png"&gt;&lt;img style="display: inline" title="image" alt="image" src="http://lh6.ggpht.com/_QY7fSC4VqiA/Sk3Hu9QVSeI/AAAAAAAAC5A/mkaTaHm0bIY/image_thumb10.png?imgmax=800" width="233" height="176" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Click the “Add Extender” menu option and select “ModalPopupExtender” from “Extender Wizard” popup. Once you have added the ModalPopupExtender control you need an ASP.NET panel control. This is because the ModalPopupExtender control will show the content of the Panel control as a popup. Once you have added the ASP.NET Panel control add the content which you want to display as popup into the ASP.NET Panel control. Sample code with all the above requirement is pasted below. Don’t forget to drag and drop the ScriptManager control to the page.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;%@ Page Language=&amp;quot;C#&amp;quot; AutoEventWireup=&amp;quot;true&amp;quot; CodeFile=&amp;quot;ModalPopup.aspx.cs&amp;quot; Inherits=&amp;quot;ModalPopup&amp;quot; %&amp;gt;            &lt;br /&gt;&amp;lt;%@ Register Assembly=&amp;quot;AjaxControlToolkit&amp;quot; Namespace=&amp;quot;AjaxControlToolkit&amp;quot; TagPrefix=&amp;quot;cc1&amp;quot; %&amp;gt;             &lt;br /&gt;&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot; &amp;quot;&lt;a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;"&gt;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;lt;html xmlns=&amp;quot;&lt;a href="http://www.w3.org/1999/xhtml&amp;quot;"&gt;http://www.w3.org/1999/xhtml&amp;quot;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;lt;head runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;title&amp;gt;Modal Popup Demo&amp;lt;/title&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;style&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;.backgroundColor              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; background-color:Gray;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; filter:alpha(opacity=25);               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/style&amp;gt;             &lt;br /&gt;&amp;lt;/head&amp;gt;             &lt;br /&gt;&amp;lt;body&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:ScriptManager ID=&amp;quot;ScriptManager1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:ScriptManager&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;&amp;lt;asp:Button ID=&amp;quot;popupBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Click to see the modal popup.&amp;quot; /&amp;gt;              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Panel ID=&amp;quot;Panel1&amp;quot; runat=&amp;quot;server&amp;quot; Style=&amp;quot;&lt;font color="#800000"&gt;display: none;&lt;/font&gt; padding:10px; border:1px; border-style:solid;&amp;quot; BackColor=&amp;quot;#FF9933&amp;quot; Width=&amp;quot;400px&amp;quot;&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;h3 style=&amp;quot;text-align: center;&amp;quot; id=&amp;quot;header&amp;quot;&amp;gt;Modal Popup&amp;lt;/h3&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p&amp;gt;This is a Modal Popup extender control. You are seeing this because you have clicked the hyperlink button.&amp;lt;/p&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&amp;lt;asp:Button ID=&amp;quot;closeBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Close&amp;quot; /&amp;gt;&amp;lt;/p&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:Panel&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;cc1:ModalPopupExtender ID=&amp;quot;ModalPopupExtender&amp;quot; runat=&amp;quot;server&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TargetControlID=&amp;quot;popupBtn&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PopupControlID=&amp;quot;Panel1&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OkControlID=&amp;quot;closeBtn&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BackgroundCssClass=&amp;quot;backgroundColor&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DropShadow=&amp;quot;true&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PopupDragHandleControlID=&amp;quot;header&amp;quot;/&amp;gt;               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/div&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/form&amp;gt;             &lt;br /&gt;&amp;lt;/body&amp;gt;             &lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above code one can see I have a ScriptManager, an asp:Button control, an asp:Panel control, some contents inside the panel control and then we have the ModalPopupExtender control. Normally when you attach an ASP.NET panel control it will become invisible and if due to some reason if the panel control is being displayed in the page then just set the “display” to “none” as highlighted in Maroon in the above e.g. So when you click the button you will see a popup as shown below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;a href="http://lh6.ggpht.com/_QY7fSC4VqiA/Sk3Hv8q91bI/AAAAAAAAC5E/jRVw6idcvn0/s1600-h/image4.png"&gt;&lt;img style="display: inline" title="image" alt="image" src="http://lh5.ggpht.com/_QY7fSC4VqiA/Sk3HxvmdeYI/AAAAAAAAC5I/g4SO0M1h9cs/image_thumb2.png?imgmax=800" width="388" height="244" /&gt;&lt;/a&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Now lets see how to configure the ModalPopupExtender control. The configuration is pretty simple, you need to set some properties and and you will have your Modal popup. The properties are as follows&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;TargetControlID:&lt;/strong&gt; This property is the one which tells, on click of which control the popup should be displayed. In our e.g. we have given the TargetControlID as our button control. You can assign the ID of any ASP.NET controls like LinkButton, ImageButton, HyperLink, DropDownList, CheckBox, RadioButton etc. Even you can give a label’ id as the TargetControlID.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;PopupControlID: &lt;/strong&gt;The ID of the control to be displayed as a modal popup. In our e.g. we have given ASP.NET panel control’ ID. You can give any ASP.NET control’ ID. If for e.g if you give the ID of a textbox then the textbox alone will be popped up. Its not compulsory that you should provide a ASP.NET panel control.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;OkControlID: &lt;/strong&gt;The ID of the control which should act as an Ok button. Clicking of this control will dismiss the popup.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;BackgroundCssClass: &lt;/strong&gt;The name of the CSS class which needs to be applied to the background of the popup. One thing to note here is that if you don’t provide a CSS class then the modal popup will not function like a modal dialog. One will be able to interact with the controls at the background of the popup control, so its imperative to provide a valid CSS class name value to the BackgroundCssClass property. In the above e.g. we have defined a CSS class called “backgroundColor” in the header section of the aspx page. Please note in the CSS class definition we have applied “filter” property to make the background transparent.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;DropShadow: &lt;/strong&gt;Accepts a boolean value which specifies whether the popup should have shadow. True will give a shadow and false will disable the shadow.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;PopupDragHandleControlID: &lt;/strong&gt;The ID of the control clicking on which the ModalPopupExtender can be dragged. The control should be inside the control specified using PopupControlID i.e. in our case control inside the ASP.NET panel control. If a particular control ID is set as the PopupDragHandlerControlID then one can click on that control and drag the ModalPopupExtender control. In the above e.g. we have set the ID of the “h3” control as PopupDragHandleControlID, clicking which the popup can be dragged.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;CancelControlID: &lt;/strong&gt;If you want a button to behave as a Cancel button provide the ID of the button. This button will cancel the popup.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Drag: &lt;/strong&gt;This property takes a boolean value which when set decides whether the popup control can have the drag feature. A value of true means the popup extender control can be dragged around the screen whereas false will disable the drag.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;RepositionMode: &lt;/strong&gt;This property accepts four values. They are “None”, “RepositionOnWindowResize”, “RepositionOnWindowResizeAndScroll” and “RepositionOnWindowScroll”. The default value is “RepositionOnWindowResizeAndScroll”. Explanation of each values is given below.&lt;/p&gt;  &lt;p align="justify"&gt;RepositionOnWindowResize: Will reposition the popup when the window is resized.&lt;/p&gt;  &lt;p align="justify"&gt;RepositionOnWindowScroll: Will reposition the popup when the scroll bar is moved.&lt;/p&gt;  &lt;p align="justify"&gt;RepositionOnWindowResizeAndScroll: Will reposition the popup when you resize or when you move the scroll bar. Its a combination of both “RepositionOnWindowResize” and “RepositionOnWindowScroll”.&lt;/p&gt;  &lt;p align="justify"&gt;None: The popup will not be tinkered with. It will be shown in its original position irrespective of window resizing or scroll bar being moved. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;DynamicServicePath: &lt;/strong&gt;Link to a webservice/aspx page from where data or content needs to be retrieved and displayed in the popup extender control. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;DynamicServiceMethod: &lt;/strong&gt;The method name from which content needs to be retrieved. The method can be a webservice method or a method in your code behind file of your aspx file. If you leave the “DynamicServicePath” empty and provide DynamicServiceMethod name then the system will try to ping the same page’ and try to find a method. We will see an e.g. with web service shortly.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;DynamicContextKey: &lt;/strong&gt;The string value which will be passed as the parameter to the method specified in DynamicServiceMethod. One thing to note here is that the parameter should be named contextKey in the webservice/code behind.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;X: &lt;/strong&gt;The X coordinates for the popup control.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;Y: &lt;/strong&gt;The Y coordinates for the popup control.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Showing the content of a webservice (asmx) method in a ModalPopupExtender control.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Showing the output of a web method of a web service in a ModalPopupExtender control is also pretty simple. First you need to have a web service, if you don’t have one its very easy to create one in Visual Studio. Create a web service and add a method the output of which you want to display in a ModalPopupExtender conrol. Below I have pasted the code of my webservice.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;using System;            &lt;br /&gt;using System.Collections;             &lt;br /&gt;using System.Linq;             &lt;br /&gt;using System.Web;             &lt;br /&gt;using System.Web.Services;             &lt;br /&gt;using System.Web.Services.Protocols;             &lt;br /&gt;using System.Xml.Linq; &lt;/p&gt;          &lt;p&gt;[WebService(Namespace = &amp;quot;&lt;a href="http://tempuri.org/&amp;quot;)]"&gt;http://tempuri.org/&amp;quot;)]&lt;/a&gt;             &lt;br /&gt;/*The below attribute is needed so that the webservice can process calls from ASP.NET AJAX controls. Without the below attribute one may get the following error. &amp;quot;Web Service call failed: 500&amp;quot;*/             &lt;br /&gt;&lt;font color="#008000"&gt;[System.Web.Script.Services.ScriptService]&lt;/font&gt;             &lt;br /&gt;public class Service : System.Web.Services.WebService             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public Service ()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; /*Method which will be called by the ModalPopupExtender control.*/             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [WebMethod]             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string HelloWorld()             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;&amp;lt;b&amp;gt;Hello World&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&amp;quot;;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above webservice is pretty simple. It has only one web method and the most famous of them all, the “HelloWorld” method. The method returns a HTML formatted “Hello World” string. &lt;/p&gt;  &lt;p align="justify"&gt;One thing to note while writing a web service whose methods can be called by the ASP.NET Ajax controls is that you need to add the “[System.Web.Script.Services.ScriptService]” attribute. If the before mentioned attribute is not added then you may get the below web service error.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;“Web Service call failed: 500”&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The mark up for the ModalPopupExtender control to display the webservice’ web method output is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;!--Web service content display demo--&amp;gt;            &lt;br /&gt;&amp;lt;asp:Panel ID=&amp;quot;webServicePanel&amp;quot; runat=&amp;quot;server&amp;quot; Width=&amp;quot;300px&amp;quot; Height=&amp;quot;300&amp;quot; BackColor=&amp;quot;Azure&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;webServiceContentLbl&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Label&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;clsBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Button&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;lt;/asp:Panel&amp;gt;             &lt;br /&gt;&amp;lt;asp:Button ID=&amp;quot;webServiceCall&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Click to see the output of a webservice in a popup.&amp;quot; /&amp;gt;             &lt;br /&gt;&lt;font color="#008000"&gt;&amp;lt;cc1:ModalPopupExtender ID=&amp;quot;ModalPopupExtender1&amp;quot; runat=&amp;quot;server&amp;quot; TargetControlID=&amp;quot;webServiceCall&amp;quot; OkControlID=&amp;quot;clsBtn&amp;quot; &lt;font color="#800000"&gt;DynamicServicePath=&amp;quot;Service.asmx&amp;quot;&lt;/font&gt; &lt;font color="#800000"&gt;DynamicServiceMethod=&amp;quot;HelloWorld&amp;quot;&lt;/font&gt;&amp;#160;&amp;#160; &lt;font color="#800000"&gt;DynamicControlID=&amp;quot;webServiceContentLbl&amp;quot;&lt;/font&gt;&amp;#160; &lt;br /&gt;&amp;#160; BackgroundCssClass=&amp;quot;backgroundColor&amp;quot; PopupControlID=&amp;quot;webServicePanel&amp;quot; DropShadow=&amp;quot;true&amp;quot;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;lt;/cc1:ModalPopupExtender&amp;gt;&lt;/font&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The difference from our previous ModalPopupExtender control is that we have configured some extra properties related to webservice request. They are DynamicServicePath where we have given the url of the web service, DynamicServiceMethod takes the web method name and finally DynamicControlID takes the ID of the control in which the output of the web method should be displayed. We have given the ID of the label control inside the panel control as the DynamicControlID. The label control will display “Hello World” string in bold.&lt;/p&gt;  &lt;p align="justify"&gt;The ModalPopupExtender is capable of displaying the output from normal aspx pages as well. To display some content its not compulsory that you need to create a webservice. One can directly show the output of a method in a code behind file in a ModalPopupExtender. Lets see how to do that.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Displaying the content of a webpage (aspx) method in a ModalPopupExtender control.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Below is the code behind file content of the ASPX page. The code is pretty straight forward. It has a static method called HelloWorld which returns the same text as that of the webservice.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public partial class ModalPopup : System.Web.UI.Page            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; protected void Page_Load(object sender, EventArgs e)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; /// &amp;lt;summary&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; /// Page method which will be called by the modal popup extender control.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; /// &amp;lt;/summary&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.Web.Services.WebMethod(), System.Web.Script.Services.ScriptMethodAttribute()]              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static string HelloWorld()               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;&amp;lt;b&amp;gt;Hello World&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&amp;quot;;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;One thing to keep in mind when you declare a method in an aspx which needs to be accessed by the ModalPopupExtender control or any ASP.NET AJAX control is to apply the following attribute.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;[System.Web.Services.WebMethod(), System.Web.Script.Services.ScriptMethodAttribute()]&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In addition to the above method attributes one need to declare the method as a static method. If the method is not declared static then you may not be able to ping from the client side. In the case of web service the method need not be static. Some of the errors which you may face because of not declaring the method static or because of not applying the above attributes are listed below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;1. Web Service call failed: 12030          &lt;br /&gt;2. Web Service call failed: 500&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;If the above two errors popup you know what to do? First check whether the method attributes are applied and secondly see to it that the method is declared as static in the code behind.&lt;/p&gt;  &lt;p align="justify"&gt;Below is the mark up needed to display the content from a method in an aspx page.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;!--Web service content display demo--&amp;gt;            &lt;br /&gt;&amp;lt;asp:Panel ID=&amp;quot;webServicePanel&amp;quot; runat=&amp;quot;server&amp;quot; Width=&amp;quot;300px&amp;quot; Height=&amp;quot;300&amp;quot; BackColor=&amp;quot;Azure&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;outputContentLbl&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Label&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;clsBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Button&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;lt;/asp:Panel&amp;gt;             &lt;br /&gt;&amp;lt;asp:Button ID=&amp;quot;webServiceCall&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Click to see the output of a webservice in a popup.&amp;quot; /&amp;gt;             &lt;br /&gt;&lt;font color="#008000"&gt;&amp;lt;cc1:ModalPopupExtender ID=&amp;quot;ModalPopupExtender1&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; runat=&amp;quot;server&amp;quot;&amp;#160; &lt;font color="#800000"&gt;DynamicServicePath=&amp;quot;ModalPopup.aspx&amp;quot;                &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; TargetControlID=&amp;quot;webServiceCall&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OkControlID=&amp;quot;clsBtn&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#800000"&gt;DynamicServiceMethod=&amp;quot;HelloWorld&amp;quot;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#800000"&gt;DynamicControlID=&amp;quot;outputContentLbl&amp;quot;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BackgroundCssClass=&amp;quot;backgroundColor&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PopupControlID=&amp;quot;webServicePanel&amp;quot; DropShadow=&amp;quot;true&amp;quot;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;lt;/cc1:ModalPopupExtender&amp;gt;&lt;/font&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;You can see in the above mark up everything is same except for the webservice related properties, highlighted in Maroon. DynamicServicePath is used to specify the url of the page. If left blank it will refer the same page. DynamicServiceMethod is the method&amp;#160; name in the aspx/codebehind which will be pinged by the ModalPopupExtender to retrieve data. DynamicControlID is the ID of the control in which you would like to display the output of the method. DynamicControlID should have the ID of a control inside the ASP.NET Panel control in our case or any control inside whatever container control the user is making use for popup. Also if you have any argument then you can use DynamicContextKey to pass the string argument. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Hiding and Showing the ModalPopupExtender control using javascript&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;The above e.g. are quite straight forward and easy to implement. Unfortunately a developer’ life is not a bed of roses. There may arise a requirement where one has to call/show/hide the popup control using javascript. So lets see how to do exactly this. Below is the HTML markup.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&lt;font color="#008000"&gt;&amp;lt;asp:Button ID=&amp;quot;popupScriptBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Click to view the modal popup using javascript&amp;quot;&amp;#160;&amp;#160; OnClientClick=”javascript:showModalPopup();return false;” /&amp;gt;&lt;/font&gt;             &lt;br /&gt;&amp;lt;asp:Button ID=&amp;quot;popupBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Click to see the modal popup.&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;lt;div ID=&amp;quot;div1&amp;quot; runat=&amp;quot;server&amp;quot; &amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Panel ID=&amp;quot;Panel1&amp;quot; runat=&amp;quot;server&amp;quot; Style=&amp;quot;display: none; padding:10px; border:1px; border-style:solid;&amp;quot; BackColor=&amp;quot;#FF9933&amp;quot; Width=&amp;quot;400px&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;table id=&amp;quot;Table1&amp;quot; style=&amp;quot;background-color: #C0C0C0; width: 400px; height: 5px;&amp;quot;&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;h3 style=&amp;quot;text-align: center;&amp;quot; id=&amp;quot;header&amp;quot;&amp;gt;Modal Popup&amp;lt;/h3&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;/table&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p&amp;gt;This is a Modal Popup extender control. You are seeing this because you have clicked the hyperlink button.&amp;lt;/p&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&amp;lt;asp:Button ID=&amp;quot;closeBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Close&amp;quot; /&amp;gt;&amp;lt;/p&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:Panel&amp;gt;             &lt;br /&gt;&amp;lt;/div&amp;gt;             &lt;br /&gt;&amp;lt;cc1:ModalPopupExtender ID=&amp;quot;ModalPopupExtender&amp;quot; runat=&amp;quot;server&amp;quot; TargetControlID=&amp;quot;popupBtn&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; PopupControlID=&amp;quot;Panel1&amp;quot; OkControlID=&amp;quot;closeBtn&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; BackgroundCssClass=&amp;quot;backgroundColor&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; DropShadow=&amp;quot;true&amp;quot; PopupDragHandleControlID=&amp;quot;header&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CancelControlID=&amp;quot;&amp;quot; Drag=&amp;quot;true&amp;quot; RepositionMode=&amp;quot;None&amp;quot; /&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Above markup is pretty much same as the one we used at the beginning of the blog. The only difference being we have an extra ASP.NET button, clicking which we will show the ModalPopupExtender control with the help of javascript. In the above markup we have used the “OnClientClick” property to assign the javascript, showModalPopup, which needs to be called when the button is clicked. The javascript to show and hide the popup is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; language=&amp;quot;javascript&amp;quot;&amp;gt;            &lt;br /&gt;//Javascript function to hide the popup control.             &lt;br /&gt;function closeModalPopup()             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Using the $find javascript method to retrieve the modal popup control.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;var modalPopup = $find('&amp;lt;%=ModalPopupExtender.ClientID %&amp;gt;');              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (modalPopup != null)               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; modalPopup.hide();               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }               &lt;br /&gt;&lt;/font&gt;}             &lt;br /&gt;//Javascript function to show the ModalPopupExtender control.             &lt;br /&gt;function showModalPopup()             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; //Using the $find javascript method to retrieve the modal popup control.             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;var modalPopup = $find('&amp;lt;%=ModalPopupExtender.ClientID %&amp;gt;');              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (modalPopup != null)               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; modalPopup.show();               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&lt;/font&gt;}             &lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above javascript we have two functions. One to show the ModalPopupExtender control and another to hide the control. In both the function we are using $find shortcut method to retrieve the popup control. To know more about $find method please see this &lt;a href="http://sandblogaspnet.blogspot.com/2009/06/difference-between-get-and-find.html" target="_blank"&gt;link&lt;/a&gt;. Once we have retrieved the modal popup control we are calling the show and hide methods of the control to show and hide the control respectively.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;Using the modal popup control within a GridView control&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;There is nothing different here. In a grid if you want to link the ModalPopupExtender to a control inside the GridView you need to just convert the column into a template column and add the ModalPopupExtender control to the column and configure the necessary properties as discussed. Below is a sample code where I have added the ModalPopupExtender control in the header section as well in a column of GridView control.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:GridView ID=&amp;quot;backIssueGrid&amp;quot; runat=&amp;quot;server&amp;quot; AutoGenerateColumns=&amp;quot;False&amp;quot;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DataMember=&amp;quot;issue&amp;quot; DataSourceID=&amp;quot;backIssuesXML&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;RowStyle BorderColor=&amp;quot;Tan&amp;quot; BorderStyle=&amp;quot;Solid&amp;quot; BorderWidth=&amp;quot;1px&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Columns&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField HeaderText=&amp;quot;Back Issues&amp;quot; HeaderStyle-CssClass=&amp;quot;headerText&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:HyperLink ID=&amp;quot;title&amp;quot; runat=&amp;quot;server&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Text='&amp;lt;%# XPath(&amp;quot;@title&amp;quot;) %&amp;gt;' CssClass=&amp;quot;gridText&amp;quot; NavigateUrl='&amp;lt;%# XPath(&amp;quot;url&amp;quot;) %&amp;gt;'&amp;gt;&amp;lt;/asp:HyperLink&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;&amp;lt;asp:Panel ID=&amp;quot;webServicePanel&amp;quot; runat=&amp;quot;server&amp;quot; Width=&amp;quot;300px&amp;quot; Height=&amp;quot;300&amp;quot; BackColor=&amp;quot;Azure&amp;quot;&amp;gt;              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;outputContentLbl&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Label&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;clsBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Button&amp;quot; /&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:Panel&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;cc1:ModalPopupExtender ID=&amp;quot;ModalPopupExtender1&amp;quot; runat=&amp;quot;server&amp;quot; DynamicServicePath=&amp;quot;ModalPopup.aspx&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TargetControlID=&amp;quot;title&amp;quot; OkControlID=&amp;quot;clsBtn&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DynamicServiceMethod=&amp;quot;HelloWorld&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DynamicControlID=&amp;quot;outputContentLbl&amp;quot;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BackgroundCssClass=&amp;quot;backgroundColor&amp;quot; PopupControlID=&amp;quot;webServicePanel&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DropShadow=&amp;quot;true&amp;quot;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/cc1:ModalPopupExtender&amp;gt;               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderStyle CssClass=&amp;quot;headerText&amp;quot;&amp;gt;&amp;lt;/HeaderStyle&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle BorderColor=&amp;quot;Tan&amp;quot; BorderStyle=&amp;quot;Solid&amp;quot; BorderWidth=&amp;quot;1px&amp;quot; /&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField HeaderStyle-CssClass=&amp;quot;headerText&amp;quot;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;quantityTxt&amp;quot; Width=&amp;quot;30&amp;quot; runat=&amp;quot;server&amp;quot; Text='&amp;lt;%#XPath(&amp;quot;@noOfCopies&amp;quot;) %&amp;gt;' CssClass=&amp;quot;gridText&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle BorderColor=&amp;quot;Tan&amp;quot; HorizontalAlign=&amp;quot;Center&amp;quot; BorderStyle=&amp;quot;Solid&amp;quot; BorderWidth=&amp;quot;1px&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderStyle CssClass=&amp;quot;headerText&amp;quot;&amp;gt;&amp;lt;/HeaderStyle&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:ImageButton ID=&amp;quot;ImageButton1&amp;quot; runat=&amp;quot;server&amp;quot; ImageUrl=&amp;quot;some.gif&amp;quot; /&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;&amp;lt;asp:Panel ID=&amp;quot;webServicePanel2&amp;quot; runat=&amp;quot;server&amp;quot; Width=&amp;quot;300px&amp;quot; Height=&amp;quot;300&amp;quot; BackColor=&amp;quot;Azure&amp;quot;&amp;gt;              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;outputContentLbl1&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Label&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;clsBtn&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Button&amp;quot; /&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:Panel&amp;gt;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;cc1:ModalPopupExtender ID=&amp;quot;ModalPopupExtender2&amp;quot; runat=&amp;quot;server&amp;quot; DynamicServicePath=&amp;quot;ModalPopup.aspx&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TargetControlID=&amp;quot;ImageButton1&amp;quot; OkControlID=&amp;quot;clsBtn&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DynamicServiceMethod=&amp;quot;HelloWorld&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DynamicControlID=&amp;quot;outputContentLbl1&amp;quot;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; BackgroundCssClass=&amp;quot;backgroundColor&amp;quot; PopupControlID=&amp;quot;webServicePanel2&amp;quot;               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DropShadow=&amp;quot;true&amp;quot;&amp;gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/cc1:ModalPopupExtender&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/HeaderTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;Label4&amp;quot; CssClass=&amp;quot;gridText&amp;quot; runat=&amp;quot;server&amp;quot; Text='&amp;lt;%# Add(XPath(&amp;quot;@noOfCopies&amp;quot;), XPath(&amp;quot;@price&amp;quot;))%&amp;gt;'&amp;gt;&amp;lt;/asp:Label&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:CommandField ShowSelectButton=&amp;quot;True&amp;quot; ButtonType=&amp;quot;Button&amp;quot;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SelectText=&amp;quot;Remove&amp;quot; ControlStyle-CssClass=&amp;quot;buttonText&amp;quot; &amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ControlStyle CssClass=&amp;quot;buttonText&amp;quot;&amp;gt;&amp;lt;/ControlStyle&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle BorderColor=&amp;quot;Tan&amp;quot; BorderStyle=&amp;quot;Solid&amp;quot; BorderWidth=&amp;quot;1px&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:CommandField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/Columns&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;EditRowStyle BorderStyle=&amp;quot;Solid&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:GridView&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above e.g. you can see the markups highlighted in green. In the first portion I have place a popup control in the “ItemTemplate” and hooked the ModalPopupExtender control to a hyperlink control. Pretty simple, isn’t it? In the second section I have put the popup control in the HeaderTemplate of the column and hooked it to a ImageButton. So when one clicks on the image button in the header he will see a popup. If you want you can add javascript “onmouseover” and “onmouseout” behavior to the image in the code behind by adding attributes to the ImageButton control. &lt;/p&gt;  &lt;p align="justify"&gt;In my case I had to show help text whenever the user clicks the header text so instead of adding ModalPopupExtender control to each and every HeaderTemplate what I did is added the ModalPopupExtender control somewhere in the aspx page and hooked it to a hidden control and on click of the header, used javascript to display the popup control. The reason why I took this approach is that if I put the ModalPoupExtender in each column’ header template, it will generate as many popup extenders as the number of columns. My popup control needs to display help text and these help text were retrieved from a method in the code behind/webservice. So I used a javascript function and passed parameters necessary to retrieve the help text. Also people who have similar kind of requirement where for e.g. you have a column and you need to show some help text or some detailed information based on some parameters you can also follow the same approach. The approach will reduce the amount of html being generated. With this approach I wanted to highlight the subtle feature of ModalPopupExtender control where one can hook up the control to a hidden control and use javascript to show the control. This method can be very useful for controls to which ModalPopupExtender cannot be attached. I think thats enough, lets see the code in action.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;asp:GridView ID=&amp;quot;backIssueGrid&amp;quot; runat=&amp;quot;server&amp;quot;           &lt;br /&gt; AutoGenerateColumns=&amp;quot;False&amp;quot; DataMember=&amp;quot;issue&amp;quot;             &lt;br /&gt;DataSourceID=&amp;quot;backIssuesXML&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;RowStyle BorderColor=&amp;quot;Tan&amp;quot;             &lt;br /&gt;BorderStyle=&amp;quot;Solid&amp;quot; BorderWidth=&amp;quot;1px&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;Columns&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField             &lt;br /&gt;HeaderStyle-CssClass=&amp;quot;headerText&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:HyperLink ID=&amp;quot;title&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot; Text=&amp;quot;Name&amp;quot; CssClass=&amp;quot;gridText&amp;quot;             &lt;br /&gt;NavigateUrl='javascript:showModalPopup(&amp;quot;helpOne&amp;quot;            &lt;br /&gt;);'&amp;gt;&amp;lt;/asp:HyperLink&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/HeaderTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:HyperLink ID=&amp;quot;title&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot; Text='&amp;lt;%# XPath(&amp;quot;@title&amp;quot;) %&amp;gt;'             &lt;br /&gt;CssClass=&amp;quot;gridText&amp;quot; NavigateUrl='&amp;lt;%#             &lt;br /&gt;XPath(&amp;quot;url&amp;quot;) %&amp;gt;'&amp;gt;&amp;lt;/asp:HyperLink&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderStyle             &lt;br /&gt;CssClass=&amp;quot;headerText&amp;quot;&amp;gt;&amp;lt;/HeaderStyle&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle BorderColor=&amp;quot;Tan&amp;quot;             &lt;br /&gt;BorderStyle=&amp;quot;Solid&amp;quot; BorderWidth=&amp;quot;1px&amp;quot; /&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TemplateField             &lt;br /&gt;HeaderStyle-CssClass=&amp;quot;headerText&amp;quot;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox             &lt;br /&gt;ID=&amp;quot;quantityTxt&amp;quot; Width=&amp;quot;30&amp;quot; runat=&amp;quot;server&amp;quot;             &lt;br /&gt;Text='&amp;lt;%#XPath(&amp;quot;@noOfCopies&amp;quot;) %&amp;gt;'             &lt;br /&gt;CssClass=&amp;quot;gridText&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ItemTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle BorderColor=&amp;quot;Tan&amp;quot;             &lt;br /&gt;HorizontalAlign=&amp;quot;Center&amp;quot; BorderStyle=&amp;quot;Solid&amp;quot;             &lt;br /&gt;BorderWidth=&amp;quot;1px&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderStyle             &lt;br /&gt;CssClass=&amp;quot;headerText&amp;quot;&amp;gt;&amp;lt;/HeaderStyle&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;HeaderTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:HyperLink ID=&amp;quot;title&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot; Text=&amp;quot;Address&amp;quot; CssClass=&amp;quot;gridText&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; NavigateUrl='javascript:showModalPopup(&amp;quot;helpTwo&amp;quot;            &lt;br /&gt;);'&amp;gt;&amp;lt;/asp:HyperLink&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/HeaderTemplate&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:TemplateField&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:CommandField             &lt;br /&gt;ShowSelectButton=&amp;quot;True&amp;quot; ButtonType=&amp;quot;Button&amp;quot;             &lt;br /&gt;SelectText=&amp;quot;Remove&amp;quot; ControlStyle-CssClass=&amp;quot;buttonText&amp;quot; &amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ControlStyle             &lt;br /&gt;CssClass=&amp;quot;buttonText&amp;quot;&amp;gt;&amp;lt;/ControlStyle&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ItemStyle BorderColor=&amp;quot;Tan&amp;quot;             &lt;br /&gt;BorderStyle=&amp;quot;Solid&amp;quot; BorderWidth=&amp;quot;1px&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:CommandField&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/Columns&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;lt;/asp:GridView&amp;gt;             &lt;br /&gt;&amp;lt;asp:XmlDataSource ID=&amp;quot;backIssuesXML&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot;             &lt;br /&gt;DataFile=&amp;quot;~/Data/BackIssue.xml&amp;quot;&amp;gt;&amp;lt;/asp:XmlDataSou            &lt;br /&gt;rce&amp;gt;             &lt;br /&gt;&amp;lt;asp:HyperLink ID=&amp;quot;title&amp;quot; runat=&amp;quot;server&amp;quot;             &lt;br /&gt;Style=&amp;quot;display:none;&amp;quot;&amp;gt; &amp;lt;/asp:HyperLink&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Panel ID=&amp;quot;webServicePanel&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot; Width=&amp;quot;300px&amp;quot; Height=&amp;quot;300&amp;quot;             &lt;br /&gt;BackColor=&amp;quot;Azure&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Label ID=&amp;quot;outputContentLbl&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot; Text=&amp;quot;Label&amp;quot;&amp;gt;&amp;lt;/asp:Label&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;clsBtn&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot; Text=&amp;quot;Button&amp;quot; /&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:Panel&amp;gt;             &lt;br /&gt;&amp;lt;cc1:ModalPopupExtender ID=&amp;quot;ModalPopupExtender1&amp;quot;             &lt;br /&gt;runat=&amp;quot;server&amp;quot; DynamicServicePath=&amp;quot;ModalPopup.aspx&amp;quot;             &lt;br /&gt;TargetControlID=&amp;quot;title&amp;quot; OkControlID=&amp;quot;clsBtn&amp;quot;             &lt;br /&gt;DynamicServiceMethod=&amp;quot;HelloWorld&amp;quot;             &lt;br /&gt;DynamicControlID=&amp;quot;outputContentLbl&amp;quot;&amp;#160; &lt;br /&gt;BackgroundCssClass=&amp;quot;backgroundColor&amp;quot; PopupControlID=&amp;quot;webServicePanel&amp;quot; DropShadow=&amp;quot;true&amp;quot;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;lt;/cc1:ModalPopupExtender&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The above code has ASP.NET hyper link controls in the header template which calls a javascript function using its NavigateUrl property and passes a string value to the javascript function based on which the help text is retrieved. Also as you can see we have a ModalPopupExtender control outside of GridView. A javascript function is used to show the popup control when you click the hyper link controls inside the header template of the gridview. Also note the hyper link control outside the GridView which has its style set to “None” so that the control becomes invisible. The javascript which is executed on clicking the hyper link controls inside the GridView is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Javascript function to show the ModalPopupExtender control.            &lt;br /&gt;function showModalPopup(helpId)             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var modalPopup = $find('&amp;lt;%=ModalPopupExtender1.ClientID %&amp;gt;');             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (modalPopup != null)             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; modalPopup._DynamicContextKey = helpId;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; modalPopup.show();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above javascript we are setting the “_DynamicContextKey” with the help of the method argument. The argument is passed by the hyper link controls in the header template of the GridView. DynamicContextKey are nothing but the string arguments to be passed to webservice/webpage method. So by varying the DynamicContextKey we can get different messages to be displayed in the ModalPopupExtender control. &lt;/p&gt;  &lt;p align="justify"&gt;The code behind method which is called by the ModalPopupExtender control is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;[System.Web.Services.WebMethod(), System.Web.Script.Services.ScriptMethodAttribute()]           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static string HelloWorld(string contextKey)            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;&amp;lt;b&amp;gt;Hello World&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&amp;quot; + contextKey;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;So that’ about working with ModalPopupExtender control. Below are some of the important javascript properties and methods of ModalPopupExtender control. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td width="168"&gt;&lt;strong&gt;Method/Properties&lt;/strong&gt;&lt;/td&gt;        &lt;td width="243"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_DynamicControlID&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Using this property you can set/retrieve the ID for the dynamic control i.e. the control which will show the output of the method being pinged by the modal popup extender control.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_DynamicContextKey&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the string parameter for the function which will be called by the ModalPopupExtender control.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_DynamicServicePath&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the path for the webservice/webpage.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_DynamicServiceMethod&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the method which needs to be invoked or pinged to retrieve the output.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_PopupControlID&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;The property can be used to set/retrieve the ID of the control which will be shown as the popup. Normally it will be the ID of the ASP.NET panel control.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_PopupDragHandleControlID&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;The ID of the control clicking on which the popup control can be dragged. &lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_BackgroundCssClass&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;CSS class name for the background of the popup can be set/retrieved using this javascript property.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_DropShadow&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Boolean value which can be set/retrieved to enable drop shadow. A value of “true” means drop shadow is enabled, false means it is disabled.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_Drag&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the drag facility. If you want to enable set it as true.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_OkControlID&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;This property can be used to set/retrieve the “Ok” button. &lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_CancelControlID&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the “Cancel” button id.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_OnOkScript&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the script which needs to fired when “Ok” button inside the popup is clicked.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_OnCancelScript&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the script which needs to fired when “Cancel” button inside the popup is clicked.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_xCoordinate&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the X coordinates of the popup control.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_yCoordinate&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the Y coordinates of the popup control.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_repositionMode&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;Property to set/retrieve the Reposition mode. Reposition mode has been discussed somewhere else in this document. The property accepts integer as its value. The various values and their corresponding reposition mode are as follows. 0 means “None”, 1 means “RepositionOnWindowResize”, 2 means “RepositionOnWindowScroll” and finally 3 means            &lt;br /&gt;“RepositionOnWindowResizeAndScroll”. Each of this mode is discussed above.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="168"&gt;_id&lt;/td&gt;        &lt;td width="243"&gt;         &lt;p align="justify"&gt;One can set/retrieve the ID of the ModalPopupExtender control.&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;So that’ about our disussion on ModalPopupExtender control. &lt;/p&gt;  &lt;p align="justify"&gt;Try to know more,&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-1854500197317652648?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/oEQnvAYB8OIVSYbnLZhSbYyPYiw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/oEQnvAYB8OIVSYbnLZhSbYyPYiw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/oEQnvAYB8OIVSYbnLZhSbYyPYiw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/oEQnvAYB8OIVSYbnLZhSbYyPYiw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/WGsENAxsW8o" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/1854500197317652648/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/07/working-with-modal-popup-extender.html#comment-form" title="23 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/1854500197317652648?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/1854500197317652648?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/WGsENAxsW8o/working-with-modal-popup-extender.html" title="Working with Modal Popup extender control." /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>23</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/07/working-with-modal-popup-extender.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ADSXw6fip7ImA9WxJVFE4.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-8584595995271966552</id><published>2009-07-01T14:46:00.001+05:30</published><updated>2009-07-01T14:46:18.216+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-07-01T14:46:18.216+05:30</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Serialization" /><category scheme="http://www.blogger.com/atom/ns#" term="SOAP" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C# 3.0" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET 2.0" /><category scheme="http://www.blogger.com/atom/ns#" term="SOAP Serialization" /><title>Serialization in .NET - 3</title><content type="html">&lt;p align="justify"&gt;In my previous two article we discussed &lt;a href="http://sandblogaspnet.blogspot.com/2009/05/serialization-in-net-1.html" target="_blank"&gt;Binary Serialization&lt;/a&gt; and &lt;a href="http://sandblogaspnet.blogspot.com/2009/05/serialization-in-net-2.html" target="_blank"&gt;XML Serialization&lt;/a&gt;. This blog we will try to know more about SOAP (Simple Object Access Protocol) serialization. First up lets see how to serialize an object to SOAP format. We will take our good old Car class which most of you are very familiar with. The Car class is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public class Car            &lt;br /&gt;{&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Color             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Model             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int NoOfDoors             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int Price             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string CubicCentimeter             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public CarType Type             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;We will make use of the above car class for SOAP serialization. The code to serialize a class to SOAP format is similar to XML serialization with a small difference. The code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;Car bmw3Series = new Car{Model = &amp;quot;BMW 3 Series&amp;quot;, Color = &amp;quot;Blue&amp;quot;, NoOfDoors = 4,&amp;#160; CubicCentimeter=&amp;quot;3500cc&amp;quot;};            &lt;br /&gt;using (System.IO.Stream soapStream = new System.IO.FileStream(&amp;quot;SoapFormat.xml&amp;quot;, System.IO.FileMode.OpenOrCreate))             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;System.Xml.Serialization.SoapReflectionImporter soapRefImp = new System.Xml.Serialization.SoapReflectionImporter();              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; System.Xml.Serialization.XmlTypeMapping xmlTypeMapping =               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; soapRefImp.ImportTypeMapping(typeof(Car));               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; System.Xml.Serialization.XmlSerializer xmlSerializer =             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new System.Xml.Serialization.XmlSerializer(xmlTypeMapping);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; xmlSerializer.Serialize(soapStream, bmw3Series);             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;To serialize an object to SOAP format one can make use of XmlSerilizer class. When we initialize the XmlSerializer class for SOAP serialization instead of passing the type of the object we pass an instance of XmlTypeMapping class. This is the subtle difference. As you can see from the above code we are instantiating XmlTypeMapping object by passing the type of the Car object to the ImportTypeMapping method of SoapReflectionImporter class. SoapReflectionImporter class can be used whenever you want to serialize objects to SOAP formats. &lt;/p&gt;  &lt;p align="justify"&gt;The SOAP XML generated from the above code for the Car class is pasted below. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; ?&amp;gt;            &lt;br /&gt;&amp;lt;Car xmlns:xsi=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema-instance&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/a&gt; xmlns:xsd=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/a&gt; id=&amp;quot;id1&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Color xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;Blue&amp;lt;/Color&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Model xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;BMW 3 Series&amp;lt;/Model&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;NoOfDoors xsi:type=&amp;quot;xsd:int&amp;quot;&amp;gt;4&amp;lt;/NoOfDoors&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Price xsi:type=&amp;quot;xsd:int&amp;quot;&amp;gt;0&amp;lt;/Price&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;CubicCentimeter xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;3500cc&amp;lt;/CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;lt;/Car&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Another way to serialize objects is by making use of SoapFormatter class. To make use of the SoapFormatter class one has to add a reference to the “System.Runtime.Serialization.Formatters.Soap.dll” assembly. The sample code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;using (System.IO.Stream soapStream = new System.IO.FileStream(&amp;quot;SoapFormatter.xml&amp;quot;, System.IO.FileMode.Create))           &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; System.Runtime.Serialization.Formatters.Soap.SoapFormatter soapFormatter =             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; soapFormatter.Serialize(soapStream, bmw3Series);            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The SOAP xml generated from the above code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;SOAP-ENV:Envelope xmlns:xsi=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema-"&gt;http://www.w3.org/2001/XMLSchema-&lt;/a&gt;            &lt;br /&gt;instance&amp;quot; xmlns:xsd=&amp;quot;&lt;a href="http://www.w3.org/2001/"&gt;http://www.w3.org/2001/&lt;/a&gt;            &lt;br /&gt;XMLSchema&amp;quot; Xmlns:SOAP-ENC=&amp;quot;&lt;a href="http://schemas"&gt;http://schemas&lt;/a&gt;.            &lt;br /&gt;xmlsoap.org/soap/encoding/&amp;quot; xmlns:SOAP-ENV=            &lt;br /&gt;&amp;quot;&lt;a href="http://schemas.xmlsoap.org/soap/envelope/&amp;quot;"&gt;http://schemas.xmlsoap.org/soap/envelope/&amp;quot;&lt;/a&gt; xmlns:clr=&amp;quot;&lt;a href="http://schemas.microsoft.com/soap"&gt;http://schemas.microsoft.com/soap&lt;/a&gt;            &lt;br /&gt;/encoding/clr/1.0&amp;quot; SOAP-ENV:encodingStyle=            &lt;br /&gt;&amp;quot;&lt;a href="http://schemas.xmlsoap.org/soap/encoding/&amp;quot;"&gt;http://schemas.xmlsoap.org/soap/encoding/&amp;quot;&lt;/a&gt;&amp;gt;            &lt;br /&gt;&amp;lt;SOAP-ENV:Body&amp;gt;            &lt;br /&gt;&amp;lt;a1:Car id=&amp;quot;ref-1&amp;quot; xmlns:a1=&amp;quot;&lt;a href="http://schemas.microsoft.com/clr/"&gt;http://schemas.microsoft.com/clr/&lt;/a&gt;            &lt;br /&gt;nsassem/SoapSerialization/SerializationSample%            &lt;br /&gt;2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%            &lt;br /&gt;2C%20PublicKeyToken%3Dnull&amp;quot;&amp;gt;            &lt;br /&gt;&amp;lt;_x003C_Color_x003E_k__BackingField id=&amp;quot;ref-3&amp;quot;&amp;gt;Blue&amp;lt;/_x003C_Color_x003E_k__            &lt;br /&gt;BackingField&amp;gt;            &lt;br /&gt;&amp;lt;_x003C_Model_x003E_k__BackingField id=&amp;quot;ref-4&amp;quot;&amp;gt;BMW 3 Series&amp;lt;/_x003C_Model_x003E_k__BackingField&amp;gt;            &lt;br /&gt;&amp;lt;_x003C_NoOfDoors_x003E_k__BackingField&amp;gt;4            &lt;br /&gt;&amp;lt;/_x003C_NoOfDoors_x003E_k__BackingField&amp;gt;            &lt;br /&gt;&amp;lt;_x003C_Price_x003E_k__BackingField&amp;gt;0            &lt;br /&gt;&amp;lt;/_x003C_Price_x003E_k__BackingField&amp;gt;            &lt;br /&gt;&amp;lt;_x003C_CubicCentimeter_x003E_k__BackingField id=&amp;quot;ref-5&amp;quot;&amp;gt;3500cc&amp;lt;/_x003C_CubicCentimeter_            &lt;br /&gt;x003E_k__BackingField&amp;gt;            &lt;br /&gt;&amp;lt;/a1:Car&amp;gt;            &lt;br /&gt;&amp;lt;/SOAP-ENV:Body&amp;gt;            &lt;br /&gt;&amp;lt;/SOAP-ENV:Envelope&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;I would not recommend using SoapFormatter class as this class is obsolete from .NET 3.5 version. Also SoapFormatter class doesn’t support serialization of generic collections.&lt;/p&gt;  &lt;p align="justify"&gt;That's pretty much how objects can be serialized to SOAP format. As in the case with XML serialization there may be situation where you would like to control the way objects are serialized to SOAP format. This can be very much useful when you are using webservices where you would like to take control of the way your objects are serialized. Following are some of the attributes which can be applied to control object serialization.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;SoapAttribute&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;One can apply “SoapAttribute” to convert the property or field as a XML attribute rather than as a XML node which is the normal behavior. If you want to change the attribute name and don’t want it to be same as that of the property name, you can make use of the “AttributeName” property along with “SoapAttribute”. To provide a namespace to the xml attribute you can make use of the “Namespace” property. When you provide namespace it has to abide by w3 guidelines. As a general practice we provide urls as namespace. If you want to provide the data type to the attribute again that can be specified with the help DataType property. Sample code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public class Car            &lt;br /&gt;{&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.Xml.Serialization.SoapAttribute (AttributeName=&amp;quot;CarColor&amp;quot;, DataType=&amp;quot;string&amp;quot;, Namespace=&amp;quot;&lt;/font&gt;&lt;a href="http://car&amp;quot;)]"&gt;&lt;font color="#008000"&gt;http://car&amp;quot;)]&lt;/font&gt;&lt;/a&gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Color             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Model             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int NoOfDoors             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int Price             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string CubicCentimeter             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The serialized XML after applying the “SoapAttribute” is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;            &lt;br /&gt;&amp;lt;Car xmlns:xsi=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema-inst"&gt;http://www.w3.org/2001/XMLSchema-inst&lt;/a&gt;             &lt;br /&gt;ance&amp;quot;xmlns:xsd=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema&amp;quot;"&gt;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/a&gt; id=&amp;quot;id1&amp;quot;d1p1:&lt;font color="#008000"&gt;CarColor=&amp;quot;Blue&amp;quot; xmlns:d1p1=&amp;quot;&lt;/font&gt;&lt;a href="http://car&amp;quot;"&gt;&lt;font color="#008000"&gt;http://car&amp;quot;&lt;/font&gt;&lt;/a&gt;&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Model xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;BMW 3 Series&amp;lt;/Model&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;NoOfDoors xsi:type=&amp;quot;xsd:int&amp;quot;&amp;gt;4&amp;lt;/NoOfDoors&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;Price xsi:type=&amp;quot;xsd:int&amp;quot;&amp;gt;0&amp;lt;/Price&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;CubicCentimeter xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;             &lt;br /&gt;3500cc&amp;lt;/CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;lt;/Car&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above pasted XML you can see how the Color property has been added as a SOAP attribute to the Car node.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;SoapElement&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;SoapElement can be used to control the xml element’ attributes. Using the SoapElement attribute on a public property/field will convert the property/field to a XML element. The properties which can be used with SoapElement are, ElementName, DataType,&amp;#160; and IsNullable.&lt;/p&gt;  &lt;p align="justify"&gt;With ElementName one can provide a Name to the element if you don’t want the SOAP XML element name to be same as that of the property/field name.&lt;/p&gt;  &lt;p align="justify"&gt;DataType will help you to specify the SOAP XML schema data type. &lt;/p&gt;  &lt;p align="justify"&gt;IsNullable specifies whether a SOAP XML element node should be generated if the property/field is null. If the IsNullable value is true an empty XML element with xsi:nil attribute set to true will be generated for properties/fields having null value else no XML element node will be generated. As we have learned in XML serialization, if IsNullable is used in SOAP serialization it won’t throw any error even if it is used along with value type objects. In SOAP serialization if you have set IsNullable to true then for value type it will create a node with a default value assigned to it. For e.g. if you have used IsNullable for int data type and boolean data type then the SOAP xml will have nodes with 0 and false as their default value, if they are not initialized. Sample code with these implementation and the SOAP XML is pasted below. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public class Car            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.Xml.Serialization.SoapElement              &lt;br /&gt;(ElementName = &amp;quot;CarColor&amp;quot;, IsNullable = true)]               &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string Color             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.Xml.Serialization.SoapElement (ElementName=&amp;quot;CarModel&amp;quot;)]&lt;/font&gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string Model             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public int NoOfDoors             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.Xml.Serialization.SoapElement (ElementName = &amp;quot;CarPrice&amp;quot;, IsNullable = true)]              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public int Price             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public string CubicCentimeter             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;} &lt;/p&gt;          &lt;p&gt;//Car object to be serialized.            &lt;br /&gt;Car bmw3Series = new Car{Model = &amp;quot;BMW 3 Series&amp;quot;, NoOfDoors = 4, CubicCentimeter=&amp;quot;3500cc&amp;quot;}; &lt;/p&gt;          &lt;p&gt;//Serialized SOAP XML            &lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;             &lt;br /&gt;&amp;lt;Car xmlns:xsi=&amp;quot;&lt;a href="http://www.w3.org/2001/XMLSchema"&gt;http://www.w3.org/2001/XMLSchema&lt;/a&gt;             &lt;br /&gt;- instance&amp;quot; xmlns:xsd=&amp;quot;&lt;a href="http://www.w3.org/2001/"&gt;http://www.w3.org/2001/&lt;/a&gt;             &lt;br /&gt;XMLSchema&amp;quot; id=&amp;quot;id1&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160; &lt;font color="#008000"&gt;&amp;lt;CarColor xsi:nil=&amp;quot;true&amp;quot; /&amp;gt;              &lt;br /&gt;&lt;/font&gt;&amp;#160; &amp;lt;CarModel xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;BMW 3 Series&amp;lt;/CarModel&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;NoOfDoors xsi:type=&amp;quot;xsd:int&amp;quot;&amp;gt;4&amp;lt;/NoOfDoors&amp;gt;             &lt;br /&gt;&amp;#160; &lt;font color="#008000"&gt;&amp;lt;CarPrice xsi:type=&amp;quot;xsd:int&amp;quot;&amp;gt;0&amp;lt;/CarPrice&amp;gt;              &lt;br /&gt;&lt;/font&gt;&amp;#160; &amp;lt;CubicCentimeter xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;3500cc&amp;lt;/CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;lt;/Car&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above xml one can see there is “xsi:nill” XML attribute set to true for “CarColor” node as we have applied “IsNullable” true. Also notice “CarPrice” node having “0” as its value. Since we have used “IsNullable” it is generating a node with a default value. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;SoapEnum&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Can be used to describe how Enum needs to be serialized. It has only one property named Name using which you can specify what should be the name of the particular Enum value. Sample code is pasted below. &lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public enum CarType            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.Xml.Serialization.SoapEnum(Name = &amp;quot;CompactCar&amp;quot;)]              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; SmallCar,             &lt;br /&gt;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.Xml.Serialization.SoapEnum(Name= &amp;quot;CompactSedan&amp;quot;)]              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; CS,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Sedan,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SportsCar,             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Suv             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;If the “Name” property is specified then the enum’ member will have the name specified in the “Name” property else it will default to the member name.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;SoapIgnore&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If you want a public field or property not to be serialized then you can make use SoapIgnore attribute. This will prevent a particular public field or property from being serialized. SoapIgnore doesn’t have any property. The serialized XML and SoapIgnore attribute applied to Color property of the Car class is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;public class Car            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;[System.Xml.Serialization.SoapIgnore]&lt;/font&gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Color             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [System.Xml.Serialization.SoapElement (ElementName=&amp;quot;CarModel&amp;quot;)]             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string Model             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int NoOfDoors             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [System.Xml.Serialization.SoapElement(ElementName = &amp;quot;CarPrice&amp;quot;, IsNullable = true)]             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int Price             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string CubicCentimeter             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; { get; set; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;          &lt;p&gt;//Car object which needs to be serialized            &lt;br /&gt;Car bmw3Series = new Car{Model = &amp;quot;BMW 3 Series&amp;quot;,             &lt;br /&gt;NoOfDoors = 4, Color=&amp;quot;Blue&amp;quot;, CubicCentimeter=&amp;quot;3500cc&amp;quot;}; &lt;/p&gt;          &lt;p&gt;//Serialized XML with SoapIgnore attribute applied.            &lt;br /&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;             &lt;br /&gt;&amp;lt;Car xmlns:xsi=&amp;quot;&lt;a href="http://www.w3.org/2001/"&gt;http://www.w3.org/2001/&lt;/a&gt;             &lt;br /&gt;XMLSchema-instance&amp;quot; xmlns:xsd=&amp;quot;&lt;a href="http://www.w3.org"&gt;http://www.w3.org&lt;/a&gt;             &lt;br /&gt;/2001/XMLSchema&amp;quot; id=&amp;quot;id1&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;CarModel xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;BMW 3 Series&amp;lt;/CarModel&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;NoOfDoors xsi:type=&amp;quot;xsd:int&amp;quot;&amp;gt;4&amp;lt;/NoOfDoors&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;CarPrice xsi:type=&amp;quot;xsd:int&amp;quot;&amp;gt;0&amp;lt;/CarPrice&amp;gt;             &lt;br /&gt;&amp;#160; &amp;lt;CubicCentimeter xsi:type=&amp;quot;xsd:string&amp;quot;&amp;gt;3500cc&amp;lt;/CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;lt;/Car&amp;gt;500cc&amp;lt;/CubicCentimeter&amp;gt;             &lt;br /&gt;&amp;lt;/Car&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;From the above pasted SOAP XML one can note that Color attribute is missing because of the usage of SoapIgnore. &lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;SoapInclude&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p align="justify"&gt;SoapInclude can be used to specify the derived classes to be included in the SOAP format. For e.g. you have Car class and other classes like “CompactCar”, “Sedan” etc are deriving from Car class then you can make use SoapInclude. Suppose you have a class called CarCollection where you have a property called CarInstance which is a generic collection of base type Car. When the CarCollection object is serialized one has to tell the compiler what classes the collection can hold or derives from. If one is not specifying the inherited classes using the SoapInclude attribute the serialization process throws the following error.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;InvalidOperationException outer exception: &amp;quot;There was an error generating the XML document.&amp;quot;            &lt;br /&gt;InvalidOperationException inner exception: &amp;quot;The type SoapSerialization.CompactCar was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.&amp;quot;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;To avoid the above error you can use SoapInclude attribute. A sample code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Derived classes.            &lt;br /&gt;public class CompactCar : Car             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string SomeProperty             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;} &lt;/p&gt;          &lt;p&gt;public class Sedan : Car            &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public int SedanCapacity             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;}             &lt;br /&gt;\*Class has a generic collection of type car. One has to use SoapInclude to tell the serializer that the class can have classes of type CompactCar and Sedan.*/             &lt;br /&gt;&lt;font color="#008000"&gt;[System.Xml.Serialization.SoapInclude(typeof(CompactCar)),              &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Xml.Serialization.SoapInclude(typeof(Sedan))] &lt;/font&gt;&amp;#160;&amp;#160; &lt;br /&gt;public class CarCollection             &lt;br /&gt;{&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; public System.Collections.Generic.List&amp;lt;Car&amp;gt; CarInstance             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { get; set; }             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;Sometimes using the SoapInclude attribute may not solve the problem rather it can throw a new error. The error with inner exception is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;System.InvalidOperationException: &amp;quot;There was an error generating the XML document.&amp;quot;            &lt;br /&gt;System.InvalidOperationException: &amp;quot;Token StartElement in state Epilog would result in an invalid XML document.&amp;quot;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;To solve this you need to add a start element and end element before and after the serialized SOAP xml using XmlTextWriter’ WriteStartElement and WriteEndElement methods respectively. The logic behind this is to add a root node. The SOAP serialization code is modified and pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;using (System.IO.Stream soapStream = new System.IO.FileStream(&amp;quot;SoapFormat.xml&amp;quot;,            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; System.IO.FileMode.Create))             &lt;br /&gt;{             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; using (System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(soapStream, System.Text.Encoding.UTF8))             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Xml.Serialization.SoapReflectionImporter soapRefImp =             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new System.Xml.Serialization.SoapReflectionImporter();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Xml.Serialization.XmlTypeMapping xmlTypeMapping =             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; soapRefImp.ImportTypeMapping(typeof(CarCollection));             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System.Xml.Serialization.XmlSerializer xmlSerializer =             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new System.Xml.Serialization.XmlSerializer(xmlTypeMapping);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;xmlWriter.WriteStartElement(&amp;quot;carRoot&amp;quot;);              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; xmlSerializer.Serialize(xmlWriter, carCol);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#008000"&gt;xmlWriter.WriteEndElement();              &lt;br /&gt;&lt;/font&gt;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Deserializing a SOAP XML&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;Deserializing an SOAP XML is pretty simple and similar to the serialization logic. You need to create an instance of “SoapReflectionImporter” class and using the “ImportTypeMapping” method of “SoapReflectionImporter” create an instance of “XmlTypeMapping” class. Once these things are done create an instance of “XmlSerializer” class by passing the “XmlTypeMapping” object as one of the parameters. After this call the Deserialize method of the “XmlSerializer” class. The sample code is pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;System.Xml.Serialization.SoapReflectionImporter soapReflImp =           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new System.Xml.Serialization.SoapReflectionImporter();            &lt;br /&gt;System.Xml.Serialization.XmlTypeMapping xmlTypeMap =            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; soapReflImp.ImportTypeMapping(typeof(CarCollection));            &lt;br /&gt;System.Xml.Serialization.XmlSerializer xmlSerial =            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new System.Xml.Serialization.XmlSerializer(xmlTypeMap);            &lt;br /&gt;using (System.Xml.XmlTextReader xmlTextReader = new System.Xml.XmlTextReader(&amp;quot;SoapFormat.xml&amp;quot;))            &lt;br /&gt;{            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; /*Needed to read the root element. In the serialization code we have added the &amp;quot;carRoot&amp;quot; element.*/            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; xmlTextReader.ReadStartElement(&amp;quot;carRoot&amp;quot;);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CarCollection carColl = (CarCollection)xmlSerial.Deserialize(xmlTextReader);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; xmlTextReader.ReadEndElement();            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt; One can see from the above code that we are using “ReadStartElement” method of “XmlTextReader” class. This is done because we have manually added the “carRoot” element by making use of “WriteStartElement” method of “XmlTextWriter” while serializing the object. If “ReadStartElement” method is not used then the following error will be thrown.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;System.InvalidOperationException: &amp;quot;There is an error in XML document (1, 2).&amp;quot;           &lt;br /&gt;Inner exception - System.InvalidOperationException: &amp;quot;&amp;lt;carRoot xmlns=''&amp;gt; was not expected.&amp;quot;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;So that’s about SOAP serialization. Next we will see how to write custom serializers so that we can take control of serialization, till then try to learn more.&lt;/p&gt;  &lt;p&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-8584595995271966552?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/kVsmcVLxHvcxfIIkG0JwAphnB5U/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kVsmcVLxHvcxfIIkG0JwAphnB5U/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/kVsmcVLxHvcxfIIkG0JwAphnB5U/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kVsmcVLxHvcxfIIkG0JwAphnB5U/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/JjHwSq3JLUk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/8584595995271966552/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/07/serialization-in-net-3.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/8584595995271966552?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/8584595995271966552?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/JjHwSq3JLUk/serialization-in-net-3.html" title="Serialization in .NET - 3" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>2</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/07/serialization-in-net-3.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0EDQHo5eSp7ImA9WxJbEko.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-6000401356282206819</id><published>2009-06-23T16:03:00.001+05:30</published><updated>2009-07-22T19:37:51.421+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-07-22T19:37:51.421+05:30</app:edited><title>Reading from a configuration file in .NET.</title><content type="html">&lt;p align="justify"&gt;Recently I was working on a web application which had an exe added as a reference to the project. The exe had its own App.Config file where some setting related to the application was saved and these were read using the code below. Pretty straight forward code.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="410" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;private static string userName = ConfigurationSettings.AppSettings[&amp;quot;UserName&amp;quot;].ToString();            &lt;br /&gt;private static string userEmail = ConfigurationSettings.AppSettings[&amp;quot;UserEmail&amp;quot;].ToString(); &lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code we are trying to retrieve value from the AppSettings section of the App.Config file of the application using the ConfigurationSettings class’ AppSettings property which is a namevalue collection object. The above approach is a deprecated method. Instead of the above approach, one can make use of the below code to retrieve the appsettings values.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;private static string userName = ConfigurationManager.AppSettings[&amp;quot;UserName&amp;quot;].ToString();            &lt;br /&gt;private static string userEmail = ConfigurationManager.AppSettings[&amp;quot;UserEmail&amp;quot;].ToString();&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;ConfigurationSettings API has become obsolete and has been replaced with the ConfigurationManager class to read the configuration&amp;#160; setting of an applicatioin. To read the application settings or AppSettings of an application one can make use of ConfigurationManager.AppSettings. But my problem was not related to the API becoming obsolete. The system was throwing error when the system was trying to read the configuration. The errors are pasted below.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&lt;font color="#ff0000"&gt;The parameter 'exePath' is invalid.              &lt;br /&gt;Parameter name: exePath &lt;/font&gt;&lt;/p&gt;          &lt;p&gt;&lt;font color="#ff0000"&gt;An error occurred loading a configuration file: The parameter 'exePath' is invalid.              &lt;br /&gt;Parameter name: exePath &lt;/font&gt;&lt;/p&gt;          &lt;p&gt;&lt;font color="#ff0000"&gt;Object reference not set to an instance of an object. &lt;/font&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The reason why these errors were thrown is that while we were trying to read the keys from the configuration file the key were actually not present&amp;#160; in the “AppSettings” section. &lt;/p&gt;  &lt;p align="justify"&gt;As I said earlier the exe file was added as a reference in an ASP.NET web application and due to some reason when the exe code was accessed through the ASP.NET web application it was throwing error. The same exe application when executed independently was running without any exceptions. &lt;/p&gt;  &lt;p align="justify"&gt;On debugging it was found that, as the request was coming from the ASP.NET web application to the exe file the ConfigurationManager’ AppSettings property had values from the appSettings section of web.config file of the web application. But I wanted the appSettings values of the App.Config file of the exe application. The only solution to my problem was to read from the app.config file of the exe file. Below is the code to read from the configuration file.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;&lt;font color="#0000ff"&gt;string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “Bin”);              &lt;br /&gt;&lt;/font&gt;&lt;font color="#808080"&gt;/*If checking to check whether the bin folder exists. As this is a console application the exe can be copied to any place and can be executed. If that is the case there will be no bin folder. */&lt;/font&gt;             &lt;br /&gt;&lt;font color="#0000ff"&gt;if (!Directory.Exists(filePath))              &lt;br /&gt;{               &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; filePath = AppDomain.CurrentDomain.BaseDirectory;               &lt;br /&gt;}               &lt;br /&gt;&lt;/font&gt;&lt;font color="#808080"&gt;/*As the request for this class can come from both exe and website we need to combine the paths. We are first retrieving the path using the AppDomain' BaseDirectory propety and then retrieving the exe name alone. Finally passing the combined file path to ConfigurationManager class. */              &lt;br /&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;filePath = Path.Combine(filePath, Path.GetFileName(System.Reflection.Assembly.              &lt;br /&gt;GetExecutingAssembly().Location));               &lt;br /&gt;System.Configuration.Configuration theConfig = ConfigurationManager.               &lt;br /&gt;OpenExeConfiguration(filePath); &lt;/font&gt;&lt;/p&gt;          &lt;p&gt;&lt;font color="#0000ff"&gt;KeyValueConfigurationCollection configuration = theConfig.AppSettings.Settings;              &lt;br /&gt;userName = configuration[&amp;quot;UserName&amp;quot;] == null ? string.Empty :               &lt;br /&gt;configuration[&amp;quot;UserName&amp;quot;].Value;               &lt;br /&gt;userEmail = configuration[&amp;quot;UserEmail&amp;quot;] == null ? string.Empty :               &lt;br /&gt;configuration[&amp;quot;UserEmail&amp;quot;].Value;&lt;/font&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code I am using AppDomain class to retrieve the directory from where the application is executing itself. This approach was taken because the exe will run on its own or can be copied and pasted anywhere and it can run from there or as in my case the request can come from an ASP.NET application. If the exe is added as a reference in an ASP.NET web application, the exe will be copied to the bin folder of the ASP.NET application. So you can see there is a checking to see if there is a “Bin” folder, if there is one the app.config settings will be read from there else it will read from the base directory.&lt;/p&gt;  &lt;p align="justify"&gt;Once the file path along with the exe file name is properly formed we are using ConfigurationManager’ OpenExeConfiguration method to open the configuration file of the exe file. One thing to note here is that we are using “GetExecutingAssembly().Location” property and retrieving the file name and passing the application name along with the extension (.exe) to the OpenExeConfiguraiton method. There is no need to specify “app.config” file name. The reason is whenever an exe is successfully compiled it will create a config file with the same name with a .config extension. For e.g. if you have an exe named “one.exe”, if it is successfully compiled it will create a config file with “one.exe.config” as the name. &lt;/p&gt;  &lt;p align="justify"&gt;Once the config file is successfully opened using “OpenExeConfiguration” method it returns a Configuration object. After successfully retrieving the configuration object, in the next line we are getting the application’ settings using the “AppSettings.Settings” property. The “Settings” property returns a “KeyValueConfigurationCollection” object. Using KeyValueConfigurationCollection object one can read the values of the configuration file. In the above code we are using “configuration[&amp;quot;UserName&amp;quot;]” to read the value stored against the “UserName” key. Here the “UserName” is the key. So that’ about reading configuration settings from a configuration file of an application.&lt;/p&gt;  &lt;p align="justify"&gt;Try to know more.&lt;/p&gt;  &lt;p align="justify"&gt;Sandeep&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-6000401356282206819?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/EvPfBC609XeBCYs3qDNgL2g0AQQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/EvPfBC609XeBCYs3qDNgL2g0AQQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/EvPfBC609XeBCYs3qDNgL2g0AQQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/EvPfBC609XeBCYs3qDNgL2g0AQQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/06fPt8xaUy4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/6000401356282206819/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/06/reading-from-configuration-file-in-net.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/6000401356282206819?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/6000401356282206819?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/06fPt8xaUy4/reading-from-configuration-file-in-net.html" title="Reading from a configuration file in .NET." /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>3</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/06/reading-from-configuration-file-in-net.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0EDR3c-fyp7ImA9WxJWEEo.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-5085490283442162056</id><published>2009-06-05T13:04:00.001+05:30</published><updated>2009-06-15T19:51:16.957+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-06-15T19:51:16.957+05:30</app:edited><title>Difference between $get and $find</title><content type="html">&lt;p align="justify"&gt;In my previous blogs I have made use of $get and $find javascript methods. These are new shortcut methods available in ASP.NET AJAX. To use these methods one has include ScriptManager in an aspx page. What are these two methods all about? Lets see one by one.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;$get&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;$get is the shortcut method for document.getElementById. If you put a break point in the $get function and step into the function you can see the following code.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;var $get = Sys.UI.DomElement.getElementById = function Sys$UI$DomElement$getElementById(id, element) {&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var e = Function._validateParams(arguments, [             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {name: &amp;quot;id&amp;quot;, type: String},             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {name: &amp;quot;element&amp;quot;, mayBeNull: true, domElement: true, optional: true}             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ]);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (e) throw e;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (!element) return document.getElementById(id);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (element.getElementById) return element.getElementById(id);             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var nodeQueue = [];             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var childNodes = element.childNodes;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; for (var i = 0; i &amp;lt; childNodes.length; i++) {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var node = childNodes[i];             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (node.nodeType == 1) {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nodeQueue[nodeQueue.length] = node;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; while (nodeQueue.length) {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; node = nodeQueue.shift();             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (node.id == id) {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return node;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; childNodes = node.childNodes;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; for (i = 0; i &amp;lt; childNodes.length; i++) {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; node = childNodes[i];             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (node.nodeType == 1) {             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nodeQueue[nodeQueue.length] = node;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return null;             &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above code one can infer that the signature of the shortcut method is something like this.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;$get(id, element)&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The function argument “id” takes a string value where one can pass the id of the element to be retrieved. “element” is an optional parameter, if passed the code will try to find a control inside the element if it supports getElementById else it will loop through all the child nodes and match the id of the child node with the id passed as the function argument. Below sample code shows the usage of the $get.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;//Getting the ASP.NET server control' id using the ClientID.            &lt;br /&gt;var txtCntl = $get('&amp;lt;% =txtChange.ClientID%&amp;gt;');             &lt;br /&gt;//Getting a control which is not a server control             &lt;br /&gt;var divCntl = $get('contentDiv');             &lt;br /&gt;//Passing the id of the control as well the control in which the controls needs to be searched.             &lt;br /&gt;var dropDownCntl = $get('&amp;lt;% =ddCountries.ClientID %&amp;gt;', divCntl);&lt;/p&gt;          &lt;p&gt;--Html tag for the above controls is pasted below--            &lt;br /&gt;&amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div id=&amp;quot;contentDiv&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:DropDownList ID=&amp;quot;ddCountries&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:DropDownList&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:TextBox ID=&amp;quot;txtChange&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/asp:TextBox&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:Button ID=&amp;quot;Button1&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot;Button&amp;quot; /&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/div&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;asp:ScriptManager ID=&amp;quot;ScriptManager1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;             &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/asp:ScriptManager&amp;gt;             &lt;br /&gt;&amp;lt;/form&amp;gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;In the above code we have seen the different ways of retrieving a control using the $get function. If you see the javascript code pasted above one can see in the first line we are trying to retrieve a ASP.NET server control using &lt;a href="http://sandblogaspnet.blogspot.com/2009/03/calling-aspnet-server-side-function.html" target="_blank"&gt;inline server code&lt;/a&gt; where we are using ClientID property to retrieve the system generated ID for the server control. In the second line we are using the id of the Div tag to retrieve the div control. Finally in the third line we are making use of ClientID and then passing the div control retrieved in the second line as the second argument to $get function. What happens in the third approach is that the $get function tries to find the control inside the div tag. So that’ about the $get function. &lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;$find&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;$find is the shortcut method for “Sys.Application.findComponent(id, parent)”. The function takes id as a string parameter and also the control in which the component needs to be searched. The second parameter is optional. If provided it will look for the component in the component or element passed as an argument. The signature of the method is as follows.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;$find(id, parent)&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;The method looks for components registered with the addComponent javascript method. If you put a debug point and step into the function you can see the below code.&lt;/p&gt;  &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="400" bgcolor="#ffffff"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;         &lt;p&gt;function Sys$_Application$findComponent(id, parent) {           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var e = Function._validateParams(arguments, [            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {name: &amp;quot;id&amp;quot;, type: String},            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {name: &amp;quot;parent&amp;quot;, mayBeNull: true, optional: true}            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ]);            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if (e) throw e;            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // Need to reference the application singleton directly beause the $find alias            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // points to the instance function without context. The 'this' pointer won't work here.            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return (parent ?            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ((Sys.IContainer.isInstanceOfType(parent)) ?            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parent.findComponent(id) :            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; parent[id] || null) :            &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Sys.Application._components[id] || null);            &lt;br /&gt;}&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p align="justify"&gt;From the above code you can see that the code first checks whether the parent element is of type IContainer if it is then it tries to find the component within the parent component else it returns the component from the _components array in the Sys.Application object.&lt;/p&gt;  &lt;p align="justify"&gt;&lt;strong&gt;&lt;u&gt;When to use $get and $find?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p align="justify"&gt;$get javascript shortcut method can be used instead of document.getElementById whereas $find can be used to find extender controls. If you use $get to retrieve extender controls like ValidatorCalloutExtender, AutoCompleteExtender etc it will return null.&lt;/p&gt;  &lt;p align="justify"&gt;So that's the difference between $get and $find.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1454785102765291191-5085490283442162056?l=sandblogaspnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/dV5t-M0w86XKtIye2rIn1UTisgM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dV5t-M0w86XKtIye2rIn1UTisgM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/dV5t-M0w86XKtIye2rIn1UTisgM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dV5t-M0w86XKtIye2rIn1UTisgM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/AspnetCAndnet/~4/20KYbK9rXHM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://sandblogaspnet.blogspot.com/feeds/5085490283442162056/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://sandblogaspnet.blogspot.com/2009/06/difference-between-get-and-find.html#comment-form" title="13 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5085490283442162056?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1454785102765291191/posts/default/5085490283442162056?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AspnetCAndnet/~3/20KYbK9rXHM/difference-between-get-and-find.html" title="Difference between $get and $find" /><author><name>Sandeep P R</name><uri>http://www.blogger.com/profile/17475854334307316616</uri><email>sndppr@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="08150350429301672305" /></author><thr:total>13</thr:total><feedburner:origLink>http://sandblogaspnet.blogspot.com/2009/06/difference-between-get-and-find.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkMMQ3w8eip7ImA9WxFQFUo.&quot;"><id>tag:blogger.com,1999:blog-1454785102765291191.post-4352819899969699967</id><published>2009-06-01T19:04:00.001+05:30</published><updated>2010-05-11T15:04:42.272+05:30</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-05-11T15:04:42.272+05:30</app:edited><title>Anonymous Types – Features of C# 3.0 (Part – 5)</title><content type="html">&lt;p align="justify"&gt;In my previous blogs we have discussed the various new features of C# 3.0. These are listed below.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/02/extension-methods-features-of-c-30-part.html" target="_blank"&gt;Extension Methods&lt;/a&gt;  &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/03/automatic-properties-features-of-c-30.html" target="_blank"&gt;Automatic Properties&lt;/a&gt;  &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/04/implicitly-typed-variable.html" target="_blank"&gt;Implicitly Typed variables&lt;/a&gt;  &lt;li&gt;&lt;a href="http://sandblogaspnet.blogspot.com/2009/04/object-initializers-and-collection.html" target="_blank"&gt;Object Initializers and Collection Initializers&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p align="justify"&gt;In this blog we will see what are Anonymous types? &lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;u&gt;Anonymous Types&lt;/u&gt;&lt;/strong&gt;&amp;nbsp; &lt;/p&gt; &lt;p align="justify"&gt;As the name suggests anonymous types are types which don’t have any concrete type. This doesn’t mean that anonymous types are not strongly typed. Anonymous types help you in creating real time objects without the hassle of creating a class and declaring an object for the same. Normally in .NET or any OOPS programming language we create object by first creating a class for the object and then instantiate an object for the class using the new keyword. With anonymous types one need not do all these, the developer can use the “new” keyword along with the “&lt;a href="http://sandblogaspnet.blogspot.com/2009/04/implicitly-typed-variable.html" target="_blank"&gt;var&lt;/a&gt;” keyword and &lt;a href="http://sandblogaspnet.blogspot.com/2009/04/object-initializers-and-collection.html" target="_blank"&gt;Object Initializer&lt;/a&gt; to create an anonymous object. The sample code for creating anonymous object is shown below.&lt;/p&gt; &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;var anonymousObj = new { FirstName = "Sandeep", LastName = "P.R" };&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p align="justify"&gt;In the above code you can see I am creating an Anonymous object with two properties “FirstName” and “LastName” by making use of &lt;a href="http://sandblogaspnet.blogspot.com/2009/04/object-initializers-and-collection.html" target="_blank"&gt;Object Initializer&lt;/a&gt; feature. One can access the properties defined in the Anonymous type using the Anonymous type’ variable name. One will get the intellisense feature as well with anonymous types. The below screenshot shows the intellisense displaying the properties defined above.&lt;/p&gt; &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;a href="http://lh6.ggpht.com/_QY7fSC4VqiA/SiPYo58HzYI/AAAAAAAAC28/yu_P_a0Dq1c/s1600-h/image%5B4%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_QY7fSC4VqiA/SiPYy02Ez1I/AAAAAAAAC3A/SKR8exYBRjo/image_thumb%5B2%5D.png?imgmax=800" width="386" height="106"&gt;&lt;/a&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p align="justify"&gt;If you closely examine the intellisense you can see some methods. These methods are the ones available in the System.Object class of .NET. The reason why these methods are available is that anonymous types implicitly inherit from .NET’ System.Object class. Just to prove that Anonymous types implicitly inherit from System.Object I have disassembled the exe using .NET Reflecter. The screenshot pasted below proves just that.&lt;/p&gt; &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;a href="http://lh3.ggpht.com/_QY7fSC4VqiA/SifOZz041OI/AAAAAAAAC3E/Khczf4p9iOE/s1600-h/image%5B5%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_QY7fSC4VqiA/SifOay4O7qI/AAAAAAAAC3I/hKxwA9mZuKw/image_thumb%5B2%5D.png?imgmax=800" width="387" height="232"&gt;&lt;/a&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p align="justify"&gt;As said previously one need not create class/type for instantiating an anonymous type but behind the scene the compiler does the extra work of creating the class/type and giving it a name. In the above screenshot one can see compiler generated class name highlighted in blue. The auto generated name cannot be accessed in your code and that is the reason the “var” keyword is used whenever anonymous types are instantiated. Anonymous types are reference types (class) and are not different from other user defined types. In user defined types the type is created by the user whereas in the case of anonymous type the compiler does the extra work of creating the type. The code pasted below is the compiler generated code for the anonymous type that we created in the above code.&lt;/p&gt; &lt;table border="1" bordercolor="#000000" bordercolorlight="#ffffff" bordercolordark="#ffffff" width="663" bgcolor="#ffffff"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; &lt;p&gt;[&lt;font color="#800000"&gt;CompilerGenerated&lt;/font&gt;, DebuggerDisplay(@"\{ FirstName = {FirstName}, LastName = {LastName} }", Type="&amp;lt;Anonymous Type&amp;gt;")] &lt;br&gt;&lt;font color="#800000"&gt;internal sealed class&lt;/font&gt; &amp;lt;&amp;gt;f__AnonymousType0&amp;lt;&amp;lt;FirstName&amp;gt;j__TPar, &amp;lt;LastName&amp;gt;j__TPar&amp;gt; &lt;br&gt;{ &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Fields &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [DebuggerBrowsable(DebuggerBrowsableState.Never)] &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private &lt;font color="#800000"&gt;readonly&lt;/font&gt; &amp;lt;FirstName&amp;gt;j__TPar &amp;lt;FirstName&amp;gt;i__
