Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for November, 2009

Write View State at bottom of the Page for SEO Friendly Site

Posted by Viral Sarvaiya on November 24, 2009


first of all create class file named ‘basepage.vb’ and add this code to class file

then after inherit this class file from page

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim stringWriter As New System.IO.StringWriter
Dim htmlWriter As New HtmlTextWriter(stringWriter)
MyBase.Render(htmlWriter)
Dim html As String = stringWriter.ToString()
Dim StartPoint As Integer = html.IndexOf(“<input type=”"hidden”" name=”"__VIEWSTATE”"”)
If StartPoint >= 0 Then
Dim EndPoint As Integer = html.IndexOf(“/>”, StartPoint) + 2
Dim viewstateInput As String = html.Substring(StartPoint, EndPoint – StartPoint)
html = html.Remove(StartPoint, EndPoint – StartPoint)
Dim FormEndStart As Integer = html.IndexOf(“</form>”) – 1
If FormEndStart >= 0 Then
html = html.Insert(FormEndStart, viewstateInput)
End If
End If
StartPoint = html.IndexOf(“<input type=”"hidden”" name=”"__EVENTTARGET”"”)
If StartPoint >= 0 Then
Dim EndPoint As Integer = html.IndexOf(“/>”, StartPoint) + 2
Dim viewstateInput As String = html.Substring(StartPoint, EndPoint – StartPoint)
html = html.Remove(StartPoint, EndPoint – StartPoint)
Dim FormEndStart As Integer = html.IndexOf(“</form>”) – 1
If FormEndStart >= 0 Then
html = html.Insert(FormEndStart, viewstateInput) ‘
End If
End If
StartPoint = html.IndexOf(“<input type=”"hidden”" name=”"__EVENTARGUMENT”"”)
If StartPoint >= 0 Then
Dim EndPoint As Integer = html.IndexOf(“/>”, StartPoint) + 2
Dim viewstateInput As String = html.Substring(StartPoint, EndPoint – StartPoint)
html = html.Remove(StartPoint, EndPoint – StartPoint)
Dim FormEndStart As Integer = html.IndexOf(“</form>”) – 1
If FormEndStart >= 0 Then
html = html.Insert(FormEndStart, viewstateInput) ‘
End If
End If
writer.Write(html)
End Sub

Advertisements

Posted in ASP.NET | Tagged: , , , , | Leave a Comment »

Date and Time Format Strings

Posted by Viral Sarvaiya on November 21, 2009


Date and time format strings are used to specify which formatting to apply when a datetime data type is represented as a string.

Date and time format strings fall into either standard or custom format strings. Standard datetime format strings are those that the framework has predefined for commonly used datetime patterns, while custom datetime format strings are supplied in cases where the standard format specifiers do not provide the type of formatting required.

Standard DateTime Format Strings

Standard datetime format strings are used to return string equivalents of datetime types in predefined patterns. When used with ToString, a standard datetime format string parameter takes the form

object.ToString ( "F" )

where F is a single alphabetic character, which can be any of the built-in format characters specified below.

The following table describes the standard datetime format strings. Note that the patterns produced by these format specifiers are influenced by the settings in the Regional Options control panel. Computers using different cultures or different currency settings will display different patterns.

Format specifier Name Description
d Short date pattern Displays a pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread or by a specified format provider.
D Long date pattern Displays a pattern defined by the DateTimeFormatInfo.LongDatePattern property associated with the current thread or by a specified format provider.
t Short time pattern Displays a pattern defined by the DateTimeFormatInfo.ShortTimePattern property associated with the current thread or by a specified format provider.
T Long time pattern Displays a pattern defined by the DateTimeFormatInfo.LongTimePattern property associated with the current thread or by a specified format provider.
f Full date/time pattern ( short time ) Displays a combination of the long date and short time patterns, separated by a space.
F Full date/time pattern ( long time ) Displays a pattern defined by the DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by a specified format provider.
g General date/time pattern ( short time ) Displays a combination of the short date and short time patterns, separated by a space.
G General date/time pattern ( long time ) Displays a combination of the short date and long time patterns, separated by a space.
M or m Month day pattern Displays a pattern defined by the DateTimeFormatInfo.MonthDayPattern property associated with the current thread or by a specified format provider.
R or r RFC1123 pattern Displays a pattern defined by the DateTimeFormatInfo.RFC1123Pattern property associated with the current thread or by a specified format provider. This is a defined standard and the property is read-only; therefore, it is always the same regardless of the culture used, or the format provider supplied. The property references the CultureInfo.InvariantCulture property and follows the custom pattern ‘ddd, dd MMMM yyyy HH:mm:ss G\MT’. Note that the ‘M’ in ‘GMT’ needs an escape character so it is not interpreted.
s Sortable date/time pattern; conforms to ISO 8601 Displays a pattern defined by the DateTimeFormatInfo.SortableDateTimePattern property associated with the current thread or by a specified format provider. The property references the CultureInfo.InvariantCultureproperty, and the format follows the custom pattern ‘yyyy-MM-ddTHH:mm:ss’.
u Universal sortable date/time pattern Displays a pattern defined by the DateTimeFormatInfo. UniversalSortableDateTimePattern property associated with the current thread or by a specified format provider. Because it is a defined standard and the property is read-only, the pattern is always the same regardless of culture or format provider. The format follows the custom pattern ‘yyyy-MM-dd HH:mm:ssZ’.
U Universal sortable date/time pattern Displays a pattern defined by the DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by a specified format provider. Note that the time displayed is for the Universal, rather than local time.
Y or y Year month pattern Displays a pattern defined by the DateTimeFormatInfo.YearMonthPattern property associated with the current thread or by a specified format provider.
Any other single character Unknown specifier

