Friday, January 27, 2012

WCF Service Host in asp.net

IIS 7 Hosting The main advantage of hosting service in IIS is that, it will automatically launch the host process when it gets the first client request. It uses the features of IIS such as process recycling, idle shutdown, process health monitoring and message based activation. The main disadvantage of using IIS is that, it will support only HTTP protocol. Let as do some hands on, to create service and host in IIS
Step 1:Start the Visual Studio 2008 and click File->New->Web Site. Select the 'WCF Service' and Location as http. This will directly host the service in IIS and click OK. (create one folder in physical location i.e D:\IISHostService next create one website in IIS and map this folder to IIS website)










Step 2: I have created sample HelloWorld service, which will accept name as input and return with 'Hello' and name. Interface and implementation of the Service is shown below.
IMyService.cs
[ServiceContract]
public interface IMyService
{
[OperationContract]
string HelloWorld(string name);
}
MyService.cs
public class MyService : IMyService
{
#region IMyService Members
public string HelloWorld(string name)
{
return "Hello " + name;
}
#endregion
}
Step 3: Service file (.svc) contains name of the service and code behind file name. This file is used to know about the service.
MyService.svc
<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/MyService.cs" %>
Step 4: Server side configurations are mentioned in the config file. Here I have mention only one end point which is configured to 'wsHttpBinding', we can also have multiple end point with differnet binding. Since we are going to hosted in IIS. We have to use only http binding. We will come to know more on endpoints and its configuration in later tutorial. Web.Config
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="MyService">
<endpoint address="http://localhost:9703/IISHostedService/MyService.svc"
binding="wsHttpBinding" contract="IMyService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the
metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for
debugging purposes, set the value below to true.
Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Note:
You need to mention the service file name, along with the Address mention in the config file. IIS Screen shot










This screen will appear when we run the application.

Thursday, January 26, 2012

How to write html/javascript code in blogger blog post?

How to write html/javascript code in blogger blog post? write html/javascript code in blogger blog postIf you have ever tried writing HTML/Javascript code in your blogger blog post then you might have found that it does not appear as code in the post. Instead the code is interpreted and displayed. The above problem can be simply fixed by replacing < with "Ampersand(&)lt;" and > by "Ampersand(&)gt;" . Here we are providing a few elegant ways to do this. We will start with the toughest one first. If you like easy things just skip to the last step.



Method 1 Open notepad and paste your code in it. Hit ctrl+h or choose Edit > Replace In Find what: box type < and in Replace with: box type "Ampersand(&)lt;" and hit Replace all button Similarly for > type > in Find what: box and "Ampersand(&)gt;" in Replace with: box. Your are done, the code is ready to be used in your post.



Method 2 Paste your code in the text box below and hit Convert button. Done! Your code is ready to be pasted in your blogger blog post. For easy access we suggest that you bookmark (ctrl+d) this page.



Method 3 The above method requires you to visit this page whenever you want to convert your code. This is waste of time and will also require your to have an active internet connection. Let's make things a bit easier download html converter now.



Method 4 The next method is the official solution from blogger. If you need to write html code in your post just select post options and in compose settings choose Show HTML literally. Any code now written will not be interpreted as HTML.

follow following link: http://bloggerfunda.blogspot.com/2009/11/how-to-write-htmljavascript-code-in.html

Wednesday, January 25, 2012

Grideview DataKeys in Asp.net Using C# code

GrideviewName.DataKeyNames = new string[] { "pid", "catId" };
OR

mension datakeys in the source code like in the Gridtag
asp:gridview DataKeyNames=pid,catId
In the above we are given datakeys names pid,catId

Get Datakeys values(pid,catId):

GridviewName.DataKeys[e.RowIndex].Values["Pid"]);
GridviewName.DataKeys[e.RowIndex].Values["catId"]);

Grid view Row Updation using Onblur event of TextBox in Javascript

Note:if you want Update gridview row when you changing the text in the textbox and you click on the out gridview or on the grideview
you can written Onblur event of textbox.
Ex:I update Quantity in the textbox by click on the webpage or gridview.
I written Onblur() event in the textbox as follows.this will call server side code

function changeQuantity(obj) {
if (obj.value == 0)
obj.value = 1;
document.getElementById('<%= ButtonName.ClientID %>').click();

server side Code:
protected void btnCheck_OnClick(object sender, EventArgs e)
{
foreach (GridViewRow item in GVCart.Rows)
{
string lPid = Convert.ToString(GVCart.DataKeys[item.RowIndex].Values["pid"]);
TextBox lQty = (TextBox)item.FindControl("txtQty");
}

}

TextBox event Onblur Is:
onblur="changeQuantity(this)"

Enter numbers only in TextBox using javascript code

function onlynumbers(event) {
if (event.keyCode == 46 || event.keyCode == 8) {
return true;
}
else {
// Ensure that it is a number and stop the keypress
if ((event.keyCode < 48 || event.keyCode > 57)) {
alert('enter only numbers....');
return false;
}
else {
return true;
}
}
this function can be written the the TextBox keypress event like

onkeypress="return onlynumbers(event)"

Monday, January 23, 2012

Email validation using Java Script

function validate()
{
var emailformat = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ //regular expression defining a 8 digit number
var email = document.getElementById('<%=txtEmail.ClientID %>').value;
if (email == "") {
alert('Enter emailid...');
return false;
document.getElementById('<%=txtEmail.ClientID %>').focus();
}
if (email.search(emailformat) == -1) { //if match failed
alert("please enter valid emailid...");
return false;
document.getElementById('<%=txtEmail.ClientID %>').focus();
}
}
Call this function in OnClientClick of Button like

OnClientClick="javascript:return validate();"

Wednesday, January 4, 2012

Clear/Remove QueryString value in asp.net

In general query string not clear using following
Request.QueryString.Remove("id");
Request.QueryString.Clear();
you get error like Collection readolny

but we can solve this following code

PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
// make collection editable
isreadonly.SetValue(this.Request.QueryString, false, null);
// remove
this.Request.QueryString.Remove("id");