this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Monday, March 29, 2010

Typed V/S Un-Typed

In .Net framework, we many choice to use type safety and also we can use object without specifying its type before it will be used.

Typed means we know type, type can be anything int, string, float or any user defined type object etc.

Un-Typed as name suggest it does not mean absence of type, but uncertainty in types like we don’t have idea which type will come weather its int,float or something else.

In .Net Framework Object is base type of any types. So if we have declared variable as Object and going to assign value then we can assign any values string, int but we don’t know or have control to assign value to Object type variable.

Un-Typed:

Array list, dataset, and many collections are most popular example of un-typed; in array list we can store any value. Also in dataset we have one or many tables; even we don’t have idea which table will be stored in our dataset before we access dataset.

Due to above characteristic, it lacks of type checking, we should be take care when cast to any type.

Typed:

Strongly typed dataset, Generics collection provides a great typed safety.

Strongly Typed Dataset we have dataset designed file, we can drag and drop tables, and we may have relation between them. Whenever we access typed dataset we can access tables and its column by its name we get intelligence menu.

We are sure that we are accessing particular table’s row and its column. We are also sure that value of column should be compatible with database’s type. Otherwise we would get compiler error.

With Generic collection, we can have collection of Dictionary,stringCollection and other collection which can accept only one type of data. Like if we are going to declare Dictionary to store key as integer and value as Person class object then we must follow it. If at the time of coding we do something wrong or use other object compiler will give error.

Summary

As per requirement we should use typed and untyped variable in our application. If we know that we are going to use only numeric datatype or have idea of particular type we should use typed, due to its checking of type safety at compile time, intelligence menu etc.

But if we don’t have any prior information of datatype, we don’t have other choice than un-typed. At the time of Un-Typed, care should be taken when casting to types. We should use try catch block to prevent crash of application.

Friday, March 19, 2010

Simple SQL Operation

Yes in this post you will see many a things of SQL Server which is very small; it may be useful in our day to day life.

Firstly just imagine you want to insert values in table, certainly you go for below approach. Let me create a table for our testing

CREATE TABLE MyTable(TestName VARCHAR(25))

Now for data insert you use ..

INSERT INTO MyTable(TestName)

VALUES ('ONE')

GO

INSERT INTO MyTable(TestName)

VALUES ('TWO')

GO

But you can do achieve same thing with below script where we have used UNION ALL

INSERT INTO MyTable(TestName)

SELECT 'ONE'

UNION ALL

SELECT 'TWO'

UNION ALL

SELECT 'THREE'

UNION ALL

SELECT 'FOUR'

UNION ALL

SELECT 'FIFVE'

UNION ALL

SELECT 'SIX'

GO

Hope this makes sense when we want to insert multiple records in table

Now let’s Go through another game, yes of-course this is some kind of game, you are playing in SQL server.

We want to insert Record to table from another table.

At that time we have two choices ( I am assuming that you have created table from above script and we will use that table in our below example)

1) New table is already created and you want to insert data in it.

CREATE TABLE MyTestTable(TempName VARCHAR(25))

INSERT INTO MyTestTable(TempName)

SELECT TestName FROM MyTable

SELECT * FROM MyTestTable

DROP TABLE MyTestTable

2) You want to create table at the time of insertion operation no table exists before it. In below MyTestTable is not created before but it will be created once we execute this

SELECT TestName

INTO MyTestTable

FROM MyTable

SELECT * FROM MyTestTable

DROP TABLE MyTestTable

Wednesday, March 3, 2010

Regular Expression in .net

Regular expressions were widely used in the UNIX programming but it can be useful in our higher level language.

We mostly ignore use of Regular expression in our coding due to its very confusing structure but it reduces your many lines of code in to a single statement.

There are so many place where use of regular expression is wise compared to other technique.

Below are some of the lists of it.

à Validating user input

à Compare string with some pattern like zip code, phone number, email address etc.

à Extract specific portion from the user input.

à Replace data on user input when some condition satisfied

To use regular expression in .net you need to import System.Text.RegularExpressions name space in your project.

Regx Class provides many static methods for the play with regular expression.

Below line return Boolean value if input is following phone number pattern (555) 555-5555.

Regex.IsMatch(input, "^\(?\d{3}\)?[\s\-]?\d{3}\-?\d{4}$")

Here number can be any from 0-9.

^ Symbol specifies starting of string.

$ Symbol specifies ending of string.

* Symbol indicates zero or more occurrence of character

? Symbol indicates one occurrence

+ Symbol indicates one or more occurrence

Match method of Regx returns object of Match class. You can extract portion from the input string. Suppose you want to extract each individual number from the above phone number then use below code.

Dim m As Match = Regex.Match(s, "^\(?(\d{3})\)?[\s\-]?(\d{3})\-?(\d{4})$")

Return String.Format("({0}) {1}-{2}", m.Groups(1), m.Groups(2), m.Groups(3))

There are so many options you can specifies when comparing string against regular expression like you can ignore case, multi line etc.

Go through following regular expression

\d{5}(\-d{4})?$ For zip code 1211,1111-1212 etc.

^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\." & _

"(com|edu|info|gov|int|abc|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$ For Email

^[0-9][0-9]-[0-9][0-9][0-9][0-9]$ For Phone number 11-1234

^[0-9][0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]$ For 1234 1234

^[0-9][0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9] RT[0-9][0-9][0-9][0-9]$

For 1234-1234 RT123

^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$ For Zip Code A1A 1A1