Simple example of configuring Enterprise library Caching Block.

Download the Enterprise Library 5 from here

Create a new windows application.

We need the configuration information to setup the caching.

Open the EntLib Configuration console.

Open the app.config file of the windows application created.

Select Blocks à Add Caching Settings

It will add the required configuration information to the config file of the windows application.

Sample code implementing Caching block to save dictionary object in Cache.

public static class Configuration
public static Dictionary<string, string> GetConfigSettings()
// get the cache manager
ICacheManager cacheManager = CacheFactory.GetCacheManager();
var configSettings = (Dictionary<string, string>)cacheManager["configSettings"];

if (configSettings == null)
configSettings = new Dictionary<string, string>();
configSettings.Add("key1", "value1");
configSettings.Add("key2", "value2");

// add the dictionary object
cacheManager.Add("configSettings", configSettings);
// get from the configsettings object from the cache
configSettings = (Dictionary<string, string>) cacheManager["configSettings"];

return configSettings;




The type initializer for ‘System.Data.SqlClient.SqlConnection’ threw an exception

I was getting the above error in one of my windows application. Was because of some section getting repeated in the app.config file.

Removing the duplicate section information from the app.config resolved the issue.

Sample code for Inserting a lead record from a .NET application : Oracle CRM On Demand

First download the wsdl file for the lead record from Oracle CRM On Demand web application.

Add web references to it within a .NET windows application.

Use the following code for inserting the lead record.

We need to specify value for all the required field for the record to get inserted.

        String loginUrlString = ";;
        String logoffUrlString =";;
        String siebelUserName = "username/password";
        String siebelPassword ="password";
        String opportunityUrl = ";jsessionid=&quot;;
        String sessionID = "";
        Private void Form1_Load(Object sender, EventArgs e)
            // Get the session from the helper Class ManageSession
             sessionID = ManageSession.Login(loginUrlString, siebelUserName, siebelPassword);

            //Create the New lead record instandce And Set its url With proper session id
             Lead myLead = New Lead();
             myLead.Url = ";jsessionid=&quot; + sessionID;

            // create arrary Of lead record To be inserted
             LeadData[] myLeadData = New LeadData[1];
             myLeadData[0] = New LeadData();
             myLeadData[0].LeadFirstName = "Nishant";
             myLeadData[0].LeadLastName = "Rana";
             myLeadData[0].Source = "Email";
             myLeadData[0].dLead_Generation_Date = Convert.ToDateTime("7/30/2010");
             myLeadData[0].LeadOwner = "Nishant Rana";
             myLeadData[0].dLead_Generation_dateSpecified = True;             

            // add lead data array To listofLeadData Class
             ListOfLeadData myLstLeadData = New ListOfLeadData();
             myLstLeadData.Lead = myLeadData;

            // create an instance Of leadinsert_input Class
            LeadInsert_Input myLeadRecordInput = New LeadInsert_Input();
            myLeadRecordInput.ListOfLead = myLstLeadData;
            LeadInsert_Output myLeadRecordOutput = myLead.LeadInsert(myLeadRecordInput);    



The sample code for the ManageSession.cs class

 class ManageSession
        public static string SessionID = "";
        public static String Login(String loginUrlString, String userName, String password)
                // create a http request and set the headers for authentication
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrlString);
                HttpWebResponse myResponse;
                myRequest.Method = "POST";
                // passing username and password in the http header 
                // username format if it includes slash should be the forward slash /
                myRequest.Headers["UserName"] = userName;
                myRequest.Headers["Password"] = password;
                myResponse = (HttpWebResponse)myRequest.GetResponse();
                Stream sr = myResponse.GetResponseStream();
                // retrieve session id
                char[] sep = { ‘;’ };
                String[] headers = myResponse.Headers["Set-Cookie"].Split(sep);
                for (int i = 0; i <= headers.Length – 1; i++)
                    if (headers[i].StartsWith("JSESSIONID"))
                        sep[0] = ‘=’;
                        SessionID = headers[i].Split(sep)[1];
            catch (WebException webException)
                return webException.Message;
            catch (Exception ex)
                return ex.Message;
            // send back the session id that should be passed to subsequent calls 
            // to webservices 
            return SessionID;

        public static String Logoff(String logoffUrlString, string SessionID)
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(logoffUrlString);          
            // set the session id in header
            req.Headers["JSESSIONID"] = SessionID;
            // make the HTTP call
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            return resp.StatusCode.ToString();