The date and time separators displayed by format strings are defined by the DateSeparator and TimeSeparator characters associated with the DateTimeFormat property of the current culture. However, in cases where the InvariantCulture is referenced by the r, s, and u specifiers, the characters associated with the DateSeparator and TimeSeparator characters do not change based on the current culture.

All datetime format strings that consist of a single alphabetic character ( without a leading or trailing white space ) are interpreted as standard datetime format strings.

If a string is interpreted as a standard datetime format string and contains one of the standard datetime format specifiers, then the datetime value is formatted accordingly. However, if a string is interpreted as a standard format string but does not contain one of the standard format specifiers, then a FormatException is thrown.

Any datetime format string that does not fit the definition of a standard datetime format string is interpreted as a custom datetime format string.

For More Reference

http://authors.aspalliance.com/aspxtreme/aspnet/types/datetimeformatstrings.aspx

http://www.123aspx.com/Rotor/RotorSrc.aspx?rot=40515

Posted in ASP.NET | Tagged: , , , | Leave a Comment »

Windows Communication Foundation

Posted by Viral Sarvaiya on November 18, 2009


Windows Communication Foundation (WCF) is the latest service execution environment from Microsoft that enables you to seamlessly expose CLR types as services and consume services as CLR types. WCF is a unified programming model that combines the best of breed features from XML Web Services, .NET Remoting, MSMQ, and COM+ into an integrated platform that is completely based on a set of open industry standards. Because of that, WCF provides interoperability between services, and it promotes productivity, including the essential off-the-shelf plumbing required by almost any application. This article series will discuss the essential building blocks of WCF describing the concepts and architecture of WCF. The first installment focuses on the basics of WCF by introducing you to the WCF through simple examples and discussion. The future installments will go into more specific features of WCF such as transactions, security, instance management techniques and so on.

Prerequisites

  • Visual Studio 2005 Professional Edition
  • Microsoft Pre-Release Software Microsoft .NET Framework 3.0 – Release Candidate
  • Microsoft Visual Studio Code Name “Orcas” Community Technology Preview – Development Tools for .NET Framework 3.0
  • Microsoft® Windows® Software Development Kit for RC 1

Introduction to WCF

WCF is a set of .NET Framework-based technologies for building and running services. WCF is the next step in the evolutionary path that started with COM, COM+, and MSMQ, and continues through .NET Remoting, ASMX, System.Messaging, and .NET Enterprise Services. WCF combines these technologies and offers a unified programming model for developing services using any CLR-compliant language. The below screenshot provides a conceptual representation of the different components that make up the WCF architecture.

As you can see from the above, the major subsystems of WCF are the Service model, the connector framework, hosting environments, and system and messaging services. The WCF service model makes service-oriented development explicit and simple from any CLR-targeted language. You use declarative attributes to mark up which aspects of their type should form the external contract of a service. The service model supports different types of contracts including service contracts, operation contracts and so on, which will be discussed later in this article. The service model also provides instance and context management for a service. WCF routes incoming messages to instances of user-defined service types. As a developer, you can leverage the declarative attributes to control how instances are associated with incoming messages as well as how the session management features of Indigo routes multiple messages to a common session object. Finally, the service model provides declarative behaviors that automate security and reliability. All of the above functionalities of service model are contained in the System.ServiceModel namespace.

Key Components of a WCF Service

A WCF service program contains four elements:

  • Contract definitions – A service must have at least one service contract, and it might contain multiple service contracts, data contracts, or message contracts
  • Endpoint definitions – One or more address-binding-contract endpoints must be declared
  • Hosting code – Some code is needed to create and start the service
  • Implementation code – The service contracts in a service need code to implement their service operations

Let us look at each of these components in detail.

Understanding Contracts

Contracts are one of the fundamental concepts in WCF. They allow clients and services to have a common understanding of available operations, data structures, and message structures while remaining loosely coupled and platform independent. WCF includes four kinds of contracts:

  • Service contract – Describes the operations a service can perform. A service contract is defined with the [ServiceContract] and [OperationContract] attributes. Binding requirements can be specified for the contract with a [BindingRequirements] attribute.
  • Data contract – Describes a data structure. A data contract is defined primarily with the [DataContract] and [DataMember] attributes.
  • Message contract – Defines what goes where in a message. A message contract is defined primarily with the [MessageContract], [MessageBodyMember], and [MessageHeader] attributes.
  • Fault contract – Allows you to document the errors that WCF code is likely to produce. A fault contract is specified along with the operation contract at the time of declaring the method. A fault contract is defined using the [FaultContract] attribute.

All four types of contracts translate between Microsoft .NET types used internally and the XML representations shared externally:

  • A service contract converts between the CLR and Web Services Description Language (WSDL)
  • A data contract converts between the CLR and XML Schema Definition (XSD)
  • A message contract converts between the CLR and Simple Object Access Protocol (SOAP)
  • A fault contract converts the CLR exceptions and to SOAP faults

You define contracts by using familiar object-oriented constructs: interfaces and classes. By decorating interfaces and classes with attributes, you create contracts.

Understanding Service Contracts

A service contract describes the operations a service can perform. A service must have at least one service contract, and it can have more than one. You can think of a service contract as follows:

  • It describes the client-callable operations (functions) exposed by the service
  • It maps the interface and methods of your service to a platform-independent description
  • It describes message exchange patterns that the service can have with another party. Some service operations might be one-way; others might require a request-reply pattern
  • It is analogous to the <portType> element in WSDL

