Monday, August 27, 2007

Add filter to Open File Dialog

fdlgOpenAttachments = new OpenFileDialog();

fdlgOpenAttachments.Filter = "All Files (*.*)*.*Text files (*.txt)*.txtWord Files (*.doc)*.docExcel Files (*.xls)*.xlsPDF Files (*.pdf)*.pdfImage Files(*.jpg,*.gif)*.jpg;*.gif";

Thursday, July 26, 2007

Caching in window applications

///
/// This is the object to use to cache low volitality data
///

///
///
///
///
public sealed class ApplicationCache
{
///
/// Empty private constructor
/// Static holder types should not have constructors
///

///
private ApplicationCache()
{
}
///
/// Use this to add to the cache
///

/// This is the key for the value to add to the cache
/// This is the value to add to the cache
public static void Add(string key, object value)
{
try
{
CacheManager myManager = CacheFactory.GetCacheManager();
if (!myManager.Contains(key))
myManager.Add(key, value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Use this to retreive from the cache
///

/// This is the key for the value to retrieve from the cache
/// Returns an object, developer must cast this properly!
public static object GetData(string key)
{
try
{
CacheManager myManager = CacheFactory.GetCacheManager();
return myManager.GetData(key); ;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Use this to remove items from the cache
///

/// This is the key for the value to remove from the cache
/// Returns nothing
public static void RemoveData(string key)
{
try
{
CacheManager myManager = CacheFactory.GetCacheManager();
myManager.Remove(key);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Use this to query from the cache for a key/value pair
///

/// This is the key for the value to retrieve from the cache
/// Returns a boolean
public static bool Contains(string key)
{
try
{
CacheManager myManager = CacheFactory.GetCacheManager();
return myManager.Contains(key);
}
catch (Exception ex)
{
throw ex;
}
}
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



if (!ApplicationCache.Contains("User_Departments"))
{

// Go to databse and get the object.
ApplicationCache.Add("User_Departments", _departments);
}
return (DepartmentList)ApplicationCache.GetData("User_Departments");


Tuesday, July 17, 2007

Select Condition 1

CREATE TABLE #Temp (ids INT)

INSERT INTO #Temp VALUES (1)
INSERT INTO #Temp VALUES (2)
INSERT INTO #Temp VALUES (3)
INSERT INTO #Temp VALUES (4)
INSERT INTO #Temp VALUES (5)

Condition :
If I pass parameter as -1 it should return records having ids 1 or 3.
If I pass parameter as 5 it should return only records having ids 5.

DECLARE @a INT
--SET @a = -1
SET @a = 5

SELECT * FROM #Temp WHERE ((ids = 1 OR ids = 3) AND @a = -1) OR ids = @a

Friday, July 13, 2007

Interface Example1

interface ISample1
{
int sum();
}
interface ISample2
{
int sum();
}
public class A : ISample1, ISample2
{
// There should be no access specifier (it will give build error).
int ISample1.sum()
{
return 10;
}
// There should be no access specifier (it will give build error).
int ISample2.sum()
{
return 20;
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
ISample1 is1 = (ISample1)a;
Console.WriteLine(is1.sum().ToString());
ISample2 is2 = (ISample2)a;
Console.WriteLine(is2.sum().ToString());
}
}

Thursday, July 12, 2007

Polymorphism Example 5

public class A
{
public virtual void Print(int count)
{
Console.WriteLine(count.ToString() + " We are in A");
}
}
public class B : A
{
public override void Print(int count)
{
Console.WriteLine(count.ToString() + " We are in B");
}
public void Print(double count)
{
Console.WriteLine(count.ToString() + " Hi Kumar.");
}
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.Print(5); // 5 Hi kumar
((A)b).Print(5); // 5 We are in B
Console.ReadKey();
A a = new A();
a.Print(5); // 5 We are in A
Console.ReadKey();
A aa = new B();
aa.Print(5); // 5 We are in B
Console.ReadKey();
}
}

Polymorphism Example 4

class A
{
public virtual void Print()
{
Console.WriteLine("We are in Class A.");
}
}
class C : A
{
public new void Print()
{
Console.WriteLine("We are in Class C.");
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a.Print();
Console.ReadKey();
C c = new C();
c.Print();
Console.ReadKey();
A aa = new C();
aa.Print();
Console.ReadKey();
A aaa = (A)c;
aaa.Print();
Console.ReadKey();
//C ccc = (C)a;
//ccc.Print();
//Console.ReadKey();
}
}

Polymorphism Example 3

public class A
{
public void PrintA()
{
Console.WriteLine("We are in class A");
}
}
public class B : A
{
public void PrintB()
{
Console.WriteLine("We are in class B");
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a.writeA();
Console.ReadKey();
// Following code will give RUNTIME error.
B b = (B)a;
b.writeA();
b.writeB();
Console.ReadKey();
////////////////////////////////////////////
B bb = new B();
bb.writeA();
bb.writeB();
Console.ReadKey();
A aa = (A)bb;
aa.writeA();
Console.ReadKey();
}
}