Wednesday, December 17, 2008

Specifying static port number for web application

Here are the steps to specify static port number to web application.
1) Right click on project name and select properties.
2) In the properties window select web tab from the left tabs.
3) In project web properties select “Specific port” option which is under servers and specify the port number.

Thursday, December 11, 2008

Using HttpWebRequest & HttpWebResponse in XML Web Services

Calling a web method using HttpWebRequest and HttpWebResponse.
Service1.asmx.cs
---------------------
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
namespace WebService1
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}
}
}
Class1.cs
---------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net;
using System.IO;
namespace WebService1
{
public class Class1
{
public string GetResponse(string InputXML, string WebserviceURL, string strSoapAction)
{
string postData = InputXML;
UTF8Encoding encoding = new UTF8Encoding();
byte[] postBytes = encoding.GetBytes(postData);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(WebserviceURL);
req.Method = "POST";
req.ContentType = "text/xml; charset=utf-8";
req.Headers.Add("SOAPAction", strSoapAction);
req.ContentLength = postBytes.Length;
Stream postStreem = req.GetRequestStream();
postStreem.Write(postBytes, 0, postBytes.Length);
postStreem.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader reader = new StreamReader(res.GetResponseStream());
return reader.ReadToEnd();
}
}
}

Method call:
-------------

Class1 obj = new Class1();
string str = obj.GetResponse(TextBox1.Text, "http://localhost:62202/Service1.asmx", "http://tempuri.org/Add");
Response.Write("Response from Webservice:
Sum of two numbers: " + str);

Ref and Out parameters in c#

C# 'ref' and 'out' parameters?
Ref and out parameter passing modes are used to allow a method to alter variables passed in by the caller. The difference between ref and out is subtle but important. Each parameter passing mode is designed to apply to a slightly different programming scenario. The important difference between out and ref parameters is the definite assignment rules used by each.
The caller of a method which takes an out parameter is not required to assign to the variable passed as the out parameter prior to the call; however, the callee is required to assign to the out parameter before returning.
Here's a simple example:
class OutExample
{
// Splits a string containing a first and last name separated
// by a space into two distinct strings, one containing the first name and one containing the last name

static void SplitName(string fullName, out string firstName, out string lastName)
{
// NOTE: firstName and lastName have not been assigned to yet. Their values cannot be used.
int spaceIndex = fullName.IndexOf(' ');
firstName = fullName.Substring(0, spaceIndex).Trim();
lastName = fullName.Substring(spaceIndex).Trim();
}

static void Main()
{
string fullName = "Yuan Sha";
string firstName;
string lastName;

// NOTE: firstName and lastName have not been assigned yet. Their values may not be used.
SplitName(fullName, out firstName, out lastName);
// NOTE: firstName and lastName have been assigned, because the out parameter passing mode guarantees it.

System.Console.WriteLine("First Name '{0}'. Last Name '{1}'", firstName, lastName);
}
}
One way to think of out parameters is that they are like additional return values of a method. They are very convenient when a method returns more than one value, in this example firstName and lastName. Out parameters can be abused however. As a matter of good programming style if you find yourself writing a method with many out parameters then you should think about refactoring your code. One possible solution is to package all the return values into a single struct.
In contrast ref parameters are considered initially assigned by the callee. As such, the callee is not required to assign to the ref parameter before use. Ref parameters are passed both into and out of a method.
Here's an example:
class RefExample
{
static object FindNext(object value, object[] data, ref int index)
{
// NOTE: index can be used here because it is a ref parameter
while (index < data.Length)
{
if (data[index].Equals(value))
{
return data[index];
}
index += 1;
}
return null;
}

static void Main()
{
object[] data = new object[] {1,2,3,4,2,3,4,5,3};

int index = 0;
// NOTE: must assign to index before passing it as a ref parameter
while (FindNext(3, data, ref index) != null)
{
// NOTE: that FindNext may have modified the value of index
System.Console.WriteLine("Found at index {0}", index);
index += 1;
}

System.Console.WriteLine("Done Find");
}
}
The two parameter passing modes addressed by out and ref are subtly different, however they are both very common. The subtle difference between these modes leads to some very common programming errors. These include:
not assigning a value to an out parameter in all control flow paths
not assigning a value to variable which is used as a ref parameter
Because the C# language assigns different definite assignment rules to these different parameter passing modes, these common coding errors are caught by the compiler as being incorrect C# code.
The crux of the decision to include both ref and out parameter passing modes was that allowing the compiler to detect these common coding errors was worth the additional complexity of having both ref and out parameter passing modes in the language.

Wednesday, December 10, 2008

Debugging JavaScript in Visual studio 2005

Steps for debugging javascript in 2005 .net:
1) Add "javascript:debugger;" as the first statement of your javascript function.
2) Enable script debugging in internet explorer settings.

Tuesday, December 9, 2008

Using CollectionBase

Sample code for System.Collections.CollectionBase implementaion.
public class Attributes : CollectionBase
{
private string _key;
private string _value;

public string Key
{
get { return this._key; }
set { this._key = value; }
}

public string Value
{
get { return this._value; }
set { this._value = value; }
}

public Attributes this[int index]
{
get
{
return (Attributes)List[index];
}
set
{
List[index] = value;
}
}

public string this[string Key]
{
get
{
string returnValue = "";
if (this.Count > 0)
{
foreach (Attributes attr in this)
{
if (attr.Key.ToLower().Equals(Key.ToLower()))
{
returnValue = attr.Value;
break;
}
}
}
return returnValue;
}
}

public bool Contains(Attributes attribute)
{
return List.Contains(attribute);
}

public int Add(Attributes attribute)
{
return List.Add(attribute);
}

public void Insert(int index, Attributes attribute)
{
List.Insert(index, attribute);
}

public void Remove(Attributes attribute)
{
List.Remove(attribute);
}

public new void RemoveAt(int index)
{
List.RemoveAt(index);
}
}

Building XML documentation from XML comments

From Visual studio 2005:
1. In Solution Explorer, right click on the project and then click Properties.
2. In project properties window select build tab on the left side.
3. In build properties window, check XML documentation file checkbox of output properties.
The XML output file will be in debug/release directory.

From Command Prompt:
Type the below commnad in command promt:
csc [FileName.cs] /doc: [OutputFileName.xml]
Ex: csc XMLsample.cs /doc:XMLsample.xml