You define a service contract by annotating an interface (or class) with the [ServiceContract] attribute. You identify service operations by placing [OperationContract] attributes on the methods of the interface. The following service contract defines only one service operation.

[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string HelloWorld (string input);
}

Once you define a service contract using an interface, you can write a class to implement the interface. For example:


public class HelloWorldService : IHelloWorldService
{
public string HelloWorld(string input)
{
return "Hello " + input;
}
}

It is also possible for you to define the service contract directly against the implementation class and skip the interface altogether. The following class both defines and implements a service contract.

[ServiceContract]
public class HelloWorldService
{
[OperationContract]
public string HelloWorld(string input)
{
return "Hello " + input;
}
}

Although this approach works, it is not a recommended way to create services as interfaces allow you to separate the implementation from the definition of a service.

Understanding Endpoints

Services expose one or more endpoints where messages can be sent. Each endpoint consists of an address, a binding, and a contract. The address specifies where to send messages. The binding describes how to send messages. And the contract describes what the messages contain. Clients need to know this information before they can access a service. The below picture demonstrates how the components of end point play a key role in communication between a client and a service.

Services can package up endpoint descriptions to share with clients, typically by using Web Services Description Language (WSDL). Then clients can use the provided service description to generate code within their environment capable of sending and receiving the proper messages .

One of the key components of an end point is bindings, which will be the topic of focus in the next section.

Understanding Bindings

The bindings of a WCF service define how an endpoint will communicate with an external client. A binding has several characteristics, including the following:

  • Transport protocols – Some of the choices include HTTP, Named Pipes, TCP, and MSMQ.
  • Encoding – Three types of encoding are available-Text, Binary, or Message Transmission Optimization Mechanism (MTOM). MTOM is an interoperable message format that allows the effective transmission of attachments or large messages (greater than 64K).
  • Security – Includes wire security (SSL) or schema-defined security (WS-Security).

Bindings can also determine if you are using sessions or a transacted communications channel. You have the choice of creating custom channels or using prebuilt bindings. WCF comes shipped with nine built-in bindings. The following table lists the WCF build-in bindings and their associated features.

Binding Description
BasicHttpBinding Basic Web service communication. No security by default
WSHttpBinding Web services with WS-* support. Supports transactions
WSDualHttpBinding Web services with duplex contract and transaction support
WSFederationHttpBinding Web services with federated security. Supports transactions
MsmqIntegrationBinding Communication directly with MSMQ applications. Supports transactions
NetMsmqBinding Communication between WCF applications by using queuing. Supports transactions
NetNamedPipeBinding Communication between WCF applications on same computer. Supports duplex contracts and transactions
NetPeerTcpBinding Communication between computers across peer-to-peer services. Supports duplex contracts
NetTcpBinding Communication between WCF applications across computers. Supports duplex contracts and transactions

You can choose the right binding to use by mapping the features they support to your requirements.

Hosting WCF Services

As for where to put your code and what it compiles to, you have some choices. You can host your service in Internet Information Services (IIS), or you can write a small amount of extra code to host a service yourself. You can self-host a service from just about any environment that supports managed code, including a WinForms application, console application, library assembly (DLL), or Windows Service controlled through SCM (Service Control Manager). The following lists show the common hosting mechanisms for WCF services.

  • IIS (Internet Information Services) – IIS provides a number of advantages if the service uses HTTP as its transport. The nice thing about using IIS is that you don’t have to write any hosting code as part of the application since IIS automatically activates service code as required. Services also benefit from IIS features such as process lifetime management and automatic restart after configuration changes. To run services using IIS, you create the service code along with its configuration file and simply save them in an IIS virtual directory.
  • WAS (Windows Activation Service) – (WAS) is the new process activation mechanism that ships with IIS 7.0. WAS builds on the existing IIS 6.0 process and hosting models, but is no longer dependent on HTTP. In addition to HTTP based communication, WCF can also use WAS to provide message-based activation over other protocols, such as TCP and named pipes. This helps WCF applications to take advantage of WAS features, such as process recycling, rapid fail protection, and the common configuration system, which were previously available only to HTTP-based applications.
  • Self-hosting – WCF services can be hosted inside any managed application, such as console applications and Windows Forms or Windows Presentation Foundation (WPF) graphical applications. To accomplish this, you need to create a class that implements a WCF service contract interface, and specify binding information in the application configuration file. The application code can then use an instance of System.ServiceModel.ServiceHost to make the service available at a particular location. To start the service, you call the ServiceHost.Open() method.
  • Managed Windows Service – A WCF service can be registered as a Windows Service, so that it is under control of the Service Control Manager (SCM). This is suitable for long-running WCF services that are hosted outside of IIS in a secure environment and are not message-activated. By hosting a WCF service with Windows Services, you take advantage of Windows service features such as automatic start at start time and control by the SCM. To host a WCF service in this way, the application must be written as a Managed Windows Service by inheriting from System.ServiceProcess.ServiceBase. It must also implement a WCF service contract interface and then create and open a ServiceHost to manage the WCF service.

Throughout this article series, I will demonstrate each of these hosting mechanisms as we explore the different features of WCF.

Advantages

Windows Communication Foundation has some important enhancements in comparison with preceding technologies.

· It merges all older separate technologies in one place and allows you to do things easier.

· It has rich communication capabilities.

· It comes with many powerful and ready to use enterprise features.

· It can be integrated with other technologies and has great interoperability.

Implementation of a Simple WCF Service

In this section, you will see the steps involved in creating a simple WCF service. Since the focus of this section is to get you familiar with the service development steps, I will keep the service implementation simple. The service is a simple MathService that implements a service contract named IMathService. This exercise has six development steps:

  1. Create the service
  2. Choose the service hosting mechanism
  3. Create the service configuration file
  4. Create the client
  5. Create the proxy and configuration file for the client
  6. Implement and run the client

