Monday, December 21, 2009

How to password protect an excel file in C#

If we need to protect an excel document with a password by some coding how will we do that? We may require to protect a report by some password after exporting it to MS Excel format. I'm going to demonstrate this by a Console Application with Visual C# code.

First create a console application and add a reference Microsoft.Office.Interop.Excel to it and just write the following piece of code. I've a standard excel file called "Test.xls".

Code:
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

namespace MyConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string filePath = @"c:\Test.xls";
Application application = new Application();
try
{
Workbook myWorkBook = application.Workbooks.Open(filePath,
Missing.Value,
false,
5,
Missing.Value,
Missing.Value,
Missing.Value,
XlPlatform.xlWindows,
Missing.Value,
false,
false,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value);

myWorkBook.Password = "koushik";
myWorkBook.Close(true, @"c:\Test2.xls", null);
Marshal.ReleaseComObject(myWorkBook);
}
catch (Exception ex)
{
application.Quit();
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}

Tuesday, June 2, 2009

My trip to Simla, Manali, Rohtang Pass and Delhi

We(me and my family) were planning for a trip this summer. After some discussion the route map was fixed. Decided that we would start with Simla followed by Manali and Rothang Pass and end it to Delhi where my elder sister stays. Started our journey on 16th of May 2009 by Kalka mail at 7.40 pm. Out of 7 members two (me nad my wife) boarded from Kolkata.


17th May(11.30 pm) - We reached Delhi station. Train was around 3 hrs late. My parents, elder sister, Jiju and my 13 years old nephew got into the train from there.


18th May(6.10am) - Finally the train got some momentum and reached Kalka station 1.5 hrs late. But the toy train from Kalka to Simla was scheduled to departure at 5.30 am. I was thinking what to do, because I thought the train was already left the station. Surprisingly found the train waiting for us!!! It was a nice small 1 boggy traing with 14 passenger capacity named by Rail Motor. Unfortunately I had 5 confirmed reserved seats and 2 waiting. After several requests the Ticket collector did not allow us(me and my wife) to board the train :-( . After that train's departure I did not get any reservation for two in any other toy train. So got into a passenger toy train which does not require any reservation and seats are available on first come first serve basis (lol...!!!). That train started moving at 8.30 am. With a pathetic speed with reached Simla after 6 hrs (normally toy trains take 4.5 to 5 hrs). But it was a nice experience regarding the scenic beauty and 103 tunnels that comes in between the journey. Here is a picture of that toy train.



(Toy train - Simla)


18th May (3.30 pm) -
We reached Simla. The car was waiting for us. After having some food we went to view some places of Simla. We went to Kufri, Green wall and some other places. Did some shopping in the famous Mall.



(Green wall - Simla)


19th May (8.00 am) -
We left out from Simla for Manali on road by a car. It took around 11 hrs from Simla. There is Kullu in between Simla and Manali. Due to road jam we had to avoid Kullu shopping center.



(Kullu)


19th May (8.30 pm) -
After reaching Manali we checked in the Hotel. It was Hotel Himangshu. A beautiful hotel with a charming lawn. The hills surrounded by snow were visible from there. We went out for a walk to see the local markets and the locality.



(Hotel Himangshu - Manali)

20th May (6 am) -
The car was ready. We got into it and started our journey to Rohtang Pass which is 51 km from Manali. It is 3979 meter / 13,051 feet high above the sea level. We just got spellbound after viewing the scenic beauty of it. The whole mountain range was covered by the snow. In winter the roads are used to be closed by the Indian Army. There was a great combination of cloud and snow at the peak of the mountains. They are kissing each other and sometimes being separated. But ice was melting and there was mud on the road. Anyway, finally we had selected a place and got down from the car. Enjoyed playing on and with the ice. The peak was very slippery. So had to get some stability while walking by the wooden stick which was available in rent. I was busy with my camera. The scenic view was changing very fast. It was very old over there and the cold wind was making our life tough. But forgetting the tough situation we were enjoying like anything! After spending around 3 hrs we had returned back to Manali. While returning back suddenly found some snow fall (wow! bonus!!!)... Before reaching hotel we had decided that we would visit
Solang valley. It was a nice small valley with lots of activities going around here and there like horse riding, free fall from the mountain while sitting in a big balloon etc etc... We did some horse riding so that we can view the whole place in quick time. After hovering around here and there we reached hotel.




(Rohtang Pass)


21st May (12 pm) -
We got ready, the packing was done and we are waiting for the car so that we could visit the local site scene and got into the bus for Delhi which was scheduled to leave from Manali at 3.30 pm. We visited many local sites of Manali. Went some temples and monastery too. Here is a picture of a monk who was not very pleased after I shot him with my camera.



( A monk - Manali)


22nd May (6.30 am) - We got down from the bus. It took around 14 hrs to reach Delhi. After that went to elder sister's house which took 1.5 hrs from there.

22nd May (1.30 pm) - We had our lunch and decided to visit some places of Delhi despite of having a very hectic journey which was continuing from some days. Anyway that didn't matter for us that time. We visited India gate, Raj ghat, Lotus temple and some other places.



(India Gate - New Delhi)


The next day (23rd May) we had our train to Kolkata. On 24th May we reached Kolkata and that's how our journey was ended. It was a very nice experience though the tour was very hectic. But surely it was a very memorable tour in my life till date.

You can view some more pictures of this tour on http://www.flickr.com/photos/koushikdas/

Happy traveling.
Cheers!
:-)

