Asp.net tips

Asp.net tips for improving the development performance


click here to download

Excel Data Reader - Read Excel files in .NET

Lightweight and fast library written in C# for reading Microsoft Excel files ('97-2007).
Cross-platform:
- Windows with .Net Framework 2
- Windows Mobile with Compact Framework
- Linux, OS X, BSD with Mono 2+

cllick here to download

How do I prevent a user from moving a form at run time?

The following code snippet shows how you can prevent a user from moving a form at run time:

[C#]
protected override void WndProc(ref Message m)
{
const int WM_NCLBUTTONDOWN = 161;
const int WM_SYSCOMMAND = 274;
const int HTCAPTION = 2;
const int SC_MOVE = 61456;

if((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE))
{
return;
}

if((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION))
{
return;
}

base.WndProc (ref m);
}

[VB.NET]
Protected Overrides Sub WndProc(ByRef m As Message)
const Integer WM_NCLBUTTONDOWN = 161
const Integer WM_SYSCOMMAND = 274
const Integer HTCAPTION = 2
const Integer SC_MOVE = 61456

If (m.Msg = WM_SYSCOMMAND) &&(m.WParam.ToInt32() = SC_MOVE) Then
Return
End If

If (m.Msg = WM_NCLBUTTONDOWN) &&(m.WParam.ToInt32() = HTCAPTION) Then
Return
End If

MyBase.WndProc( m)
End Sub

How do I make my child Form fill the entire mdi client without being maximized?

Here is how it can be done. This takes into account all docked controls (including menus) in the mdi parent form.

[C#]
private void FillActiveChildFormToClient()
{
Form child = this.ActiveMdiChild;
Rectangle mdiClientArea = Rectangle.Empty;
foreach(Control c in this.Controls)
{
if(c is MdiClient)
mdiClientArea = c.ClientRectangle;
}
child.Bounds = mdiClientArea;
}



[VB.Net]
Private Sub FillActiveChildFormToClient()
Dim child As Form = Me.ActiveMdiChild
Dim mdiClientArea As Rectangle = Rectangle.Empty
Dim c As Control
For Each c In Me.Controls
If TypeOf c Is MdiClient Then
mdiClientArea = c.ClientRectangle
End If
Next
child.Bounds = mdiClientArea
End Sub

Avoid duplicate child form in MDI

How do I check to see if a child form is already displayed so I don't have two instances showing?

// MyChildForm is the one I'm looking for

MyChildForm childForm = null;
foreach(Form f in this.MdiChildren)
{
if(f is MyChildForm)
{
// found it
childForm = (MyChildForm) f;
break;
}
}

if( childForm != null)
{
childForm.Show();
childForm.Focus();
}
else
{
childForm = new MyChildForm();
childForm.MdiParent = this;
childForm.Show();
childForm.Focus();
}

Read Pdf file using c#

So you’ll have to download the PDFBox package. In this package you’ll find a bin directory. To read your PDF file, you’ll need the following files:

* IKVM.GNU.Classpath.dll
* PDFBox-0.7.3.dll
* FontBox-0.1.0-dev.dll
* IKVM.Runtime.dll

PDF Box Package is free distribution tool







You’ll have to add a reference to the first two in your project. You’ll also have to copy the last two on your project’s bin directory.


The program will look something like this (if you’re working with a Console application):



using System;
using org.pdfbox.pdmodel;
using org.pdfbox.util;
namespace PDFReader

{
class Program
{
static void Main(string[] args)
{
PDDocument doc = PDDocument.load("lopreacamasa.pdf");

PDFTextStripper pdfStripper = new PDFTextStripper();

Console.Write(pdfStripper.getText(doc));
}

}

}

read Excel file using c#

How to read an Excel file with OleDb and a simple SQL query?

This approach is extremely useful when you need to read the
data from an Excel file fast and store the data in a DataTable
for further usage.

using System.Data;
using System.Data.OleDb;

...

String sConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + [Your Excel File Name Here] + ";" +
"Extended Properties=Excel 8.0;";


OleDbConnection objConn = new OleDbConnection(sConnectionString);

objConn.Open();

OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;

DataSet objDataset1 = new DataSet();

objAdapter1.Fill(objDataset1);

objConn.Close();

unicode character in C# names

You can use any unicode character in C# names, for example:

public class MyClass
{
public string Hårføner()
{
return "Yes, it works!";
}
}

Using @ for variable names that are keywords.

var @object = new object();
var @string = "";
var @if = IpsoFacto();

shortcut for creating properties

Has anybody used "props"?

You type "prop" and then press [TAB] twice, it generates useful code for your properties and can speed your typing.

I know this works in VS 2005 (I use it) but I don´t know in previous versions.

Bind countries from CultureInfo class in C#

Some people have asked me how BlogEngine.NET displays a dropdown list of countries when no source XML file is present. The simple answer is that you don’t need any external list to bind to from C#, you can instead use the CultureInfo class.

Consider that you have the following dropdown list declared in an ASP.NET page:



Then from code-behind, call this method which binds the countries alphabetically to the dropdown:

public void BindCountries()
{
System.Collections.Specialized.StringDictionary dic = new System.Collections.Specialized.StringDictionary();
System.Collections.Generic.List col = new System.Collections.Generic.List();

foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
{
RegionInfo ri = new RegionInfo(ci.LCID);
if (!dic.ContainsKey(ri.EnglishName))
dic.Add(ri.EnglishName, ri.TwoLetterISORegionName.ToLowerInvariant());

if (!col.Contains(ri.EnglishName))
col.Add(ri.EnglishName);
}

col.Sort();

ddlCountry.Items.Add(new ListItem("[Not specified]", ""));
foreach (string key in col)
{
ddlCountry.Items.Add(new ListItem(key, dic[key]));
}

if (ddlCountry.SelectedIndex == 0 && Request.UserLanguages != null && Request.UserLanguages[0].Length == 5)
{
ddlCountry.SelectedValue = Request.UserLanguages[0].Substring(3);
}
}

The method first adds all the countries from the CultureInfo class to a dictionary and then sorts it alphabetically. Last, it tries to retrieve the country of the browser so it can auto-select the visitors country. There might be a prettier way to sort a dictionary, but this one works.

C# coding Standards

csharp coding style

Tabbed MDI Dock panel

It's free one



Validating ASP.NET CheckBox

html 5 cheat sheet

HTML cheat for your reference with more options




css3 cheat sheet

css3 style sheet reference for enhance web development



Difference between two dates

This snippet calculates the difference between two dates.

DateTime d1 = new DateTime( 2000, 7, 13 );
DateTime d2 = new DateTime( 2003, 12, 5 );
TimeSpan s = d2 - d1;
int numberofdays = s.TotalDays

Ajax chat Application

This application shows how to develop a simple AJAX chat application. It uses the ScriptManager and UpdatePanel Class to get messages stored in a SQL Server database.



Visual C# 2005 Keyboard Shortcut

keyboard shorctut reference poster from Microsoft

click here

Dot net keyboard shortcuts

Key board shortcut keys always help to increase your speed. So i am giving below some of the Keyboard Shortcuts which will be very useful.

Ctrl + N :- Opens the New Project Dialogue Box
Ctrl + Shift + O :- Opens the Open File Dialog Box
Ctrl + Shift + A :- Opens Add New Item window
Ctrl + D :- Opens Add Existing Item window
Ctrl + F :- Opens Find window
Ctrl + H :- Opens Find and Replace window
Ctrl + Shift + H :- Opens Replace in Files window
Ctrl + Alt + Shift + F12 :- Opens Find Symbol window
F7 :- Opens Code Designer window
Shift + F7 :- Gets you back to Design View
Ctrl + R :- Opens the Solution Explorer window
Ctrl + Alt + S :- Opens the Server Explorer window
Ctrl + Shift + C :- Opens the Class View window
F4 :- Opens the Properties window
Ctrl + Shift + E :- Opens the Resource view window
Ctrl + Alt + X :- Opens the Toolbar window
Shift + Alt + Enter :- Takes you to Full Screen View
Alt+F8 :- Opens Macro Explorer window
F2 :- Opens Object Browser window
Ctrl + Alt + T :- Opens Document Outline window
Ctrl + Alt + K :- Opens Task List window
Ctrl + Alt + A :- Opens Command window
Ctrl + Alt + O :- Opens Output window
Ctrl + Alt + Y :- Opens Find Symbol Results window
Ctrl + Alt + F :- Lists Items under the Favorites Menu in your
Ctrl + Shift + B :- Builds your project
Ctrl + Shift + F9 :- Clears All Breakpoints
Ctrl + Alt + P :- Opens the Processes Dialog box
Ctrl + T :- Opens Customize Toolbox window
Ctrl + Shift + P :- Runs Temporary Macro
Ctrl + Shift + R :- Records Temporary Macro
Alt + F11 :- Opens Macros IDE
F5 :- Runs your Application
Ctrl + F5 :- Runs your Application without Debugging
Ctrl + Alt + E :- Opens the Exceptions Dialog Box
F8 :- Used while Debugging Applications
Shift + F8 :- Used While Debugging Applications
Ctrl + B :- Inserts a New Breakpoint
F10:-Line By Line execution
Ctrl+Tab:-To Shift B/W .net Editor Pages

c# 2005 cheat sheet

Store Images in Database

Sample application for storing and getting images from sql server.....

click here to download

Clear all TextBox values

protected void ClearControl(Control Ctrl)//passing the control
{
foreach (Control eachCtrl in Ctrl.Controls)//loop taking each controls
{
if (eachCtrl.Controls.Count > 0)
ClearControl(eachCtrl);
else if (eachCtrl is TextBox)
((TextBox)eachCtrl).Text = null;
}
}

To call the method like this:

ClearControl(this);