1.  Create the Service

To start with, create a new project named WCFBasics using Visual Studio 2005 as shown in the below screenshot.

Once the project is created, add a new WCF Service using the Website->Add New Item menu option and name the service MathService as shown below:

After the service is created, you should see a MathService.svc file in your project with the below line of code.


 <%@ ServiceHost Language="C#" Debug="true" Service="MathService" CodeBehind="~/App_Code/MathService.cs" %>

Note that the MathService.svc has a code-behind file (named MathService.cs) that is placed in the App_Code directory. Open up the MathService.cs and modify its code to look as follows:

using System;
using System.ServiceModel;

[ServiceContract]
public interface IMathService
{
[OperationContract]
int Add(int x, int y);

[OperationContract]
int Subtract(int x, int y);

[OperationContract]
int Divide(int x, int y);
}

public class MathService : IMathService
{
public int Add(int x, int y)
{
return x + y;
}

public int Subtract(int x, int y)
{
return x - y;
}

public int Divide(int x, int y)
{
return x / y;
}
}

2.  Choose the Service Hosting Mechanism

As mentioned before, WCF is flexible because its services can be hosted in different types of applications. For the purposes of this installment, let us use IIS for hosting the WCF services because of its simplified configuration.

3.  Create the Service Configuration File

Configuration-based development permits part of an application’s behavior to be specified in configuration files. You can make such things as addresses, bindings, security details, service behavior, and tracing changeable without having to modify and recompile the program code. By specifying addresses, bindings, and behaviors in a configuration file, you can make deploy-time decisions about services that don’t require any modification to the service programs themselves.

As mentioned before, by using IIS you can take advantage of IIS features such as service activation, process recycling and so on. To host the MathService in IIS, modify the Web.config in the Web service project to look as follows:


<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service name="MathService"
behaviorConfiguration="MathServiceBehavior">
<endpoint address="" contract="IMathService"
binding="wsHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MathServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

Through its attributes, the <service> element under <system.ServiceModel>\<services> specifies the name of the service as well as the name of the behavior configuration that will be used to control the behavior of the service. The <service> element also contains another child element named <endpoint> that enables you to specify the address, contract, and binding for the service. In this case, since IIS is the hosting mechanism, the address attribute is set to “”. For an IIS-hosted application, the endpoint address should be defined by the physical *.svc file and the name of its hosting virtual directory and should have an empty value unless you would like to give it an extension and assign a different set of bindings to it. For example, in this case if the endpoint address=””, then the actual communication endpoint is http://localhost/MyProjects/15Seconds/WCFBasics/MathService.svc. Note that the contract attribute is set to the name of the interface, which is IMathService in this case. To expose the service through HTTP, the binding attribute is set to “wsHttpBinding”.

4.  Create the Client

To test the service, let us create an ASP.NET Web site named WCFBasicsServiceClient with Visual C# as the language of choice. Once the Web site is created, you now need to create a proxy for the WCF service that was created earlier. In addition, you also need to create a configuration file that contains the configuration settings required to connect to the service. You can accomplish both of these through a new utility called svcutil.exe. You will see this tool in action in the next section.

5.  Create the Proxy and Configuration file for the Client

The Service Model Metadata Utility (svcutil.exe) builds the proxy and the corresponding configuration file based on the published service’s metadata (WSDL + Schema + Policy). Once the service is up, running and listening, you can run svcutil.exe against the service endpoint address. For example, if it were an IIS-hosted service, it would be something like the following:

svcutil.exe http://localhost/MyProjects/15Seconds/WCFBasics/MathService.svc?WSDL

To use the svcutil utility, open up the command prompt through Start->Programs->Microsoft Visual Studio 2005->Visual Studio Tools->Visual Studio 2005 Command Prompt.

As you can see from the above screenshot, the output has two files.

  1. A generated typed WCF proxy in a preferred language that translates method calls to dispatched message. By default, the proxy is generated in C#.
  2. An output.config file for the client implementation that corresponds with the technical deployment details of the configuration file of the service side.

6.  Implement the Client

Now that you have created the proxy and the configuration file, you are ready to work with them from the client application. Before that, create a new ASP.NET Web site named WCFBasicsServiceClient using Visual C# as the programming language. Once the Web site is created, add the proxy file MathService.cs to the App_Code folder. Also rename the output.config file created through the svcutil to Web.config and add that to the root of the Web site. If you open up the output.config file, you will notice it contains a number of configuration entries for controlling the invocation behavior of the client. However it is also possible for you to manually generate the Web.config file using the below lines of code:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<client>
<endpoint
address="http://localhost/MyProjects/15Seconds/WCFBasics/MathService.svc"
binding="wsHttpBinding" contract="IMathService">
</endpoint>
</client>
</system.serviceModel>
</configuration>

Note the use of <system.ServiceModel> as the root of the service client configuration section. Inside that you have an element named <client> that specifies that you are dealing with client configuration sections here. Underneath the <client> element, you have an element named <endpoint> that captures the address, binding, and the contract information about the service.

At this point, you are ready to consume the service by writing code against the proxy. To this end, add a new ASP.NET Web page named MathServiceClient.aspx and modify its code to look as follows:


<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
MathServiceClient obj = new MathServiceClient();
int result = obj.Add(1, 5);
lblResult.Text = "Add (1, 5) =" + result.ToString();
result = obj.Subtract(5,1);
lblResult.Text += "Subtract(5,1)=" + result.ToString();
result = obj.Divide(10,2);
lblResult.Text += "Divide(10,2)=" + result.ToString();
obj.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Invoking the Math Service</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="Server"
Font-Bold="true" Height="128px" Width="405px" />
</div>
</form>
</body>
</html>

