Search Here

Monday, April 20, 2009

Article on Garbage Collector -- Good Read

A beautiful article on GC by Abhinaba Basu is explained thoroughly at his blog

http://blogs.msdn.com/abhinaba/

Thursday, April 16, 2009

Key-Value Functionality in Combo Box

Combo box control is a control which displays a list of items. It combines the features of a text box and a list box. For the item which is selected in the combo box, we can get the selected index, which is in order of 0, 1, 2..

Today, we will learn how we can assign a key value pair to a combo box. The value will be shown to the end user and on the basis of the value, you can get the key.

E.g.: Consider the following key, value pairs:

Key: 1, Value: Rohit
Key: 2, Value: Suraj

Now, the combo box will show the values to the end user as Rohit, Suraj and for the selected item, you can get the key as either 1 or 2.

It is a simple three step procedure and am explaining it with some code samples.

Step 1: Create a new public class, say, KeyValuePair as
   public class KeyValuePair
{
public object m_objKey;
public string m_strValue;

public KeyValuePair(object NewKey,string NewValue)
{
m_objKey = NewKey;
m_strValue = NewValue;
}

public override string ToString()
{
return m_strValue;
}
}
Step 2: From your Main class, add the items to combo box as
      comboBox1.Items.Add(new KeyValuePair("1", "Rohit"));
comboBox1.Items.Add(new KeyValuePair("2", "Suraj"));
comboBox1.Items.Add(new KeyValuePair("3", "Sandeep"));
comboBox1.Items.Add(new KeyValuePair("4", "Jagdeep"));
comboBox1.SelectedIndex = 0;
Step 3: Now you can fetch the key-value pair as
          KeyValuePair objKeyValuePair = (KeyValuePair)comboBox1.SelectedItem;
string strkey = objKeyValuePair.m_objKey.ToString();
string strvalue = objKeyValuePair.m_strValue.ToString();
MessageBox.Show("Key: " + strkey + " || Value: " + strvalue);
That’s it. Put the code written in step 3 on some button click event, say button1Click_event. Now, whenever you will click the button, a message box will come that show you the Key and Value of the item which is currenlt selected in the combo box.

If you find any kind of issue/query, please do let me know.


Haapy coding
-Rohit

Wednesday, April 15, 2009

Computed Column in Sql Server

In this short article, we will discuss how can we alter a computed column:

If you have columns in your sql database tables such that their values depend on other columns, you may use computed columns. Using computed columns or calculated columns will enable you to save the calculation logic of such computed values in database and will save you from extra coding everytime you have to do when you require the computed item.

A computed column can be defined as an expression which can use other columns in the same table as well as using other table values via user-defined functions.

Suppose we have the table schema as:

Create Table Example

(

Col1 Decimal(10, 2),

Col2 Decimal(10, 2),

Col3 Decimal(10, 2),

Col4 Decimal(10, 2),

Col5 Decimal(10, 2),

Col6 Decimal(10, 2),

Col7 Decimal(10, 2),

ComputedCol1 As (Col1 + Col2 - Col3),

ComputedCol2 As (Col4 + Col5 - Col6)

)


Now if you want to alter the computed column (say, ComputedCol1), you can not do it direclty by using ALTER command. Instead, you have to drop the column first and then add the column again with new computed value, as:

--Drop the column First

Alter Table Example

Drop Column ComputedCol2

--Now add the column again with new computed value

Alter Table Example

Add [ComputedCol2] As (Col5 + Col6 - Col7) 

You can add a computed column where a CASE expression is used in the definition of the computed column value as,

Alter Table Example

Add [ComputedCol3] As (Case When Col7 = 0 Then (Col1 + Col2 - Col3) Else (Col4 + Col5 - Col6) End) 

However, you can not use a computed column as a condition expression in the definition of other computed column as,

Alter Table Example

Add [ComputedCol4] As (Case When ComputedCol1 = 0 Then 0 Else (Col5 + Col6 - Col7) End) 

/*

--Sql server will throw the following error:

Msg 1759, Level 16, State 0, Line 1

Computed column 'ComputedCol1' in table 'Example' is not allowed to be used in another computed-column definition.

*/

So to resolve this use the computed column's calculated values as condition expression as,

Alter Table Example

Add [ComputedCol4] As (Case When (Col1 + Col2 - Col3) = 0 Then 0 Else (Col5 + Col6 - Col7) End) 

Regards
Rohit