Friday, August 8, 2014

Simple WCF Application Call From Client End

Today, i am going to create simple WCF Web Service and consume same web service from client side.
(A) Steps to create new WCF Service
(1) Right Click on your solution --> Add --> New Project --> Select ASP.NET Web Application --> Give appropriate name like "REST"
(2) Above step will create new Web Application, you can remove master page and other pages included from newly created project
(3) Right Click on newly created project --> Add --> New Item --> Select "WCF Service" --> Give appropriate name like "MyService"
(4) Above step will add an interface named "IMyService" already implemented and one service with extension "MyService.svc"
(5) Remove existing code implemented in interface as well as in service.
(6) Add following line of code in interface
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
string DoWorkWithString();
- This is simple method we have declare which will return string on execute.
- WebInvoke requires "System.ServiceModel.Web" name space to be added as reference in your project
(7) Implement above method with following code in MyService.cs
public string DoWorkWithString()
{
    return "This is one test";
}

(8) After completion of above code, create virtual directory named "REST" and bind it with above project.

(9) To run with existing code from client end, you need to change serviceModel tag with following line of code in your Web.Config file. To do that, replace following line of code of serviceModel with your Web.Config tag.



(B) Steps to call WCF Service from client end
Now, you are done with your simple service implementation, you can now call above service from client end using following line of code to your html and it will prompt "This is one test".
$.ajax({
    url: 'http://localhost/REST/MyService.svc/DoWorkWithString',
    dataType: 'json',
    cache: false,
    type: 'GET',
    contentType: 'application/json; charset=utf-8',
    data: {},
    error: function (XMLHttpRequest, textStatus, errorThrown) {
     alert('Error occurred during operation');
    },
    success: function (data1) {
        alert(data1.DoWorkWithStringResult);
    }
});

(C) You can also following navigate to following links to do same.


(1) An Introduction to WCF

(2) Four Steps to Create First WCF Service For Beginners

(3) Consuming WCF / ASMX / REST service using JQuery
       

Thursday, August 7, 2014

Monday, August 4, 2014

Error : Service data not get refresh in IE8 from Client Side

I face problem of Data not refresh from client side when we are working with IE. I found this problem while i am working with IE8 and above.

Problem is like, in my MVC Single Page Application, when we are fetching any data from client side for first time it will get data perfectly but subsequent call for same data or some back and forth to some other location give me previous unchanged data which are cached. But, after a bit of googling following line of code works for me and resolved my problem.



This line of code i put in "_layout.cshtml" header section and it works for me and then after it gives me refreshed data view.


The above line of code works fine for me and resolved my problem.

Friday, July 11, 2014

Error : The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

Here i found another error generated over working with importing excel file. So, my idea behind importing an excel file is simple shown below.

Code to import excel file is shown below.
excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties='Excel 12.0;HDR=YES';";
DataTable dt = new DataTable();
OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
string query = string.Format("Select * from [{0}]", excelSheets[0]);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
{
        dataAdapter.Fill(ds);
}

but, doing this stuff i found error "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine."


Googling around this error help me out by two cases. For the solution, i found following two links useful.
(1) 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

(2) 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine error


So, that's it for this error.

Tuesday, June 24, 2014

Error : 405 Method Not Allowed for PUT

Recently i came across with a problem where PUT is not working for me and throwing error of "405 Method Not Allowed for PUT".

Following line of code i found working for me.
- Open your web.config file
- Go To "system.webServer" Tag
- Add runAllManagedModulesForAllRequests="true" in modules clause
- Put remove name="WebDAVModule" between modules tag
- After adding this lines, your config will look like following..
This change works for me. Also, There are some useful links for this as well shown below.
(1) 405 method not allowed web api

(2) Web API on IIS 8.0 – 405 Method Not Allowed for PUT

(3) Web API Put Request generates an Http 405 Method Not Allowed error
                      

Thursday, March 13, 2014

TFS Options in VS2012