The above implementation is very simple. It invokes the various methods of the MathService using the proxy class (generated through the svcutil utility) from the previous section and displays the results of the invocation through a label control. If you navigate to the page using the browser, you should see an output that is somewhat similar to the following:

So far, you have seen a simple WCF service that only dealt with simple types. With that foundation, let us switch gears and discuss a service implementation that works with complex types. As mentioned before, you need to create data contracts to be able to work with complex types, which is the topic of focus in the next section.

Understanding Data Contracts

Data contracts can be explicit or implicit. Simple types such as int, string have an implicit data contract. For example, you can use an int or a string in a service operation without having to define a data contract. If you are passing more than simple types to or from a service operation, you must define an explicit service contract by using the [DataContract] and [DataMember] attributes. A data contract can be defined as follows:

  • It describes the external format of data passed to and from service operations
  • It defines the structure and types of data exchanged in service messages
  • It maps a CLR type to an XML Schema
  • It defines how data types are serialized and deserialized. Through serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Through deserialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
  • It is a versioning system that allows you to manage changes to structured data

You define a data contract by decorating a class, structure, or enumeration with the [DataContract] attribute. You identify members of a data contract by placing [DataMember] attributes on the fields or properties of the class. The following code defines a data contract for a Category class.

using System;
using System.ServiceModel;
using System.Runtime.Serialization;</code>

[DataContract]
public class Category
{
[DataMember]
public int CategoryID;

[DataMember]
public string Name;

[DataMember]
public Guid Rowguid;

[DataMember]
public DateTime ModifiedDate;
}

In a data contract, you must explicitly identify each member of the contract by using a [DataMember] attribute. This requirement ensures that developers make a conscious choice to expose data externally. The DataMember attribute is the sole determinant of whether data is shared externally; access modifiers such as public and private do not play a role.

Implementing the CategoryService that uses the DataContract

Once a data contract is defined, you can use the defined type in service operations. The following code shows the CategoryService that exposes a method named GetCategory that returns the details of the Category object based on the supplied category id.

First, add a new service named CategoryService.svc to the WCFBasics project, which will create a CategoryService.svc with the following lines of code.


<%@ ServiceHost Language="C#" Debug="true" Service="CategoryService" CodeBehind="~/App_Code/CategoryService.cs" %>

Modify the code-behind for the CategoryService to look as follows:


using System;
using System.ServiceModel;
using System.Data;
using System.Data.SqlClient;</code>

[ServiceContract()]
public interface ICategoryService
{
[OperationContract]
Category GetCategory(int categoryID);
}

public class CategoryService : ICategoryService
{
public Category GetCategory(int categoryID)
{
string connectionString = System.Web.Configuration.
WebConfigurationManager.ConnectionStrings["AdventureWorks"].
ConnectionString;
Category categoryObj = new Category();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "Select ProductCategoryID, Name, rowguid, " +
" ModifiedDate from Production.ProductCategory " +
" Where ProductCategoryID = " + categoryID.ToString();
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader reader =
command.ExecuteReader(CommandBehavior.CloseConnection);
while(reader.Read())
{
categoryObj.CategoryID = (int)reader["ProductCategoryID"];
categoryObj.Name = (string)reader["Name"];
categoryObj.Rowguid = (System.Guid)reader["rowguid"];
categoryObj.ModifiedDate = (DateTime)reader["ModifiedDate"];
}
}
return categoryObj;
}
}

Let us examine the GetCategory() method implementation. You start by retrieving the connection string from the Web.config file that is defined as follows:


<connectionStrings>
<add name="AdventureWorks" connectionString="server=localhost;Initial
Catalog=AdventureWorks;uid=sa;pwd=thiru;"
providerName="System.Data.SqlClient" />
</connectionStrings>

After that, you open the connection to the database through a SqlConnection object, create a SqlCommand object with the appropriate sql to be executed. Then you execute the query against the database using the SqlCommand.ExecuteReader() method.


 SqlDataReader reader =
command.ExecuteReader(CommandBehavior.CloseConnection);

Now you loop through the resultant SqlDataReader object and set the Category object with the values retrieved from the SqlDataReader object.


while(reader.Read())
{
categoryObj.CategoryID = (int)reader["ProductCategoryID"];
categoryObj.Name = (string)reader["Name"];
categoryObj.Rowguid = (System.Guid)reader["rowguid"];
categoryObj.ModifiedDate = (DateTime)reader["ModifiedDate"];
}
}

Finally you return the Category object back to the client. Now that you have created the service, you can now consume the service from a client application.

Creating the Client Application for the CategoryService

Prior to creating an ASP.NET page that consumes the service, you need to first create the proxy file using the same steps outlined in the previous example. Once you have the CategoryService proxy class created, add that to the App_Code folder of the WCFBasicsServiceClient project. After that, add a new ASP.NET page named CategoryServiceClient and add the following lines of code to it.


 <%@ Page Language="C#" %>
<script runat="server"></code>