Tuesday, April 21, 2009

Project could not be opened because Microsoft Visual C# 2008 compiler could not be created

Did you face this problem in Visual Studio ever? When you open or create a new project then a error message come "Project could not be opened because Microsoft Visual C# 2008 compiler could not be created" followed by another error message like "QueryService for '{74946829-37A0-11D2-A273-00C04F8EF4FF}' failed."

If this error is buzzing you then there is an easy solution for this. First Close all Visual Studio instances that are presently running. Open Windows registry editor (run -> regedit) and go to HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Packages. There you will find a Key called SkipLoading. Check the value, it must be 0x00000001. Change it to 0x00000000 and you are done.
Just restart the Visual Studio and enjoy...
Happy Coding...

Monday, February 23, 2009

In Love with my Sony H10 digital camera

Pheu... Finally grabbed a digital camera which suited my pocket. Its Sony DSC H10. More upgraded than a normal Point and Shoot camera. It's an extended zoom type camera with 10x optical zoom. I love the big 3" LCD screen for the clear view of the pictures. It supports a shutter speed of 1/2000 sec which is reasonable. I am also happy with 8.1 mp.
But something I dislike about it are: Focusing a object when I am in Macro mode, its tough to focus properly. Secondly the grains in the picture due to noise. I think Sony should care about it. Thirdly, the battery discharges soon. Actually the culprit is the big LCD screen which consumes much energy, though it runs on Lithium battery. Lastly the accessories are costly than the other company make camera like Canon, Kodak etc of its kind.
I know all products have some pros and cons. so taking it into consideration, I am happy with it for now.
For more details about this gear Click Here.

But but but.... when I see any DSLR or the pictures taken by it I used to drool... OMG! stunning picture quality and stunning performance. Don't know when I will be financially able to grab a DSLR, but I will definitely go for it one day! and for now my dream gear is Canon 450D.

Now let me share some pictures taken with Sony H10

#1




#2




#3


#4


and there's many more...

To see some others please go here : http://www.flickr.com/photos/koushikdas/


Thanks and Regards
Koushik

Monday, May 5, 2008

Showing Reports by ReportViewer Control and NHibernate

Showing Reports by ReportViewer Control and NHibernate

In one of my Projects I was told to show some data using ReportViewer control and NHibernate. I had no idea how to do it.

Started Googling.... But I was quite surprised there was no such postings, even not found any information whether it is possible or not ! Never mind, started doing it and after completion found it pretty simple. Would like to share it with you.


Step 1:

I am using a single table, say Customers with the following schema :

Customers

CustomerID int IDENTITY(1,1) NOT NULL,

FirstName nvarchar(50) default NULL,

LastName nvarchar(50) default NULL,

Address nvarchar(150) default NULL,

PRIMARY KEY (CustomerID)

The Business Class is:

Customer.cs

namespace NHibernate.Example

{

public class Customer

{

public Customer()

{

}

private int id;

private string firstName;

private string lastName;

private string address;

public virtual int Id

{

get { return id; }

set { id = value; }

}

public virtual string FirstName

{

get { return firstName; }

set { firstName = value; }

}

public virtual string LastName

{

get { return lastName; }

set { lastName = value; }

}

public virtual string Address

{

get { return address; }

set { address = value; }

}

}

}

And Here is the Mapping file:

Customer.hbm.xml
(Click to view)



Step 2:

We have to create a Typed Dataset in App_Code folder of our Website (or you can have your separate Project Library for DataSet). The data table of this DataSet will contain the fields which we want to show in our Report. Here, I would like to show all the fields of my Customers table. Therefore my Dataset looks like this :



Step 3 :

Create a RDLC file. Arrange the fields just by dragging and dropping the fileds of Dataset which you will find "Website data Sources" toolbar (to view this toolbar just click the RDLC body, go to the menu "Data" --> "Show Data Sources").



Step 4:

Now the whole environment is ready for coding. Just add a ReportViewer to an aspx page and use the following code and you are done to throw some data on your screen!

using System;

using System.Collections;

using Microsoft.Reporting.WebForms;

using NHibernate;

using NHibernate.Example;

public partial class CustomerReport : System.Web.UI.Page

{

DataSet1 ds = new DataSet1();

NHibernate.Cfg.Configuration newConfig = new NHibernate.Cfg.Configuration();

ISessionFactory sFactory;

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

newConfig = newConfig.AddAssembly("NHibernate.Example");

sFactory = newConfig.BuildSessionFactory();

rvCustomer.LocalReport.ReportPath = "TestReport.rdlc";

rvCustomer.LocalReport.ReportEmbeddedResource = "ReportViewer.TestReport.rdlc";

rvCustomer.LocalReport.DataSources.Clear();

ISession session = sFactory.OpenSession();

//get the list of customers using NHibernate

IList customers = session.CreateCriteria(typeof(Customer)).List();

//populate the typed Dataset

foreach (Customer customer in customers)

{

ds.Customers.AddCustomersRow(customer.Id.ToString(),

customer.FirstName,

customer.LastName,

customer.Address);

}

rvCustomer.LocalReport.DataSources.Add(

new ReportDataSource("DataSet1_DataTable1", ds.Customers));

}

// data Source name "DataSet1_DataTable1" generally made by DataSet name + "_"

// + DataTable name of the DataSet

}

}


Finally the Output :


----------------------------------------------------------

Saturday, July 7, 2007

Photos of Digha

I am not a Professional Photographer but love to shoot. I used to shoot with my old Kodak KB10 camera! Thinking of buying a Digital Camera, but I have not a detailed knowledge which one is better and what are the requirements of a good camera. So, any suggestion is invited cordially. Before joining eFORCE, I went to Digha and took some pictures with my friend's Digital camera. Here are some of them...

=> => =>


---- ~ -----

Place of Root


Hi Friends...

This is Koushik Das form Kolkata... Basically I am from Panagarh. I know many of you didn't hear about this place. Hey!!! got an idea... Why not share some of my knowledge and experience about this place? That will be a great fun... So here it is... Something about the place which is my root...

Panagarh(23.40N, 87.25E) is a small town situated under Durgapur subdivision in Burdwan district of West Bengal and near about 150 km for Kolkata. Any nature lover may fall in love with its intrinsic beauty where your lungs can feel free to breath. Many small and huge forests (eg, 11 miles) are there where you can find the place of solitude which can help for a complete relaxation. Two mighty rivers "Damodar" and "Ajay" flow nearby this area which have some economical value too and I think DVC(Damodar Vally Corporation) is the best example.

Not only the beautiful nature, Panagarh is also enriched with some historical values. There is a very ancient Air Force camp. This is very rare place where both the Indian Army and Air force have their establishment. There is an Airport which is extensively used by Indian Air Force. You can not find the name of Panagarh in an ancient political map because it was a secrete place of Indian Air force and Army. Now a days the Air force had shifted their base to little more inside than before because of some heavy damages of the runway and some interesting places like "firing squad ground", "cannon place" and the residential place of the Army persons. Now this huge area is now uncovered to any body and for this reason this abandon place of Indian Army has become the most happening place of spending leisure afternoons and evenings.

A very beautiful place is there nearby Panagarh which is called Randeeha. There is a barrage here and a beautiful river side place. I took some pictures with my mobile which may describe this place (Randeeha) better than me.



Now the road transportation of Panagarh has improved a lot. Now Kolkata is directly connected to it on road and takes just less than 3 hours in Bus which is very much available now a days. The Grand Trunk road (GT Road) and NH 34 have connected Panagarh to various other places which made life easy for the people.

That is some extended view of Panagarh where I born and studied till my 10th standards in the Panagarh Kanksa High school.

If you want to know something more about this place then please let me know.


---- ~ ----