Archive for the ‘.NET’ Category


July 14, 2017


MEX and WSDL are two different schemes to tell potential clients about the structure of your service. So you can choose to either make your service contracts public as (MEX) or WSDL.

A WSDL is generally exposed through http or https get urls that you can’t really configure (say for security limitations or for backward compatibility). MEX endpoints expose metadata over configurable endpoints, and can use different types of transports, such as TCP or HTTP, and different types of security mechanisms.


Visual Studio remove references count (Codelens)

July 5, 2017

Remove/Set the codelens in VS editor

Tools -> Options -> Text Editor -> All Languages -> CodeLens -> check/uncheck

Restore NuGet Packages

September 28, 2016

If Solution -> Restore NuGet Packages doesn’t work
delete the packages folder and try again and then rebuild solution.
That should fix it.


How to use __doPostBack

February 5, 2015


<script type=“text/javascript”>
function SaveWithParameter(parameter){
   __doPostBack(‘btnSave’, parameter)


public void Page_Load(object sender, EventArgs e)
string sender = Request[“__EVENTTARGET”]; // btnSave
string eventarg = Request[“__EVENTARGUMENT”]; //parameter

Call ASP.NET codebehind from javascript (PageMethod)

November 17, 2014
The first thing to do is to add a ScriptManager to our page,
and tell it to accept page Methods.
<asp:ScriptManager ID="ScriptManager1" runat="server"

Next add the codebehind method to call, it must be static and have
the WebMethod attribute

public static void SetDimensions(int width, int height)
    //Method code

Finally call the method using PageMethods 

<script type="text/javascript">
    function setDimensions() {
        PageMethods.SetDimensions(width, height);

WCF service for AJAX

August 6, 2014

WCF service for client AJAX call returning JSON

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
// Your code comes here

public interface IService
[WebInvoke(Method = GET”,
ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);

[WebInvoke(Method = POST”,
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id);

<behavior name=”ServiceBehavior”>
<serviceMetadata httpGetEnabled=”true”/>
<serviceDebug includeExceptionDetailInFaults=”true”/>
<behavior name=”EndpBehavior”>
<service behaviorConfiguration=”ServiceBehavior” name=”Service”>
<endpoint address=” binding=”webHttpBinding”
contract=”IService” behaviorConfiguration=”EndpBehavior”/>

Client code with JQuery:

function CallService() {
type: POST”, //GET or POST or PUT or DELETE verb
url: Service.svc/GetUser”, // Location of the service
data: {“Id”: “‘ + userid + “}’, //Data sent to server
contentType: application/json; charset=utf-8″, // content type sent to server
dataType: json”, //Expected data format from server
processdata: ProcessData, //True or False
success: function(msg) {ServiceSucceeded(msg);},
error: ServiceFailed// When Service call fails

function ServiceSucceeded(result) {
if (DataType == json”) {
resultObject = result.GetUserResult;

for (i = 0; i < resultObject.length; i++) {


function ServiceFailed(result) {
alert(Service call failed: ‘ + result.status + + result.statusText);
Type = null;
varUrl = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;

ASP.NET AJAX MaskedEditExtender

September 25, 2013
  • MaskType – Type of validation to perform:
    None – No validation
    Number – Number validation
    Date – Date validation
    Time – Time validation
    DateTime – Date and time validation
  • Mask Characters and Delimiters
    9 – Only a numeric character
    L – Only a letter
    $ – Only a letter or a space
    C – Only a custom character (case sensitive)
    A – Only a letter or a custom character
    N – Only a numeric or custom character
    ? – Any character

Linq operators on null Linq collection

September 25, 2013

You cannot apply the Linq operators (First, Single, Any, …) when the query returns null.

We have to add an extension method:

public static IEnumerable<TSource> NullToEmpty<TSource>(
    this IEnumerable<TSource> source)
    if (source == null)
        return Enumerable.Empty<TSource>();

    return source;

And now you can write:

public void Should_handle_nulls()
    List<int> ints = null;


LINQ parameter == null with SQL CE

August 6, 2013

SQL CE cannot handle parameters so, the so standard construction

.Where(p => p.Category == category || category == null)

fails because LINQ needs to use a parameter to do that value comparison in-query.  But it is perfectly valid SQL to pass something like this:

.Where(p => p.Category == category || true)

because ‘true’ is always…well…true.  So if you parse your variable to a bool before the LINQ statement and then pass that variable to SQL CE, you’re in good shape:

bool bCategory = (string.IsNullOrEmpty(category)) ? true : false;
Products = repository.Products.Where(p => p.Category == category || bCategory);

This allows you do to all the filtering in the query before returning data, and you can use this trick on as many conditions as you want without it getting messy.

Server Tags

September 24, 2012

Server Tags:

<% %> An embedded code block is server code that executes during the page’s render phase. The code in the block can execute programming statements and call functions in the current page class.

<%= %> most useful for displaying single pieces of information.

<%# %> Data Binding Expression Syntax.

<%$ %> ASP.NET Expression.

<%@ %> Directive Syntax.

<%– –%> Server-Side Comments.

<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4).