protected void btnGetCategory_Click(object sender, EventArgs e)
{
CategoryServiceClient obj = new CategoryServiceClient();
int categoryID = Convert.ToInt32(txtCategoryID.Text);
Category cate = obj.GetCategory(categoryID);
lblResult.Text = "Category ID=" + cate.CategoryID.ToString() + "<br>";
lblResult.Text += "Name=" + cate.Name + "<br>";
lblResult.Text += "RowGuid=" + cate.Rowguid + "<br>";
lblResult.Text += "Modified Date=" + cate.ModifiedDate;
obj.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Invoking the Category Service</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblCategoryID" runat="server" Height="20px"
Text="Category ID:" Width="135px"></asp:Label>
<asp:TextBox ID="txtCategoryID" runat="server"></asp:TextBox>
<asp:Button ID="btnGetCategory" runat="server"
OnClick="btnGetCategory_Click" Text="Get Category"/>
<br/><br/>
</div>
<asp:Label ID="lblResult" Font-Bold="true" runat="Server" Height="128px"
Width="405px"/>
</form>
</body>
</html>

Before navigating to the page, add the following section to the Web.config file directly under the <system.serviceModel>/<client> section.


<endpoint address="http://localhost/MyProjects/15Seconds/WCFBasics/CategoryService.svc" binding="wsHttpBinding" contract="ICategoryService">
</endpoint>

Open up the browser and navigate to the CategoryServiceClient.aspx page. In the Category ID textbox, enter a valid category id and hit the “Get Category” button and you should see an output like the below one.

Conclusion

In this article, you have understood the WCF’s architecture and its key design characteristics. You have also seen the different types of contracts including ServiceContract, OperationContract, DataContract, and MessageContract. In addition, you have also seen the end points and the different components that make up an end point such as address, binding, and contract. After a thorough discussion of these concepts, you have also seen examples demonstrating the steps involved in creating and consuming a WCF service. Specifically,

  • How to create a simple WCF service and host it in IIS
  • How to create client proxy and configuration files using the Service Model Metadata Utility (svcutil.exe)
  • How to consume the WCF service from a client application
  • How to leverage the DataContract to return complex types from a WCF service

For more Details :

http://www.15seconds.com/Issue/061130.htm

http://aspalliance.com/1103_introduction_to_windows_communication_foundation

http://msdn.microsoft.com/en-us/library/ms731082.aspx

Posted in ASP.NET | Tagged: , , , , , , | 2 Comments »

List of Table Constraints in sql server

Posted by Viral Sarvaiya on November 18, 2009


There five type of constrains provided by SQL Server. We shall see each one of then in details

1. Primary Key Constraint

A primary key is used to uniquely identify each row in a table. A primary key can consist of one or more then more columns in table. When they consist of more then one column they are called composite key. It ensures unique non-null key.


CREATE TABLE Student(
StudID INT PRIMARY KEY,
StudName VARCHAR(50)
)

or


ALTER TABLE Department ADD PRIMARY KEY (DeptID)

2. Foreign Key Constraint

A foreign key is a field in a table that matches the primary key column of another table. It establishes and enforces a link between data of two tables. It ensures that value points to a valid key.


CREATE TABLE Subjects(
SubID INT,
SubName VARCHAR(50),
StudID INT FOREIGN KEY REFERENCES Student(StudID),
TotalMarks INT,
)

or


ALTER TABLE Subjects(ADD CONSTRAINT StudID_Fk
FOREIGN KEY (StudID) REFERENCES Student(StudID)

3. Unique Constraint

It is similar to Primary Key Constraint. It ensures that every value in the column is unique value. It allows one NULL value.


CREATE TABLE Employee(
EmpID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DeptID INT,
Salary INT,
ProjectCode INT UNIQUE,
)

or


ALTER TABLE Employee ADD UNIQUE (ProjectCode)

4. Nullability Constraint

It defines whether a column will accept a NULL value or not. It is referred to as nullability of the column.


CREATE TABLE Employee(
EmpID INT,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50),
DeptID INT,
Salary INT,
ProjectCode INT
)

or


ALTER TABLE Employee ALTER COLUMN FirstName VARCHAR(50) NOT NULL;

5. Check Constraint

Check Constraint is extensively used to enforcement of general data-validation rules or simple business rules. Such as correct date range, max value that can be inserted into a table, etc.


CREATE TABLE Employee(
EmpID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DeptID INT,
Salary INT,
ProjectCode INT,
CONSTRAINT chk_sal CHECK (Salary BETWEEN 1000 AND 25000)
)

or


ALTER TABLE Employee
ADD CONSTRAINT chk_salary CHECK (Salary BETWEEN 1000 AND 25000)

Now in the below example show the all the Constraints in one table,


CREATE TABLE Employee(
EmpID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50),
DeptID INT FOREIGN KEY REFERENCES Department(DeptID),
Salary INT,
ProjectCode INT UNIQUE,
CONSTRAINT chk_salary CHECK (Salary BETWEEN 1000 AND 25000)
)

Posted in Sql Server | Tagged: , , , , , , , , , , , , , | 2 Comments »

List of HTTP headers

Posted by Viral Sarvaiya on November 13, 2009


HTTP Headers form the core of an HTTP request, and are very important in an HTTP response. They define various characteristics of the data that is requested or the data that has been provided. The headers are separated from the request or response body by a blank line. HTTP headers can be near-arbitrary strings, but only some are commonly understood.

Requests