Here is some tips regarding known TFS options while you are working with VS2012.
(1) Go Online : for VS2010 if you are working offline you will find an icon of Go Online on Solution Explorer, but for VS2012 you will find this option under:
File --> Source Control --> Go Online

(2) Source Control Explorer: for Source Control Explorer in VS2010 you found this option under Team Explorer --> Source Control. For VS2012, this option found under Team Explorer --> Home --> Source Control Explorer

(3) Following menu items are independent items in VS2010 context menu
- Get Specific Version

- View Pending Changes
- Shelve Pending Changes
- Undo Pending Changes
- View History
- Annotate
But, in case of VS2012 you will find all this options under

Context Menu of Solution Explorer on Any Item --> Source Control

(4) Disconnect TFS

VS2010: In this case, you will find this option under Team Explorer to disconnect from TFS as well as from Top Menu --> Team --> Disconnect To Team Foundation Server

VS2012: To disconnect from TFS server you need to navigate to Top Menu --> Team --> Disconnect From Team Foundation Server and there is no such option available on Team Explorer to disconnect.

Download Microsoft Visual Studio Team Foundation Server 2012 Power Tools
   
        

Tuesday, March 11, 2014

Working with Conditional Comments

Here are some links about Conditional Statements.

Conditional comments are statements interpreted by Microsoft Internet Explorer in HTML source code. Conditional comments can be used to provide and hide code to and from Internet Explorer.

(1) About conditional comments

(2) Conditional comments

(3) Conditional comment

(4) Conditional comments of IE
     
      

Thursday, February 20, 2014

Error : CSS, Images not loading when Debub="false" in web.config for MVC Application

For MVC application, i came across problem while i set Debut="false" in web.config file and ultimately css and images not loading from BundleConfig file but when i set it to "true" will load all files.

While googling i got some Web.Config settings shown below, modify your web.config in the system.webServer section to run these requests through the BundleModule


But, this line of code for Web.Config not solved my problem. Finally, i got to know about bundle name in BundleConfig file, i chnaged it shown below.

- Before Change Path :
"~/Content/css" : My all CSS were reside under "Content/css"
- After Change:
"~/Content/css/css"

So, to get rid of the problem you need to give path exactly as per project and put extra "css" after it will resolve your problem for CSS not loading.

Friday, January 31, 2014

Error - A potentially dangerous Request.Path value was detected from the client

During calling REST service from client end i came across the below error..

"A potentially dangerous Request.Path value was detected from the client"

For this, i found following solution in web.config file of REST end and it works for me



- For httpruntime put requestpathinvalidcharacters="" & requestvalidationmode="2.0"
- For pages put validaterequest="false" 
- Both of these tags are located under configuration --> system.web

Also, there are some useful links too for this error
(1) Getting “A potentially dangerous Request.Path value was detected from the client (&)”

(2) A potentially dangerous Request.Path value was detected from the client (*)

Tuesday, January 28, 2014

Working with filtering JSON objects

Following are some way by which we can filter out objects in JSON format using jQuery.

(1) filter : using this method we can filter out JSON data by following way..
var filterData = (JSONDATA.filter(function (item) {
            return item["ColumnName"] != "Your Data To Compare";
        }));
- In above situation, filter will loop through each data and finds the specific data by comparing it.
- filter is only available in new version of jQuery. Also, it is not working in IE-8 and not compatible with IE-8 or lower.

(2) grep : using this method we can filter out JSON data by following way..
var filterData = ($.grep(JSONDATA,function (item,l) {
            return item["ColumnName"] != "Your Data To Compare";
        }));
In above situation, grep will loop through each data and finds the specific data by comparing it.


(3) slice : using this method we can removes and returns a given number of elements starting from a given index from JSON object by following way..
var filterData = $.each(JSONDATA.slice(1,3), function(item) {
                    return item;

                }),
- In above example, removes three elements starting from index position 1 (i.e., the 2nd, 3rd, and 4th elements) and returns them as an array.

- Find following link for the Grep vs Filter in jQuery.