Thursday, July 18, 2013


Difference between asynchronous (AJAX) and synchronous request?

Now-a-days, this is a very common question that been asked in all most every basic interview – What is the main strength of AJAX? Or why and when should we use AJAX in web application?
And the most common answer is – “AJAX does not refresh or reload the whole page”.
But, the more perfect answer is – “AJAX is an asynchronous technology where others request are synchronous.”
So, what is the difference?
In a word, a program does not wait for response after requesting an asynchronous call whereas synchronous does so.
Here is a simple example –
function check() {
var a=0;
a = getStatus(“getstatus.php?id=5”);
if(a==1) {
alert(“active”);
} else {
alert(“not active”);
}
}
Here getStatus() function sends a AJAX request to the server with “getstatus.php?id=5” url and the php file decides (from database may be) the status and output/response as 1 or 0.
But, this function will not work properly. It will alert “not active” instead of “active”. And yes, that is for the asynchronous request.
The reason is – when a = getStatus(“getstatus.php?id=5”); line is being executed program does not wait for the response of setStatus() function. So, value of keep unchanged or set to null.
So, how should we work with asynchronous request?
Of course, using callback function. Callback function is that function which is triggered when the request completes to get the response (or as defined).

Refer link:- http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/ 

Monday, July 15, 2013

Read /Write in XML column in sqlserver

This is simple example to read and write in xml column of a table in sqlserver . You can directly execute in sqlserver too.


----------------- To insert/update in xml column
declare @t table (id int, xmlCol xml)

insert into @t values(1,'14747')

select * from @t;

update @t set xmlcol = '1474714749' where id =1

select * from @t

--even you cast as
select cast ( xmlcol as varchar(max)) , * from @t

----------------- Select example-----1st example --
Declare @responce xml; DECLARE @douvalue varchar(100);
SELECT @responce = ' 60.4655'
select @douvalue = @responce.value('(/double)[1]', 'varchar(100)');
select @responce, @douvalue AS DownloadEnvironment

--Declare @responce xml;
SELECT @responce = '
60.4655'

;WITH XMLNAMESPACES(DEFAULT 'http://www.webserviceX.NET/')
select @douvalue = @responce.value('/double[1]', 'varchar(100)');

select @douvalue

------2nd example - To retrieve value ---
declare @xml xml 
set @xml = '14747  14749  14750  14752'
    SELECT
        xmlnodes.idnode.value('.', 'int')
    from   
        @xml.nodes('/root/id') as xmlnodes(idnode) 

--3rd example    -- even you compare with other table too -----
   SELECT
        a.activityID, a.columnName,
        convert(varchar(12), a.createdDate,13) as createdDate ,
        CONVERT(varchar(15),CAST(a.createdDate AS TIME), 100) as activityTime     
   from   
        @xml.nodes('/root/id') as xmlnodes(idnode) 
        JOIN Activity a 
            ON   xmlnodes.idnode.value('.', 'int') = a.activityID 
   WHERE
        a.active = '1' AND
        (a.activityType = 'Viewed' OR a.activityType = 'Edited')  
   ORDER BY
        a.createdDate desc,
        a.activityTime desc

Friday, July 12, 2013

Read And write xml data in xml column in sqlserver

Suppose I have an entry


assetsIDs_xml  '119'

update Designs set assetsIDs_xml = ''
where ( assetsIDs_xml.exist('/root/id[text()=119]') =1 ) 



SELECT
           Columnlist
      FROM
            tableName c
      WHERE
            AND ((c.userIDs_xml.exist('/root/id[text()=sql:variable("@userID")]') = 1)  

Monday, July 1, 2013

IIS not work, Give error "HRESULT...." or When IIS start gives error "Already running a process on this port"

Hello All,


In my pc, IIS was working fine as I know. Today when I want to run my site from IIS. It gives me above error . I am searching lot for my this issue. Even I was did stop my firewall , still getting the same.

So I googling and get solution that skype is also share the same port of IIS : 80. So follow below to solve this issue.

The cmd NETSTAT -ano showed Skype (via the PID) was using port 80.
This was preventing IIS starting a local website with the message:
The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)
This was resolved in Skype by unchecking Tools > Options... > Advanced > Connection > "Use port 80 and 443 as alternatives for incoming connections"

Refer link :Prevent Skype using port 80 and causing IIS to throw a HRESULT: 0x80070020 Exception

Monday, January 16, 2012

Introduction to WCF - WCF tutorial | WCF Tutorial - Windows Communication Foundation | WCF Example | WCF Sample code in asp.net 3.5 | Basic WCF Tutori

Introduction:

Here explain what WCF (windows communication foundation) is, uses of windows communication foundation and how to create and use windows communication foundation in c#.

Description:
In previous articles explained clearly what webservice is and how to create and consume webservice using asp.net . In another post I explained clearly what windows service is and how to create windows service and sample of windows service using c#. Now in this article I will explain about windows communication foundation. First we will see what a WCF (window communication foundation) is and uses of WCF (windows communication foundation) after that we will see how to create and use WCF in c#.net.

What is WCF (windows communication foundation) Service?

Windows Communication Foundation (Code named Indigo) is a programming platform and runtime system for building, configuring and deploying network-distributed services. It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF. It is unified programming model provided in .Net Framework 3.0. WCF is a combined feature of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.
Advantages of WCF
1) WCF is interoperable with other services when compared to .Net Remoting where the client and service have to be .Net.

