Dotnet notes : LINQ

July 7, 2015

Use of dynamic in c#

LINQ : FirstOrDefault

Enabling remote debugger in visual studio

June 25, 2015

In production environment
Suppose you dont have visual studio editor. You have only dlls in the production machine. There is no code.
Along with framework you will get Remote Debugger tools in visual studio menu.

In the production machine start remote debugger tool. You will get machineno along with port no.

In development machine attach to process – Qualifier -> Enter the machineno along with portno.
in Attach to : select Automatic :Managed(v4.5,v4.0)code.
And attach to the w3p process
remote debugger

Please select a row error popup message

April 2, 2015

In IE 11
if you are getting “Please select a row” error pop up message then u have to check the below solution.

Tools – >Compatibility View settings ->

uncheck the checkbox of “Display intranet sites in Compatibility View”

Please select a row

Convert File to Byte Array and Byte Array to Files

November 14, 2014

In this article, let us see how to convert a file content to a byte array and restore the original content from the byte array and display it in its original file format such as pdf, doc, xls, rtf, jpeg, png etc.

Step 1: Create an ASP.Net application and add a class Document.

public class Document {
public int DocId { get; set; }
public string DocName { get; set; }
public byte[] DocContent { get; set; }

Step 2: Create a file of format doc/pdf/rtf etc. and convert the file content to a ByteArray using the following method. Then create an object of type Document and assign the Docname and DocContent property values from filename and filecontent.

public Document FileToByteArray(string fileName) {
byte[] fileContent = null;

System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
long byteLength = new System.IO.FileInfo(fileName).Length;
fileContent = binaryReader.ReadBytes((Int32)byteLength);
Document = new Document();
Document.DocName = fileName;
Document.DocContent = fileContent;
return Document ;

Step 3: Now we need to convert the byte array back again to the file content and display it to the user with download options. For that I am using another method, ShowDocument, as below:

private void ShowDocument(string fileName, byte[] fileContent)
//Split the string by character . to get file extension type
string[] stringParts = fileName.Split(new char[] { ‘.’ });
string strType = stringParts[1];
Response.AddHeader(“content-disposition”, “attachment; filename=” + fileName);
//Set the content type as file extension type
Response.ContentType = strType;
//Write the file content

Step 4: In the page_load event, I have the following code, which will read the contents from the file and convert it to a ByteArray using the FileToByteArray method and convert it back to content using the ShowDocument method.

protected void Page_Load(object sender, EventArgs e)
FileToByteArray(“C:\\Users\\santhosh.kumar\\Documents\\Test for rht format.rtf”);
ShowDocument(Document.DocName, Document.DocContent);

jquery grid example in c#

June 19, 2013

C# 4.0-Dynamic Data Type, Difference between var and dynamic

November 21, 2011

C# 4.0-Dynamic Data Type, Difference between var and dynamic inShare0
C# 4.0 introduces a new keyword called ‘Dynamic‘. It can consume any object anything. Let’s see some examples for that.dynamic intExample = 1;
Response.Write(intExample);dynamic floatExample = 2.5;

dynamic stringExample = “DotNetJaps”;


It will print out put on web page as following.


Now, you will have question what’s new in that. It could be also done with var keyword . Yes, you can do same thing with var but dynamic keyword is slightly different then var keyword.

Diffrence between var and dynamic keyword:

var keyword will know the value assigned to it at compile time while dynamic keyword will resolve value assigned to it at run time. I know you guys don’t believe me without example. So let’s take example of string.

string s = “DotNetJaps-A Blog for,,VB.NET”;
var varstring=s;


Now try to compile above code it will not compile and it gives a error that ‘string’ does not contain a definition for ‘MethodDoesnotExist’ and no extension method ‘MethodDoesnotExist’ accepting a first argument of type ‘string’ could be found (are you missing a using directive or an assembly reference?’. So var keyword knows that what value or object or anything assigned to it. Now lets try to compile same code with dynamic example like following.string s = “DotNetJaps-A Blog for,,VB.NET”;
dynamic varstring=s;


This will compile. So it is the difference between dynamic and var keyword. With dynamic keyword anything assigned to it like property,objects operators anything that will be determined at compile time. It can be useful while we are doing programming with com,JavaScript which can have runtime properties.

C# Singleton Pattern

November 21, 2011

Singleton Pattern in C#

This piece of code will explain how to implement the Singleton pattern when we create a class.

    public class MySingleTon
        private static MySingleTon singleTonObject;
        // private constructor. This will avoid creating object using new keyword
        private MySingleTon()

        // public method which will be called
        public void GetName()
            Console.WriteLine("Brainstorming Guy");
        public static MySingleTon CreateInstance()
            // this object will be used with lock, so that it will be always one thread which will be executing the code
            object myObject = new object();
            // put a lock on myObject. We won't be able to use singleTonObject becuase it will be null. lock is to make the object thread safe.
            // lock can't be worked with null objects.
            lock (myObject)
                // check whether the instance was there. If it's not there, then create an instance.
                if (singleTonObject == null)
                    singleTonObject = new MySingleTon();
            return singleTonObject;

You can get an instance and you can make a call as given below.

            MySingleTon myObject = MySingleTon.CreateInstance();
            myObject = MySingleTon.CreateInstance();

When you execute the above piece of code, you can note that when the first time CreateInstance method is called, it will create an instance. But when you make a second call, it will check whether instance exists there and if it exists, it will return the same instance.
I placed comments in almost all the lines of the singleton class.