Header Description Example
Accept Content-Types that are acceptable Accept: text/plain
Accept-Charset Character sets that are acceptable Accept-Charset: iso-8859-5
Accept-Encoding Acceptable encodings Accept-Encoding: compress, gzip
Accept-Language Acceptable languages for response Accept-Language: da
Accept-Ranges Allows the server to indicate its acceptance of range requests for a resource Accept-Ranges: bytes
Authorization Authentication credentials for HTTP authentication Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Cache-Control Used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain Cache-Control: no-cache
Connection What type of connection the user-agent would prefer Connection: close
Cookie an HTTP cookie previously sent by the server with Set-Cookie (below) Cookie: $Version=1; UserId=JohnDoe
Content-Length The length of the request body in octets (8-bit bytes) Content-Length: 348
Content-Type The mime type of the body of the request (used with POST and PUT requests) Content-Type: application/x-www-form-urlencoded
Date The date and time that the message was sent Date: Tue, 15 Nov 1994 08:12:31 GMT
Expect Indicates that particular server behaviors are required by the client Expect: 100-continue
From The email address of the user making the request From: user@email.com
Host The domain name of the server (for virtual hosting), mandatory since HTTP/1.1 Host: en.wikipedia.org
If-Match Only perform the action if the client supplied entity matches the same entity on the server. This is mainly for methods like PUT to only update a resource if it has not been modified since the user last updated it. If-Match: "737060cd8c284d8af7ad3082f209582d"
If-Modified-Since Allows a 304 Not Modified to be returned if content is unchanged If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
If-None-Match Allows a 304 Not Modified to be returned if content is unchanged, see HTTP ETag If-None-Match: "737060cd8c284d8af7ad3082f209582d"
If-Range If the entity is unchanged, send me the part(s) that I am missing; otherwise, send me the entire new entity If-Range: "737060cd8c284d8af7ad3082f209582d"
If-Unmodified-Since Only send the response if the entity has not been modified since a specific time. If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT
Max-Forwards Limit the number of times the message can be forwarded through proxies or gateways. Max-Forwards: 10
Pragma Implementation-specific headers that may have various effects anywhere along the request-response chain. Pragma: no-cache
Proxy-Authorization Authorization credentials for connecting to a proxy. Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Range Request only part of an entity. Range: bytes=500-999
Referer This is the address of the previous web page from which a link to the currently requested page was followed. Referer: http://en.wikipedia.org/wiki/Main_Page
TE The transfer encodings the user agent is willing to accept: the same values as for the response header Transfer-Encoding can be used, plus the “trailers” value (related to the “chunked” transfer method) to notify the server it accepts to receive additional headers (the trailers) after the last, zero-sized, chunk. TE: trailers, deflate;q=0.5
Upgrade Ask the server to upgrade to another protocol. Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
User-Agent The user agent string of the user agent User-Agent: Mozilla/5.0 (Linux; X11)
Via Informs the server of proxies through which the request was sent. Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warn A general warning about possible problems with the entity body. Warn: 199 Miscellaneous warning

Responses

Header Description Example
Accept-Ranges What partial content range types this server supports Accept-Ranges: bytes
Age The age the object has been in a proxy cache in seconds Age: 12
Allow Valid actions for a specified resource. To be used for a 405 Method not allowed Allow: GET, HEAD
Cache-Control Tells all caching mechanisms from server to client whether they may cache this object Cache-Control: no-cache
Content-Encoding The type of encoding used on the data Content-Encoding: gzip
Content-Language The language the content is in Content-Language: da
Content-Length The length of the response body in octets (8-bit bytes) Content-Length: 348
Content-Location An alternate location for the returned data Content-Location: /index.htm
Content-Disposition An opportunity to raise a “File Download” dialogue box for a known MIME type Content-Disposition: attachment; filename=fname.ext
Content-MD5 A Base64-encoded binary MD5 sum of the content of the response Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
Content-Range Where in a full body message this partial message belongs Content-Range: bytes 21010-47021/47022
Content-Type The mime type of this content Content-Type: text/html; charset=utf-8
Date The date and time that the message was sent Date: Tue, 15 Nov 1994 08:12:31 GMT
ETag An identifier for a specific version of a resource, often a Message Digest, see ETag ETag: "737060cd8c284d8af7ad3082f209582d"
Expires Gives the date/time after which the response is considered stale Expires: Thu, 01 Dec 1994 16:00:00 GMT
Last-Modified The last modified date for the requested object, in RFC 2822 format Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT
Location Used in redirection, or when a new resource has been created. Location: http://www.w3.org/pub/WWW/People.html
Pragma Implementation-specific headers that may have various effects anywhere along the request-response chain. Pragma: no-cache
Proxy-Authenticate Request authentication to access the proxy. Proxy-Authenticate: Basic
Refresh Used in redirection, or when a new resource has been created. This refresh redirects after 5 seconds.
(This is a proprietary/non-standard header extension introduced by Netscape and supported by most web browsers.)
Refresh: 5; url=http://www.w3.org/pub/WWW/People.html
Retry-After If an entity is temporarily unavailable, this instructs the client to try again after a specified period of time. Retry-After: 120
Server A name for the server Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
Set-Cookie an HTTP cookie Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
Trailer The Trailer general field value indicates that the given set of header fields is present in the trailer of a message encoded with chunked transfer-coding. Trailer: Max-Forwards
Transfer-Encoding The form of encoding used to safely transfer the entity to the user. Currently defined methods are: chunked, compress, deflate, gzip, identity. Transfer-Encoding: chunked
Vary Tells downstream proxies how to match future request headers to decide whether the cached response can be used rather than requesting a fresh one from the origin server. Vary: *
Via Informs the client of proxies through which the response was sent. Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning A general warning about possible problems with the entity body. Warning: 199 Miscellaneous warning
WWW-Authenticate Indicates the authentication scheme that should be used to access the requested entity. WWW-Authenticate: Basic

Posted in ASP.NET, General | Tagged: , , , | 3 Comments »

Get HTML code from URL in ASP.NET

Posted by Viral Sarvaiya on November 10, 2009


To get HTML of web page you need only few lines of code.

To start, place two TextBox controls named txtURL and txtPageHTML, and one button control on web form, like in image bellow:

Web form for getting page HTML at design time

