This article sais it all.
Monday, December 29, 2008
Tuesday, August 12, 2008
ASP.NET Custom Error pages
A great explanation about where and how errors can be caught in ASP.NET application and how to build custom error pages.
Saturday, July 19, 2008
ASP.NET: Extract data from a SQLDataSource to a DataTable
1. Dim dv As New System.Data.DataView
2. Dim dt As New System.Data.DataTable
3. dv = mySQLDataSource.Select(DataSourceSelectArguments.Empty)
4. dt = dv.ToTable()
Jump between braces in Visual Studio
Put your cursor before or after the brace (your choice) and then press Ctrl+].
Thursday, March 6, 2008
Introduction to Windows Workflow Foundation
- Intro to Windows Workflow Foundation (Part 1 of 7): Workflow in Windows Applications (Level 100)
- Intro to Windows Workflow Foundation (Part 2 of 7): Simple Human Workflow Using E-mail (Level 200)
- Intro to Windows Workflow Foundation (Part 3 of 7): Hosting and Communications Options in Workflow Scenarios (Level 300)
- Intro to Windows Workflow Foundation (Part 4 of 7): Workflow, Messaging, and Services: Developing Distributed Applications with Workflows (Level 300)
- Intro to Windows Workflow Foundation (Part 5 of 7): Developing Event Driven State Machine Workflows (Level 300)
- Intro to Windows Workflow Foundation (Part 6 of 7): Extending Workflow Capabilities with Custom Activities (Level 300)
- Intro to Windows Workflow Foundation (Part 7 of 7): Developing Rules Driven Workflows (Level 300)
Sunday, March 2, 2008
Sunday, February 10, 2008
Friday, February 1, 2008
Debugger variable $exception
If your catch block do nothing with caught exception you may declare block argument without name (to avoid warning message "CS0168: The variable 'ex' is declared but never used"):
try
{
...
}
catch (Exception)
{
// deliberately suppressing all exceptions
}
But one day during debugging you may actually want to examine Exception. Since you don't have variable where exception is stored you can use debugger variable $exception provided by the Visual Studio.NET 2005 Debugger to examine the exception in a catch block. Just add it to Watch Window.
Consider using System.IO.Path.Combine() instead of string concatenation
Let's review the following code for creating a file path:
public string GetFullPath(string fileName) { string folder = ConfigurationManager.
AppSettings["MyFolder"]; return folder + fileName; }
This code is prone to error. For example, when you set the folder setting, you have to remember to make sure it ends with a slash. To avoid such problems use Path.Combine() method which will ensure that the folder has ending slash:
public string GetFullPath(string filename) { string folder = ConfigurationManager.
AppSettings["MyFolder"]; return Path.Combine(folder, filename); }
Monday, January 28, 2008
C# Events
using System;
using System.Windows.Forms;
namespace EventsMadeSimple
{
public partial class Form1 : Form
{
SimpleClass SimpleObject = new SimpleClass(); // create a new SimpleClass object
public Form1() // Form1 constructor
{
InitializeComponent();
Load += new EventHandler(Form1_Load); // add Form1's Load event handler
// add our object's event handler
SimpleObject.SimpleEvent += new OnSimpleEvent(SimpleEventOccurred);
}
void Form1_Load(object sender, EventArgs e)
{
SimpleObject.SimpleMethod(); // call the SimpleObject's method
}
private void SimpleEventOccurred() // method is called when the event is raised
{
listBox1.Items.Add("SimpleEvent was fired at " + DateTime.Now.ToString());
}
}
public delegate void OnSimpleEvent(); // delegate
public class SimpleClass
{
public event OnSimpleEvent SimpleEvent; // event
public void SimpleMethod()
{
SimpleEvent(); // raise the event
}
}
}
There are two classes here. The standard windows generated Form1 and SimpleClass. SimpleClass is our own class with one event and one method.
Inside Form1 we've created a new instance of SimpleClass called SimpleObject.
In Form1's constructor, we've added an event handler (this makes it 'listen' for the specified event and call the specified method) for loading of Form1, and another for our SimpleObject's event. This calls the SimpleEventOccurred method in Form1 when the event is raised.
In Form1's load method we call SimpleObject's SimpleMethod, which is where the event gets raised. This method could be called from anywhere but I've placed it in the Load so it happens immediately when you run the project.
How easy was that?
Experiment Suggestions
Passing parameters: You will often see something similar to this in the signatures of methods called by events:
(object sender, EventArgs e)
If you want to do this or anything similar you will also need to give your delegate the same signature and pass valid paramaters when raising the event i.e.
public delegate void OnSimpleEvent(object sender, EventArgs e);
......
SimpleEvent(this, EventArgs.Empty); // raise the event
Custom EventArgs: You can create your own class that inherits from System.EventArgs
public class SimpleEventArgs : EventArgs { }
and pass an instance of this class in your parameters. This class can obviously have its own propeties/methods/constructors etc that can be manipulated and/or made accessible to the receiving method.
Return type: The method that gets called by the event handler (
private void SimpleEventOccurred() in our case) can return a value type. To do this you will need to specify the same type in your delegate declaration. The call to SimpleEvent() will then be returned a value of type you specified.
Saturday, November 10, 2007
Single Instance Application
Making an application single instance, can be achieved by using a mutex (Mutual Exclusion Semaphore).
A Windows application loads the main form through the Application.Run( ) method. In the Main method, create a new mutex. If a new mutex is created the application is allowed to run. If the mutex has already been created, the application cannot start. This will ensure that only one instance will be running at any time.
// Used to check if we can create a new mutex
bool newMutexCreated = false;
// The name of the mutex is to be prefixed with Local\ to make
// sure that its is created in the per-session namespace,
// not in the global namespace.
string mutexName = "Local\\" +
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
Mutex mutex = null;
try
{
// Create a new mutex object with a unique name
mutex = new Mutex(false, mutexName, out newMutexCreated);
}
catch(Exception ex)
{
MessageBox.Show (ex.Message+"\n\n"+ex.StackTrace+
"\n\n"+"Application Exiting...","Exception thrown");
Application.Exit ();
}
// When the mutex is created for the first time
// we run the program since it is the first instance.
if(newMutexCreated)
{
Application.Run(new AnimatedWindowForm());
} When a new mutex is created the mutex name can be prefixed with either Global\ or Local\. Prefixing with Global\ means the mutex is effective in the global namespace.
Prefixing with Local\ means the mutex is effective in the user's session namespace only.
Windows XP and Windows 2003 allow fast user switching through Terminal Services Sessions. So if a mutex is created with a Global\ prefix, the application can have only one instance system wide. So if one user launches the application, other users cannot create a second instance in their sessions. If the mutex is not prefixed with Local\ it is effective per session only.
Placing Your C# Application in the System Tray
- To get started, open an existing C# Windows form (or create a new one).
- Open the Visual Studio Toolbox.
- Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
- Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. For example, this value could be "KillerApp 1.0".
- Set the control's Icon property to the icon that you want to appear in the System Tray
- Add an event handler for the form's Resize event that will hide the application when it's minimized. That way, it won't appear on the task bar.
- Add an event handler for the NotifyIcon.DoubleClick event and code it as follows so that the application will be restored when the icon is double-clicked.
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
private void notifyIcon1_DoubleClick(object sender,
System.EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
At this point, your application will fuction perfectly in terms of an icon appearing in the System Tray when the application is run (see Figure 1), the application not appearing on the task bar when minimized and the application restoring itself when the Tray icon is double-clicked.
Figure 1
Now, let's see the steps involved with adding a context menu to the icon.
- From the Visual Studio Toolbox, drag a ContextMenu control onto the form.
- Right-click the ContextMenu control and select the Edit Menu.option.
- Type in the options that you want to appear in your context menu. For example, you can add options such as Restore and Close Application.
- As with any menu, double-click the menu item to create and code each item's handler. As an example, you could copy the code from the form's DoubleClick handler into the context menu's Restore handler and for the Close Application menu item; simply call the form's Close method.
- Finally, set the NotifyIcon control's ContextMenu property to the new context menu you just created by selecting the menu from the drop-down list. Figure 2 shows a simple Tray context menu.