ASP.NET MVC Action filters

January 14th, 2009

I love the really cool Action Filter’s in ASP.Net MVC. I use it to keep my code clean and reusable. This post is not about what is an Action Filter but is more about how to use them to have cleaner and reusable components.
Read the rest of this entry »

Calculating width of TextBox dynamically based on Text in C#

December 23rd, 2008

Sometimes we need to expand the TextBox dynamically based on the length of the entered text. TextBox does not have a AutoSize property by which it can adjust it’s size based on the content. On way of calculating the Width for the TextBox is using the Graphics.MeasureString and calculating the Pixel width. This method has it’s Pros and cons.

Read the rest of this entry »

WPF Binding Datagrid to DataTable

December 17th, 2008

The following code snippet demonstrates different ways to bind a ADO.Net DataTable to a WPF Datagrid.

WPF Datagrid can be found at this location - Codeplex

1. Setting the ItemsSource of the Datagrid to the DataTable’s DefaultView
XAML,

<Window x:Class="WpfDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dtgrd="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
    Title="Window1" Height="400" Width="400">
    <Grid  Name="_maingrid">
        <dtgrd:DataGrid
                         x:Name="_dataGrid"
                         ColumnHeaderHeight="25"
                         AutoGenerateColumns="True"
                              >
        </dtgrd:DataGrid>
    </Grid>
</Window>

Code-behind,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;  

namespace WpfDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private DataSet _ds;  

        public Window1()
        {
            InitializeComponent();
        }  

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);  

            ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);  

            DataColumn cl = new DataColumn("Col1", typeof(string));
            cl.MaxLength = 100;
            dt.Columns.Add(cl);  

            cl = new DataColumn("Col2", typeof(string));
            cl.MaxLength = 100;
            dt.Columns.Add(cl);  

            DataRow rw = dt.NewRow();
            dt.Rows.Add(rw);
            rw["Col1"] = "Value1";
            rw["Col2"] = "Value2";  

            _datagrid.ItemsSource = ds.Tables[0].DefaultView;
        }
    }
}

2. Setting the DataContext to the table and then binding ItemsSource to the DataContext
XAML,

<Window x:Class="WpfDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dtgrd="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
    Title="Window1" Height="400" Width="400">
    <Grid  Name="_maingrid">
        <dtgrd:DataGrid
                         x:Name="_dataGrid"
                         ItemsSource="{Binding Path=.}"
                         ColumnHeaderHeight="25"
                         AutoGenerateColumns="True"
                              >
        </dtgrd:DataGrid>
    </Grid>
</Window>

Code-behind,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;  

namespace WpfDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private DataSet _ds;  

        public Window1()
        {
            InitializeComponent();
        }  

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);  

            ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);  

            DataColumn cl = new DataColumn("Col1", typeof(string));
            cl.MaxLength = 100;
            dt.Columns.Add(cl);  

            cl = new DataColumn("Col2", typeof(string));
            cl.MaxLength = 100;
            dt.Columns.Add(cl);  

            DataRow rw = dt.NewRow();
            dt.Rows.Add(rw);
            rw["Col1"] = "Value1";
            rw["Col2"] = "Value2";  

            _datagrid.DataContext = ds.Tables[0];
        }
    }
}

Have fun.

Detecting removable drive types using C#

November 20th, 2008

Ever wanted to find out the different drives or just the removable drives on the machine ?? The DriveInfo class is the answer to your prayers.

Read the rest of this entry »

JavaScript Replace selected text in a Text box

November 12th, 2008

The following code snippet demonstrates how to get the selected text from a Text box and then replace it with some different text using JavaScript.
Read the rest of this entry »

IIS7 Executing Directory

October 27th, 2008

Generally we get the executing directory path using the HttpContext.Request.PhysicalPath etc. In IIS7 this won’t work. We cannot access the HttpContext.Request object in Global.ascx.
This is the way to do this,

int binPos = HttpRuntime.BinDirectory.LastIndexOf("\\bin");
string executingDirectory = HttpRuntime.BinDirectory.Remove(binPos);

Have fun.

WPF Dispatcher CheckAccess not displaying in intellisense

October 16th, 2008

This one really struck me hard. I am doing a threading scenario with some UI feedback marshaled back on the UI thread. And you won’t believe it, I am amazed to find CheckAccess missing from the Dispatcher object. Intellisense wouldn’t just show me CheckAccess(). Rubbing my eyes and again checking, it still seems to have vanished…

Read the rest of this entry »

C# Converting Time between TimeZones in C#

October 2nd, 2008

.Net 3.5 has introduced a new TimeZoneInfo class which is an improvement over the existing TimeZone class. The TimeZone class can work on local TimeZone only i.e. converting between UTC and Local time zone only. The new TimeZoneInfo class provide much more richer functionality of working with different other Time zone’s.
Read the rest of this entry »

Get user IP address

September 25th, 2008

Well, yes, it looks like trivial but it isn’t. Many a times we need to get the IP address of the user. There are a handful of suggestions to do this. I discuss the methods and the short comings in them.
Read the rest of this entry »

Javascript Replace all occurrences

August 12th, 2008

This code snippet demonstrates how to use Javascript to replace all occurrences of a particular text in a given string.

The normal way to use Javascript replace is as follows,

var newText = oldText.replace("textToBeReplaced", "textToBeReplacedWith");
The above code will replace the first occurence of "textToBeReplaced" with "textToBeReplacedWith". Yes, only the FIRST occurrence.
Read the rest of this entry »