Thursday, December 10, 2009

Building own ORM tool using .net Reflection

All professional .net programmer know about System.Reflection. Through it, you can extract meta information of a class or assembly. Using reflection testing tools, ORM tools etc. can be developed.

Here is a sample how you can develop your own ORM tool using .net Reflectin. It is similar to LINQ and obviously not at all so powerful like LINQ. But it has the superiority over the LINQ in the sense that it can run on any Database. Suppose you have a class Student which has properties like public long Student_Id, public string Student_Name. Like LINQ you must have a table Table Students which has columns student_id number, student_name varchar2(256).

So, if you want to get the list of Student from Database to application you need to use C# generic. Steps are as follows -

1. Use typeof() to get object type.

2. List it properties from the Type object using GetProperties() of type object.

3. Generate sql query - class name maps the table name and property names map the column names.

4. Get the data from database.

5. Invoke set method of the object to set property to the database column values. Example code can be like this -
MethodInfo method = _methodList.FirstOrDefault(m => m.Name == "set_" + property.Name);
method.Invoke(typedObject, paramObjects);
6. Return the list.

This is the high-level algorithm of this simple ORM.

Monday, October 12, 2009

Data validation using awk command

One common use of patterns in awk is for simple data validation task. Specially in ETL process, you can use it for source data cleansing.

In shell scripting, usually =~ operator is used for regular expression matching. There is a nice guide for advance shell script. Point 17 describes regular expression.

But in awk command ~ is used for matching. Suppose we want to filter a file which contains line delimited by "|" and print only those line whose second column contains "*8999*". So the awk command will be -

$ awk -F '|' 'if(($2 ~ "\*8999\*")){print NR,"|",$0;}}' $file

Thus, you can validate data using awk command. This is one of the great feature of awk command.

Saturday, September 12, 2009

Reset password in oracle

At first log in the server where oracle is installed using administrative account of the OS. Then set the ORACLE_SID user variable to the name of database service. So, if your database service name is orcl, then set the variable ORACLE_SID to value orcl. Then -

Log in sqlplus from command prompt of the server using the following command-

sqlplus "/as sysdba"

now you are connected.

Then you can change sys password by the following command-

alter user sys identified by *******;


Thus your password is reset. Now you can connect by any client s/w of oracle.

Sunday, August 30, 2009

protocol configuration of .asmx webservice

The default protocol configuration of .asmx webservice is -
<webServices>
<protocols>
<clear />
<add name="HttpSoap12" />
<add name="HttpSoap" />
<add name="HttpPostLocalhost" />
<add name="Documentation" />
</protocols>
</webServices>

So it supports data & payload in soap envelope and documentation for all; but supports post only for localhost. Thus if we want to test a webservice using post method, we can test it only from localhost.

If we want to allow to invoke the webservice via get & post method we have to add the following under the system.web node -

<webServices>
<protocols>
<clear />
<add name="HttpSoap12" />
<add name="HttpSoap" />
<add name="HttpPostLocalhost" />
<add name="Documentation" />
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>

Suppose you want to invoke a webmethod(MethodName which takes a parameter param) of a webservice(SomeService.asmx) using get method, then you need to hit the following url-
http://localhost/WebXmlHttpDemo/SomeService.asmx/MethodName?param=value

Wednesday, July 15, 2009

batch update on a table from another table

CREATE TABLE A
(
NAME VARCHAR2(20 BYTE),
AGE INTEGER
)

CREATE TABLE B
(
NAME VARCHAR2(20 BYTE),
AGE INTEGER
)

Suppose we need to update the age of table B with the age of table A.

For oracle the command will be -

UPDATE tmp_B b
SET age = (SELECT a.age FROM tmp_A a WHERE a.name = b.name)
where b.name in (select a.name from tmp_A a);

Oracle also support n-ordered tuple. So if you want to update all row in a single run, the following command is also valid -

UPDATE tmp_B b
SET (age,name) = (SELECT a.age, a.name FROM tmp_A a WHERE a.name = b.name);

For MS Sql Server the command will be-

update tmp_B
set tmp_B.age=tmp_A.age
from tmp_A,tmp_B where tmp_A.name=tmp_B.name;

Tuesday, June 30, 2009

Consume webservice From JavaScript

1. Add an asp:ScriptManager in form of the aspx page.

2. Add the service that you want to access in the script manager tag.

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/ZipCodeService.asmx" />
</Services>
</asp:ScriptManager>

here ZipCodeService.asmx is the path of the web service. In this case the web service resides in the same project. If this does not reside in the same project then add web reference.

3. Ensure that the web service added can be invoked from script. In case of .net, this is ensured by adding the following attribute to the class of the web service.

In ZipCodeService, this is ensured by -

[System.Web.Script.Services.ScriptService]
public class ZipCodeService : System.Web.Services.WebService
{
. . .
}

4. Now this web service is eligible to call from JavaScript. So call the webmethod of web service with full namespace name.

For detail please visit here

Sunday, February 8, 2009

Add extension to IIS to serve .sadique extension file

A very good & detail article for this will be found at here.

It describes how to http handlers & modules work.

For busy readers here is the summary of how we have to configure our application to handle .sadique extension file-

1) Create a new C# Class Library project in Visual Studio.NET and name it "MyHandler". Add NewHandler.cs class to this project which implements IHttpHandler by implementing method- public void ProcessRequest(System.Web.HttpContext context).

2) Register this handler by adding httphandlers in the web.config file:

<httpHandlers>
<add verb="*" path="*.sadique" type="MyHandler.NewHandler,MyHandler">
</add>
</httphandlers>

3) Launch the Internet Services Manager tool, right click on Web Site, select Properties, go to Home Directory tab and press Configuration button. This will popup Application Configuration dialog. Click Add button and fill the Executable field with the path to the aspnet_isapi.dll file and fill .sadique in the Extension field.