2) WCF services provide better reliability and security in compared to ASMX web services.

3) In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements.

4) WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology developer has to write the code.

Difference between WCF and Web service

Web service is a part of WCF. WCF offers much more flexibility and portability to develop a service when comparing to web service. Still we are having more advantages over Web service; following table provides detailed difference between them.

Features Web Service WCF
Hosting It can be hosted in IIS It can be hosted in IIS, windows activation service, Self-hosting, Windows service
Programming

[WebService] attribute has to be added to the class

[ServiceContract] attribute has to be added to the class
Model

[WebMethod] attribute represents the method exposed to client

[OperationContract] attribute represents the method exposed to client
Operation

One-way, Request- Response are the different operations supported in web service

One-Way, Request-Response, Duplex are different type of operations supported in WCF
XML

System.Xml.serialization name space is used for serialization

System.Runtime.Serialization namespace is used for serialization
Encoding

XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom

XML 1.0, MTOM, Binary, Custom
Transports

Can be accessed through HTTP, TCP, Custom

Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Protocols

Security

Security, Reliable messaging, Transactions

A WCF Service is composed of three components parts viz,

1) Service Class - A WCF service class implements some service as a set of methods.

2) Host Environment - A Host environment can be a Console application or a Windows Service or a Windows Forms application or IIS as in case of the normal asmx web service in .NET.

3) Endpoints - All communications with the WCF service will happen via the endpoints. The endpoint is composed of 3 parts (collectively called as ABC's of endpoint) as defines below:

Address: The endpoints specify an Address that defines where the endpoint is hosted. It’s basically url.

Ex:http://localhost/WCFServiceSample/Service.svc

Binding: The endpoints also define a binding that specifies how a client will communicate with the service and the address where the endpoint is hosted. Various components of the WCF are depicted in the figure below.

* "A" stands for Address: Where is the service?
* "B" stands for Binding: How can we talk to the service?
* "C" stands for Contract: What can the service do for us?

Different bindings supported by WCF

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
BasicHttpBinding

Basic Web service communication. No security by default
WSHttpBinding

Web services with WS-* support. Supports transactions

Contract: The endpoints specify a Contract that defines which methods of the Service class will be accessible via the endpoint; each endpoint may expose a different set of methods.

Different contracts in WCF

Service Contract

Service contracts describe the operation that service can provide. For Eg, a Service provide to know the temperature of the city based on the zip code, this service is called as Service contract. It will be created using Service and Operational Contract attribute.

Data Contract

Data contract describes the custom data type which is exposed to the client. This defines the data types, which are passed to and from service. Data types like int, string are identified by the client because it is already mention in XML schema definition language document, but custom created class or data types cannot be identified by the client e.g. Employee data type. By using DataContract we can make client to be aware of Employee data type that are returning or passing parameter to the method.

Message Contract

Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute.

Fault Contract

Suppose the service I consumed is not working in the client application. I want to know the real cause of the problem. How I can know the error? For this we are having Fault Contract. Fault Contract provides documented view for error occurred in the service to client. This helps us to easy identity, what error has occurred.

Overall Endpoints will be mentioned in the web.config file for WCF service like this
























Creating simple application using WCF

First open Visual Studio and click file --> Select New --> Website Under that select WCF Service and give name for WCF Service and click OK



Once you created application you will get default class files including Service.cs and IService.cs




Here IService.cs is an interface it does contain Service contracts and Data Contracts and Service.cs is a normal class inherited by IService where you can all the methods and other stuff.

Now open IService.cs write the following code

[ServiceContract]
public interface IService
{
[OperationContract]
string Welcome(string Name);
}

After that open Service.cs class file and write the following code

public class Service : IService
{
public string SampleMethod(string Name)
{
return "First WCF Sample Program " + Name;
}
}


Here we are using basicHttpBinding for that our web.config file system.serviceModel code should be like this and I hope no need to write any code because this code already exists in your web.config file in system.serviceModel























Our WCF service ready to use with basicHttpBinding. Now we can call this WCF Service method console applications

After completion of WCF service creation publish or deploy your WCF Service in your system. If you don’t’ have idea on deploy check this post publish or deploy website

After completion of deploy webservice now we can see how to use WCF Service in our console application

Calling WCF Service using Console Application

To call WCF service we have many ways like using console app, windows app and web app but here I am going for console application.

Create new console app from visual studio select project type as console application gives some name as you like.




After Creation Console application now we need to add WCF reference to our console application for that right click on your windows application and select Add Service Reference


Now one wizard will open in that give your WCF service link and click Go after add your service click OK button.


After completion of adding WCF Service write the following code in Program.cs class file Main method

static void Main(string[] args)
{
ServiceReference1.ServiceClient objService = new ServiceClient();
Console.WriteLine("Please Enter your Name");
string Message = objService.SampleMethod(Console.ReadLine());
Console.WriteLine(Message);
Console.ReadLine();
}
After that open your app.config file and check your endpoint connection for WCF Service reference that should be like this






Now everything is ready run your application that output should be like this


Monday, June 6, 2011

datetime bind in grid in aspx

adfoooc
<%# Datetime.Now.ToShortDateString() %>
asdfasdf