Now, on button’s click event function, place this code:

[ C# ]


// We  need these namespaces
using  System;
using  System.Text;
using  System.Net;

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

protected void  btnGetHTML_Click(object sender, EventArgs e)
{
// We'll use WebClient class for reading HTML of web  page
WebClient MyWebClient = new WebClient();

// Read web page HTML to byte array
Byte[] PageHTMLBytes;
if (txtURL.Text !=  "")
{
PageHTMLBytes =  MyWebClient.DownloadData(txtURL.Text);

// Convert result from byte array to string
// and display it in TextBox txtPageHTML
UTF8Encoding oUTF8 =  new UTF8Encoding();
txtPageHTML.Text  = oUTF8.GetString(PageHTMLBytes);
}
}
}

[ VB.NET ]

‘ We need these namespaces


Imports  System
Imports  System.Text
Imports  System.Net

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnGetHTML_Click(ByVal  sender As Object, ByVal e As  System.EventArgs) Handles btnGetHTML.Click
' We'll use WebClient class for reading HTML of web  page
Dim MyWebClient As  WebClient = New WebClient()

' Read web page HTML to byte array
Dim PageHTMLBytes() As Byte
If txtURL.Text <> "" Then
PageHTMLBytes =  MyWebClient.DownloadData(txtURL.Text)

' Convert result from byte array to string
' and display it in TextBox txtPageHTML
Dim oUTF8 As  UTF8Encoding = New UTF8Encoding()
txtPageHTML.Text =  oUTF8.GetString(PageHTMLBytes)
End If
End Sub

End Class

Now you can start sample project, type some valid URL in first TextBox control and click to “btnGetHTML” button. Code listed above will return HTML code of requested URL and display it in second text box, like in image bellow:

HTML code is read and shown in text box

As you see, loading of HTML code of web page is relatively easy. Analyzing of this data is much harder and depends of page structure.

Reference :

http://www.beansoftware.com/ASP.NET-FAQ/Read-Page-HTML.aspx

Posted in ASP.NET | Tagged: , , , , | 5 Comments »

Get Primary key – Foreign key relations table in sql server

Posted by Viral Sarvaiya on November 4, 2009



select * from information_schema.constraint_column_usage

This Query give the list of the table with the constraint


select * from information_schema.referential_constraints

this query give the list of the table with the foreign key constraint

now the query to find the relations


select

tblAll.table_name as Primary_TableName,

tblAll.column_name as Primary_TableColumn,

tblFK.table_name as ForeignKey_TableName,

tblFK.column_name as ForeignKey_ColumnName

from information_schema.constraint_column_usage tblAll

inner join information_schema.referential_constraints tblAllFK on

tblAllFK.unique_constraint_name = tblAll.constraint_name

inner join information_schema.constraint_column_usage tblFK on

tblAllFK.constraint_name=tblFK.constraint_name

Enjoy coding…..

Posted in Sql Server | Tagged: , , , | Leave a Comment »

ASP.NET Gridview: Merging Cells

Posted by Viral Sarvaiya on November 4, 2009


I had been searching for a way to merge cells in a GridView


If e.Row.DataItemIndex % 2 = 0 Then
e.Row.Cells(0).RowSpan = 2
e.Row.Cells(1).RowSpan = 2
End If
'Remove the extra cells created due to row span for odd rows
If e.Row.DataItemIndex % 2 = 1 Then
e.Row.Cells.RemoveAt(0)
e.Row.Cells.RemoveAt(0)
e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
End If

This code should take every cell in the first and second columns and merge it with the cell directly below it. Of course, this should be used very carefully, because you could screw up your data if each record does not have another corresponding record with it. However, this shouldn’t be too difficult to program around by adding data checks.

Posted in ASP.NET | Tagged: , , | Leave a Comment »

Retrieving Data as XML from SQL Server

Posted by Viral Sarvaiya on November 2, 2009


All the hype that once surrounded XML is finally starting to die down, and developers are really beginning to harness the power and flexibility of the language. XML is a data descriptive language that uses a set of user-defined tags to describe data in a hierarchically-structured format.

The release of Microsoft SQL Server 2000 a couple of months ago saw Microsoft jump on the XML band-wagon too – they’ve included a number of different ways to manipulate data as well-formed XML. Firstly, there’s the SQL XML support. Microsoft’s implementation of SQL XML provides a simple configuration tools that allows developers to gain remote access to databases using URL based queries over HTTP. For example, we can setup an SQL XML virtual directory on our Web server named “myVirtual”. Then, assuming we have the appropriate security permissions, we can use any browser to query our database using a simple URL based query (such as: http://www.myserver.com/myVirtual?SQL=select+*+from+products+for+xml+auto). This then returns our results as an XML based recordset.

Notice the “for xml auto” part of our query above? This determines the way in which SQL Server 2000 shapes our data. There are three shaping methods:

1. “for xml auto”: Returns XML elements that are nested, based on which tables are listed in the “from” part of the query, and which fields are listed in the “select” part.

2. “for xml raw”: Returns XML elements with the “row” prefix (ex: “<row tProduct …>”). Each column in a table is represented as an attribute and null column values aren’t included.

3. “for xml explicit”: Explicit mode is the most complex shaping method used in SQL Server 2000. It allows users to query a data source in such a way that the names and values of the returned XML are specified before the query batch is executed.

for read detials click here

other links…

http://msdn.microsoft.com/en-us/library/ms345137%28SQL.90%29.aspx

http://www.15seconds.com/issue/001102.htm

Posted in ASP.NET, Sql Server | Tagged: , , , | 1 Comment »

 
%d bloggers like this: