Finding Lead records that have an associated activity with them


Here in the query one more condition is added which is when the Rating is Hot. 

select leadqualitycodename,* from filteredlead As fl inner join dbo.FilteredActivityParty as fap
ON fl.leadid=fap.partyid
and fl.leadid=‘yourleadid’ and leadqualitycodename=‘Hot’ 

Events in C#


We’ll take a simple and useful scenario to make our understanding of Events as clear as possible.
First of all copy and paste the code below. We’ll try understanding the code than.

using System;
public class ResultDeclarationEventArgs : EventArgs
{
private string message;
public ResultDeclarationEventArgs(string message)
{
this.message = message;
}
public string Messsage
{
get
{
return message;
}
}
}
public delegate void ResultHandler(object sender, ResultDeclarationEventArgs e);
public class Kid
{
private int percentage;
public event ResultHandler onResultDeclaration;
public int Percentage
{
set
{
percentage = value;
if(percentage > 80)
{
ResultDeclarationEventArgs myREA = new ResultDeclarationEventArgs( ” I got Distinction”);
onResultDeclaration(this, myREA);
}
if(percentage > 60 && percentage < 80)
{
ResultDeclarationEventArgs myREA = new ResultDeclarationEventArgs(” I got FirstClass”);
onResultDeclaration(this, myREA);
}
if(percentage > 40 && percentage < 60)
{
ResultDeclarationEventArgs myREA = new ResultDeclarationEventArgs(” I got SecondClass”);
onResultDeclaration(this, myREA);
}
if(percentage < 40)
{
ResultDeclarationEventArgs myREA = new ResultDeclarationEventArgs(” Sorry I failed “);
onResultDeclaration(this, myREA);
}
}
}
}
public class Mom
{
public Mom(Kid myKid)
{
myKid.onResultDeclaration += new ResultHandler(GiveMsgToMom);
}
void GiveMsgToMom(object sender, ResultDeclarationEventArgs e)
{
Console.WriteLine(e.Messsage);
}
}

public class Dad
{
public Dad(Kid myKid)
{
myKid.onResultDeclaration += new ResultHandler(GiveMsgToDad);
}
void GiveMsgToDad(object sender, ResultDeclarationEventArgs e)
{
Console.WriteLine(e.Messsage);
}
}

class myDemo
{
public static void Main()
{
Kid kid = new Kid();
Mom mom = new Mom(kid);
Dad dad = new Dad(kid);
Console.WriteLine(“Please enter the percentage of the Kid”);
string percentage = Console.ReadLine();
kid.Percentage = Convert.ToInt32(percentage);
}
}

The scenario is something like this First there is a class named Kid. It has a field called Percentage and declares an event OnResultDeclaration. It is of type ResultHandler. ResultHandler is a delegate. public delegate void ResultHandler(object sender, ResultDeclarationEventArgs e);
It can point or refer to any method which has return type void and takes as parameter an object and ResultDeclarationEventArgs.
Object will give the name of the sender who has called this eventhandler and ResultDeclarationEventArgs is to pass any information regarding the event.

ResultDeclarationEventArgs is a class that derives from EventArgs class. It has a message field which will contain the message the Kid class, which has the event OnResultDeclaration, would like to convey to the class (e.g. Mom, Dad) which are interested in that event.
For this reason, we have our event handlers(method) inside Mom and Dad class which have the same signature as defined by the event.
void GiveMsgToMom(object sender, ResultDeclarationEventArgs e)
void GiveMsgToDad(object sender, ResultDeclarationEventArgs e)
Than we register these eventHandler to the events.
myKid.onResultDeclaration += new ResultHandler(GiveMsgToDad);
myKid.onResultDeclaration += new ResultHandler(GiveMsgToDad);

Kid class will check for the percentage passed in and accordingly set’s the message.
Whatever message is set is than displayed in the Event handler inside Mom and Dad class using e.Message

Understading Delegates


What are delegates?
Delegates are object that refer to an method. Normally we refer to objects, however referring to an object isn’t any different from referring a method they too have physical location in memory.

Why use delegate?
One delegate can be used to call different methods during runtime of a program by simply changing the method to which the delegate refers.
and Delegates Support Events.

delegate ret-type name(paramerter-list);
e.g. delegate string MyDelegate();

The MyDelegate can call any method whose return type is string and accepts no parameter. It can be instance method or a static method.

delegate string MyDelegate(String s);
class Program
{
static string GetNameLower(String s)
{
return s.ToLower() ;
}
static string GetNameUpper(string s)
{
return s.ToUpper();
}
static void Main(string[] args)
{
MyDelegate myD = new MyDelegate(GetNameLower); //or myD=GetNameLower
string s1 = myD(“Hi Nishant”);
Console.WriteLine(s1);
myD = new MyDelegate(GetNameUpper); //or myD=GetNameUpper
string s2 = myD(“Hi Nishant”);
Console.WriteLine(s2);
}
}

Understanding Multicasting

We can have chain of methods that will be called automatically when a delegate is invoked.
For this we will use += operator to add methods to chain and -= to remvove a method.
If delegate returns value than value returned by the last method becomes the return value of entire deleagation invocation. Thus a delegate making use of multicasting will have void as return type.

delegate void MyDelegate();
class Program
{
static void GetNameLower()
{
Console.WriteLine(“GetNameLower Called”);
}
static void GetNameUpper()
{
Console.WriteLine(“GetNameUpper Called”);
}
static void Main(string[] args)
{
MyDelegate myD = GetNameLower;
myD +=GetNameUpper;
myD(); //invoking the delegate
}

Understanding Static keyword in C#


Static keyword can be applied to
Class, field, method, properties, operator, event and constructors.
Static member belongs to the class and not to any object of the class.
They can be used without creating the instance of the class.

For e.g. Static Void Main()
It is called by the operating system on program execution
To access the static member we’ll use

ClassName.staticmember

When a variable is declared as static internally what happens is that all the instances of the class share the same static variable. A static variable is initialized when its class is loaded. If not
initialized explicitly then
it is initialized to zero for numeric variable
null in case of object references
false for boolean

StaticMethod they can only contain static member and call other static method.
If we need to access them than it can be done through the object of that class

class Game
{
string getGameName()
{
……………….
}
public static void getNameThroughStatic(Game g)
{
g.getGameName(); // accessing static method
}
}

When to use them?
Well we can use them when we need to maintain information applicable to the entire class

suppose we have a class Employees there we can have a static count variable to keep track of no of employees.

class Employees
{
static in count=0;
public Employees()
{
count++;
}

~Employees
{
count–;
}

}

What are Static Constructor?
They can be used to initialize the static variables.
They are called automatically and before the instance constructor (if called any).

for above class
static Employees() // no other access modifiers for them
{}

What are static Classes?
A class whose objects can’t be created and which can only have static members. They can’t be inherited as well.
They can have static constructor

Why use static Classes?
It can be used to group related static method.

How to – Dynamically add rows and columns to Grid View


Hi to add rows and columns to Gridveiw dynamically what
we will do is that

1) Create a DataTable object to which than we will bind the GridView
DataTable dt=new DataTable();

2) IF you need two columns than create two DataColumn object
DataColumn dCol1=new DataColumn(FirstC, typeof(System.String)); // string FirstC=”column1″
DataColumn dCol2=new DataColumn(SecondC, typeof(System.String)); // string SecondC=”column2″

Add it to the table

dt.Columns.Add(dCol1);
dt.Columns.Add(dCol2);

3)Run the loop for as many rows you want to add.

// say you want to add two rows

for(int i=0;i<2;i++)
{
DataRow row1 = dt.NewRow();
row1 [FirstC] = “One”;
row1 [SecondC] =”Two”
dt.Rows.Add(row1 );

}

Now iterate through each datacolumn and create a BoundField foreach column

foreach (DataColumn col in dt.Columns)
{
BoundField bField = new BoundField
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.Columns.Add(bField);
}
GridView1.DataSource = dt;
//Bind the datatable with the GridView.
GridView1.DataBind();

Advertisements

Internal Access Modifier in C#


In C#
The internal access modifier can be applied to
Classes and it’s member
Structure and it’s member
Enumeration
Interface

This way it is available to all the files within that assembly.
Particularly useful when creating software components.

It can be used in conjunction with protected to produce
protected internal access modifier
It can be applied only to
Class members

It is available withing it’s own assembly or to derived types.

For e.g.

Say if we create an Class Library which has a class like

namespace ClassLibrary1
{
public class MyClass

……

And then you create

Now we create new project say a windows application and add a refrence to that dll
And can create the object of that class lik

MyClass my=new MyClass();

this works fine,

now if we modify the definition of the class by replacing public with internal and update the assembly

namespace ClassLibrary1{
internal class MyClass

This time if we try to create the object of MyClass which is inside our referenced assembly
The error which we will get is

MyClass is inaccessible due to its protection level.

That’s why they are basically used in component development so that any other class couldn’t be able to access it i.e. create objects of it.

That’s it