Hope it helps !


Preventing your system from getting auto locked

Within most of the organization, there is a default setting that after a few minutes have escaped, the system gets locked itself, if system is left unattended. Well to prevent this i have written a small utility (windows application) which periodically sends user input to the system, making it to believe that some mouse movement has been made and thus stopping the system from getting locked.

Here we would be calling SendInput function within user32.dll

The SendInput function synthesizes keystrokes, mouse motions, and button clicks and that is what we need.

This is useful in case we are giving presentation or else reading some documents …

Following is the code for the same

1) Create a new windows application in C#

2) Add a button named btn_Unlock to it and a timer control named timer1.

3) Set enabled to false and interval to 120000 for timer control.

4) Put the following code to your form class

public partial class frmKU : Form






[DllImport(“User32.dll”, SetLastError = true)]

public static extern int SendInput(int
nInputs, ref INPUT
pInputs, int cbSize);

public struct INPUT


int type;



public struct MOUSEINPUT


int dx;

int dy;

int mouseData;

int dwFlags;

int time;

int dwExtraInfo;


// Call the


void btnUnlock_Click(object
sender, EventArgs e)


timer1.Enabled = true;

input = new INPUT();

resSendInput = SendInput(5, ref input, Marshal.SizeOf(input));


void timer1_Tick(object
sender, EventArgs e)


input = new INPUT();

resSendInput = SendInput(5, ref input, Marshal.SizeOf(input));




Bye …

Unable to find a version of the runtime to run the application

I was facing the problem while trying to run one of my windows application created in Visual studio 2005 using .NET framework 2.0.

Few days back the application was running fine and today it on trying to run it i was getting the error.

The way it solved for me was to again open the project in solution explorer in visual studio and rebuilding it.


Creating Word document using C#

Create a new windows application project and add a button to it.

On click of that button, we will create a new document(word) and write a simple Hello World text in it.

To create a word document using C# we need to first reference the following DLL(com)


After adding reference, add this directive

using Microsoft.Office.Interop.Word;

Put this code on button click

    private void button1_Click(object sender, EventArgs e)


            object missing = System.Reflection.Missing.Value;

            object Visible=true;

            object start1 = 0;

            object end1 = 0;


            ApplicationClass WordApp = new ApplicationClass();

            Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            Range rng = adoc.Range(ref start1, ref missing);




                rng.Font.Name = “Georgia”;

                rng.InsertAfter(“Hello World!”);

                object filename = @”D:\MyWord.doc”;

                adoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,

                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                WordApp.Visible = true;


            catch (Exception ex)





The easiest way to write code for office interoperability is to make use of VBA code.

Say you want to insert a picture in a word document what you can do is

open the word document – Go to Tools ->Macro-> Record New Macro

Now click on insert menu and insert the picture. Stop the recording, again go to Macro -Macros-> Select your Macro and click on edit

You will find the vba code over there

Sub Macro1()

Selection.InlineShapes.AddPicture FileName:= _
“C:\Documents and Settings\nishantr1\My Documents\My Pictures\untitled.bmp” _
, LinkToFile:=False, SaveWithDocument:=True
End Sub

Now to write the same code in c# you will do something like this

Range rngPic = adoc.Tables[1].Range;

rngPic.InlineShapes.AddPicture(@”C:\anne_hathaway.jpg”, ref missing, ref missing, ref missing);


%d bloggers like this: