Monday, September 29, 2008

Open Access 2007 database (*.accdb) with ASP.net

The new office suite version 2007 comes with brand-new file formats. One of them is the new Access database format version 2007 with accdb extension. Of course there is a new connection string for those new access databases. You can use the connection string below to access your accdb databases through asp.net.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database1.accdb;Persist Security Info=False;

In addition, if you are using passwords to access your database, you can include it in your connection string like below;

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database1.accdb;Jet OLEDB:Database Password=MyDbPassword;

However, your hosting provider will not be aware of this upgrade or probably you don’t want to install Access 2007 on your server. Actually, you don’t need to. Just install 2007 Office System Driver: Data Connectivity Components

Friday, September 12, 2008

Create and Install SSL Certificate in IIS


You have created an application say for example a web service and want to secure it but wondering how to create and install SSL certificate in order to test it?, then do not worry; just read this article :)
So before proceeding, let’s make sure you have Internet Information Services (IIS) 6.0 Resource Kit Tools installed on your machine.


You can download this from here: http://www.microsoft.com/downloads/details.aspx?familyid=56FC92EE-A71A-4C73-B628-ADE629C89499&displaylang=en Size: 5.8 MB.


Once you finish downloading, install it. Now go to Start menu and look for “IIS Resources” and under this, look for “Self SSL”. Run the exe (which looks similar to command prompt).

The syntax of the command is:

SelfSSL [/T] [/N:CN] [/K:key Size] [/S:site id] [/P:port]

Now type the following command:
selfssl.exe /T /N:CN="your machine name" /K:1024 /V:7 /S:1 /P:443

Just specify your machine’s name. You can also change the parameters if required.
Meaning of these parameters is as follows:

/T Adds the self-signed certificate to "Trusted Certificates" list. The local browser will trust the self-signed certificate if this flag is specified.

/N:CN Specifies the common name of the certificate. The computer name is used if not specified.

/K:key size Specifies the key length. Default is 1024.

/V:validity days Specifies the validity of the certificate. Default is 7 days.

/S:site id Specifies the id of the site. Default is 1 (Default site).

/P:port specifies the SSL port. Default is 443.

You can verify your newly installed certificate from IIS. It will look similar to the one shown above.

Oh! yea, please do not forget to leave a comment on this post.


Wednesday, September 10, 2008

Create .exe file from C# code

Wow! What a gr88 feature in .Net… Now you can create your own executable file directly on the runtime. Just supply the source code and create an .exe as you want it to be.

Here is the code snippet; I was surprised to see how easy it was.

Then just place this code in a Button Click event.

Microsoft.CSharp.CSharpCodeProvider cp = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler ic = cp.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters cpar = new System.CodeDom.Compiler.CompilerParameters();
string pathIcon = Application.StartupPath + "\\icon.ico";
cpar.CompilerOptions = "/target:winexe" + " " + "/win32icon:" + "\"" + pathIcon + "\"";
cpar.IncludeDebugInformation = false;
cpar.TreatWarningsAsErrors = false;
cpar.OutputAssembly = Application.StartupPath + "\\MyApplication.exe";
cpar.GenerateInMemory = false;
cpar.GenerateExecutable = true;
cpar.ReferencedAssemblies.Add("System.dll");
cpar.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cpar.ReferencedAssemblies.Add("System.Drawing.dll");

//List all dll's to which your program would refer to.
string src = //C-Sharp source code (Or you can directly give the path of .cs file)
System.CodeDom.Compiler.CompilerResults cr = ic.CompileAssemblyFromSource(cpar, src.Trim());
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) MessageBox.Show(ce.ErrorText);


Here in the code, you can see that I have specified a path for an icon of my application “icon.ico”. You can just change the path and file name with your own file.
Also, same is with the application name “MyApplication.exe”; give what ever name you like :-)

That’s all! This way you can create your own application… Enjoy…
And yea, please do not forget to leave a comment on this post.

Sunday, September 7, 2008

Add, Delete and Move controls at runtime.


In this article you will learn how to:
· Add or delete controls at runtime.
· Access controls with the indexer.
· Create control collection using Hash Table.

Download full source code.
To start with, create a windows application project and add a class to it. We will create a button array so name it as “ButtonArray.cs”.

Add the following code to it:

using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ManageControls
{
public delegate void SendSelectedButton(object sender);
public class ButtonArray : Hashtable
{
private readonly Form HostForm;
public event SendSelectedButton SelectedButton;
Point buttonLocation;
int cntButton = 0;

public ButtonArray(Form host)
{
HostForm = host;
}

public void AddButton(int left, int top)
{
Button btnElement = new Button();

btnElement.Top = top;
btnElement.Left = left;
btnElement.Tag = cntButton;
btnElement.Cursor = System.Windows.Forms.Cursors.Default;
btnElement.FlatStyle = System.Windows.Forms.FlatStyle.System;
btnElement.Text = "Button " + cntButton.ToString();
btnElement.Click += new EventHandler(btnElement_Click);
btnElement.MouseDown += new MouseEventHandler(btnElement_MouseDown);
btnElement.MouseMove += new MouseEventHandler(btnElement_MouseMove);
this.Add(cntButton.ToString(), btnElement);
HostForm.Controls.Add(btnElement);
btnElement.BringToFront();
cntButton++;
}

public void RemoveButton(string btnIndex)
{
if (this.Count > 0)
{
HostForm.Controls.Remove((Button)this[btnIndex]);
this.Remove(btnIndex);
}
}

private void btnElement_Click(Object sender, System.EventArgs e)
{
if(null != SelectedButton)
SelectedButton(sender);
}

private void btnElement_MouseDown(object sender, MouseEventArgs e)
{
buttonLocation = e.Location;
}

private void btnElement_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Button)sender).Left += e.X - buttonLocation.X;
((Button)sender).Top += e.Y - buttonLocation.Y;
}
}
}
}
Class “ButtonArray” is inherited from the Hash table, so it will be easy for us to add buttons in the collection. Let’s have a look into the “AddButton” method.


public void AddButton(int left, int top)
{
Button btnElement = new Button();
btnElement.Top = top;
btnElement.Left = left;
btnElement.Tag = cntButton;
btnElement.Cursor = System.Windows.Forms.Cursors.Default;
btnElement.FlatStyle = System.Windows.Forms.FlatStyle.System;
btnElement.Text = "Button " + cntButton.ToString();
btnElement.Click += new EventHandler(btnElement_Click);
btnElement.MouseDown += new MouseEventHandler(btnElement_MouseDown);
btnElement.MouseMove += new MouseEventHandler(btnElement_MouseMove);
this.Add(cntButton.ToString(), btnElement);
HostForm.Controls.Add(btnElement);
btnElement.BringToFront();
cntButton++;
}

In this method, we create an instance of a button with the necessary properties and add it to the hash table as well as to the form’s control collection. Each time the control is added in the hash table with its unique key. This helps us to access the controls when required.
Now we’ll see how to remove control from the hash table and form’s control collection.
In order to remove the control, just pass the key. That’s simple! Isn’t it? :-)


public void RemoveButton(string btnIndex)
{
if (this.Count > 0)
{
HostForm.Controls.Remove((Button)this[btnIndex]);
this.Remove(btnIndex);
}
}


Ok! That was about the methods to add and remove controls from the collection. Now we will see how to use this class from the UI (Form).
First thing to do is to initialise the ButtonArray class and a button.


ButtonArray buttonArray;
Button btnSelected = new Button();

Now create two more buttons on the form and name them as “AddButton” and “RemoveButton”.
Code them as shown below.


private void AddButton_Click(object sender, EventArgs e)
{
isClicked = true;
}
private void RemoveButton_Click(object sender, EventArgs e)
{
buttonArray.RemoveButton(btnSelected.Tag.ToString());
}

Under the Mouse Down event of the form, code as shown below.

private void frmMain_MouseDown(object sender, MouseEventArgs e)
{
if (isClicked)
{
buttonArray.AddButton(e.X, e.Y);
isClicked = false;
}
}


You can add other controls as well. Just create array class for each control type and use it.

And yea, please do not forget to leave a comment on this post.