Monday, March 29, 2010

Debugging Script Component


Last week, I face one problem in SSIS package. I used Script component and it fails in my application. I tried to debug my code but I am not able to debug it. I dig this problem and found that SSIS package not support break points in script component.

Scenario:
I created one data flow task in my SSIS package. This data flow task contains one script component with other component. This script component is used to split one string as per my logic. This script component fails for some defined cases. So I want to debug the code but BIDS (Business Intelligence Development Studio) is not supporting breakpoints in Script Component.

I found this information from Microsoft website. You can get more detail from http://msdn.microsoft.com/en-us/library/ms136033.aspx

You can use following alternate way:
  • Use Message Boxes to print variable values. So you can check the conditions and your logic according to that value.
  • User logging. Log every variable and condition value into file. You can get idea from that log file.

Wednesday, March 24, 2010

Invoke method using reflection


We have find out methods of any class using reflection but sometime we need to execute it using reflection.

For example, it might required that you have one logic for calculating pricing information which change time to time then you have to use reflection to calculate price. You can create one assembly to calculate pricing information. Whenever you need to change logic, you have to put new assembly on the same path with new logic.

Before calling any method you have to declare object of that class. You can use Activator class to create object runtime.


object objClass1 = Activator.CreateInstance(type);


The above code will create Class1 object using Activator class.

After loading class object successfully, you have to find out method from that type and invoke it. Following source code helps you to invoke method.


Assembly assembly = Assembly.Load("TestLibrary1");
Type t = assembly.GetType("TestLibrary1.Class1");
MethodInfo method = t.GetMethod("GetSum");
object objClass1 = Activator.CreateInstance(t);
object[] objParameters = new object[2];
objParameters[0] = 10;
objParameters[1] = 11;
MessageBox.Show(method.Invoke(objClass1, objParameters).ToString());


Here we load assembly and find out the class type using “GetType” method of assembly class. After getting class type, we have created class object dynamically using Activator class. Then we find out method which we need to invoke and also create parameter array to pass into method. After creating parameter array, we need to call “Invoke” method of MethodInfo class to invoke the method. Return value of Invoke method is the same which that method returns.

You can download source code from here.

Get Class Members using Reflection


You can find basic information of Reflection from What is Reflection.

You can get members of any class using reflection. Let’s start with constructor.

You can retrieve all constructors using “GetConstructors” method which is available in Type class object. You can get Type class object from loading assembly and find Type using “GetType” method. For loading assembly and find type please click here.

Following code snippet is used to get all constructors from any class.

private void GetConstructors(Type type)
{
ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.CreateInstance | BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
foreach (ConstructorInfo constructor in constructors)
{
string constructorString = "";

if (constructor.IsStatic)
constructorString = "static new(";
else if (constructor.IsPrivate)
constructorString = "private new(";
else if (constructor.IsPublic)
constructorString = "public new(";

ParameterInfo[] parameters = constructor.GetParameters();
foreach (ParameterInfo parameter in parameters)
{
constructorString += parameter.ParameterType.Name + " " + parameter.Name + ", ";
}
if(constructorString.EndsWith(", "))
constructorString = constructorString.Substring(0, constructorString.Length - 2);
constructorString += ")";
listBox2.Items.Add(constructorString);
}
}


Here I have not handling error. You can optimize code. I have just draft the code for demo purpose.

We need to call “GetConstructors” method of Type class object which returns ConstructorInfo class array. You can iterate that array and find out constructor information like parameters.

Same way you can find out variables which are declared into class. Following code snippet is used for that:


private void GetVariables(Type type)
{
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
foreach (FieldInfo field in fields)
{
listBox3.Items.Add(field.FieldType.Name + " " + field.Name);
}
}


GetFields class returns all the available variables declared into that type. Here we mentioned Binding Flags which are used to find out type of variable. If you want to find out only private variables then you have to specify “Instance” and “Private” flag only. Here I need to find out Public, Private, Protected and static variables so I have mentioned all required flags.

GetMethods method used to find out list of methods into type. Following code will help you to find out available methods:


private void GetMethods(Type type)
{
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
string methodString = "";

if (method.IsStatic)
methodString = "static " + method.Name + "(";
else if (method.IsPrivate)
methodString = "private " + method.Name + "(";
else if (method.IsPublic)
methodString = "public " + method.Name + "(";

ParameterInfo[] parameters = method.GetParameters();
foreach (ParameterInfo parameter in parameters)
{
methodString += parameter.ParameterType.Name + " " + parameter.Name + ", ";
}
if (methodString.EndsWith(", "))
methodString = methodString.Substring(0, methodString.Length - 2);
methodString += ")";
listBox4.Items.Add(methodString);
}
}


You can also download source code from here.

What is Reflection


Reflection means to find out metadata information of any modules or assembly. You can also create class using reflection.

Developer can load any assembly runtime and find out metadata information of that assembly. He can also create object of any type which are exist in that assembly and also able to assign value and call any method from that object at runtime.

Let’s take an example, you have to use any third party control (DLL). You can get your requested type dynamically.

Here I explained you how to load any assembly which is added as a reference in your project.

You are getting one Test Library1 (TestLibrary1.dll) and you have to find out “Class1” type. You have to add reference of that class library and call this code snippet to load library:


Assembly assembly = Assembly.Load("TestLibrary1");
Type t = assembly.GetType("TestLibrary1.Class1");


Here we have pass “TestLibrary1” as a parameter of Assembly.Load method is an assembly name. This is same as a namespace name.

Assembly is in-built .net class which has static method “Load” which load assembly so developer can easily find out types.

After loading assembly, you can find out your type using GetType method of assembly object.

Load method of Assembly class has different overloads. You can also pass physical path or Assembly name or byte array of assembly name.

You can download code from here.

Monday, March 15, 2010

First Data Global Gateway API Integration with VB.Net


Introduction


Many payment gateways are available in the market to pay for any product online. Below are some online payment gateways:
  • Your Pay

  • PayPal

  • Authorize.Net

  • First Data Global Gateway


Let’s talk about First Data Global Gateway. First Data Global Gateway gives two ways to integrate payment gateway:
  • Using LinkPoint DLL

  • Using Web service


Here I explained how to integrate using LinkPoint DLL.

Before starting integration you need following things to integrate First Data Global gateway with your application:
  • PEM file

  • Merchant Account Number

  • LinkPointTransaction.dll and lpssl.dll (Ask to provide from First Data)


Download application from here.

Above application is .Net class library project. You have to take reference of class library. Now you are ready to use the Global Data payment gateway with your application. You have to pass following parameters:
  • strType – Type of the payment, It may hold either “Good”, “Decline” or “Duplicate”.

  • strMerchantNumber – You will get this from First Data when you will open your account with them

  • dblAmount – Amount of the transaction

  • strZip – Zip code of the customer who used his/her credit card to do payment

  • strAddNum – Address of the customer

  • strCC – Credit card number

  • strcardexpmonth – Expiry month of credit card

  • strcardexpyear – Expiry year of credit card

  • strKeyFile – PEM file path (full path). You will get PEM file from First Data when you will open account with them

  • strHost – It may hold either “staging.linkpt.net” or “secure.linkpt.net”. For testing purpose you have to pass “staging.linkpt.net”. On server, you have to pass “secure.linkpt.net”.

  • intPort – Port number to connect above host. For now you have to pass “1129”.


Sunday, March 14, 2010

What is fill factor


When you create clustered index, SQL server will create index pages to store the data in order to clustered index. When user will insert new record or update clustered index column data then SQL server will change its index pages data. For example if you will insert new data into table, SQL server will shift its data from one data page to another to make sufficient space to insert new record in correct place. It will decrease your query performance. This also implies with non-clustered index.

Fill factor is used at the time of creating index. Fill factor is percentage value from 0 to 100. Fill factor option will add some spaces in data pages so when user will add new record it will easily manageable for SQL server to insert record in any data page.

Fill factor value 100 means there is no space required in data pages. When your table is read only or you are sure about there is no change in your table in future then you can provide 0 as a fill factor.

Lower fill factor value leaves more spaces in data pages. So whenever user will add new record it will easily manageable for SQL server to make sufficient space to add the record in particular order. It will increase speed of your query.

Fill factor imply only when you will create index. It is not maintained after records are added, updated or deleted.

Wednesday, March 10, 2010

asp.net page life cycle


When user requests any asp.net page, the page goes to asp.net page life cycle in
which it perform a series of processing state. ASP.Net page will execute some methods
in sequence in its life cycle. All those methods are explained below:


Method Name

Description

Pre Initialization

During page initialization, controls on the page are available and Unique id property is set. Theme is also applied in this method. If page is post back then posted data is not loaded in this method and also controls state is not restored from the view state.

Load

During load, if page is post back then controls is restored from the view state or control state

Validation

During validation, the validate method of all the validation control is executed which set IsValid property of validation control

Post event handling

If page is post back then handle event handling

Rendering

Before rendering, view state data are saved.

Unload

Unload is called after page is successfully rendered, sent to the client and is ready to dispose. At this point Response and Request object are unloaded and any cleanup is performed.

what is multicast delegate?


What is multicast delegate?

Multicast delegate is delegate which holds more than one method reference. When you call delegate, it will execute all the methods which are associated with that delegate.

Sometime we assigned two functions to one delegate it calls multicast delegate. Just take an example, if you have one delegate to transfer fund from one account number to another account. You also want to log every entry into your log system so you can solve any customer query if any.

We have one delegate which is used to transfer funds from one account to another account. Delegate syntax is displayed as below:

delegate void TransferFund(string FromAccountNumber, string ToAccountNumber, decimal Amount);

Now in your class, you can give more than one method reference to this delegate. Code for this is as below:

private void Form1_Load(object sender, EventArgs e)
{
TransferFund transferFundObject = new TransferFund(DoTransferFund);
transferFundObject += new TransferFund(DoTransferFundLog);
transferFundObject("From_Account_No", "To_Account_No", 1000);

}

void DoTransferFund(string FromAccountNumber, string ToAccountNumber, decimal Amount)
{

}

void DoTransferFundLog(string FromAccountNumber, string ToAccountNumber, decimal Amount)
{

}

In above code, In Form load event, I have registered two methods with one delegate. When we call the delegate it will execute both methods one by one.

DotNet Code Guru