Sunday, June 28, 2009

Geographic information of the User on your page!!!

Wanted to show the geographical information of the visitor on homepage of your site. Here is a great API to your rescue
http://ipinfodb.com/ip_query2.php?ip=(YourMachinesIP)
I have written this sample code for using this API for your reference have a look :
protected void Page_Load(object sender, System.EventArgs e)
{
string userip;
string url;
userip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (userip== null)
{
userip = Request.ServerVariables["REMOTE_ADDR"];
}
url = String.Format("
http://ipinfodb.com/ip_query2.php?ip=%7B0%7D", userip);
WebClient wc = new WebClient();
Stream data = wc.OpenRead(url);
String str;
using (StreamReader sr = new StreamReader(data))
{
str = sr.ReadToEnd();
data.Close();
}
//Use data is str variable to display on web page….
}

Happy coding!!!

Vinod

Happy coding!!!!!

Tech Tip:Tostring() vs Convert.Tostring()

We normally use the .Tostring() or Convert.Tostring() functions in day to day coding. Although the function of two methods are same but It’s important to understand the behavior of the methods in determining which to use.

When using the Convert.ToString() on a string value it simply returns the value, the ToString() on a sting returns this (a reference to the string itself).
When called on an object, the Convert.ToString() will try to convert it an IConvertible interface first, if this fails and the object is not null it will call the ToString on the object. If the object reference is null it returns an empty string.

The default behavior of the ToString method on an object is to return the name of the type (this.GetType().ToString()) . Calling the ToString() method directly has less overhead but you need to perform a null test prior to calling any methods on the object.

So, it’s clearly advantageous to use the Convert.Tostring() instead of ToString() as it will eliminate need to check for null prior to conversion.


Happy coding !!!!
Vinod

SCOPE_IDENTITY() vs. @@IDENTITY

As we all know the use of @@IDENTITY to retrieve the identity of the most recently added row in a table. Although this is one of those common practices in SQL Server 7, which does not have a SCOPE_IDENTITY() function, it is no longer the accepted way to get the identity of the most recently added row in a table in SQL Server 2000 and SQL Server 2005.
@@IDENTITY returns the most recently created identity for your current connection, not necessarily the identity for the recently added row in a table. You could have a situation where there is a trigger that inserts a new record in a Logs Table, for example, when your Stored Procedure or INSERT SQL Statement inserts a record in the Orders Table. If you use @@IDENTITY to retrieve the identity of the new order, you will actually get the identity of the record added into the Log Table and not the Orders Table, which will create a nasty, nasty bug in your data access layer.

To avoid the potential problems associated with someone adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your INSERT SQL Statement or Stored Procedure.

Happy Coding!!!!

Friday, January 30, 2009

What are Design Patterns?

Sitting at your desk in front of your workstation, you stare into space, trying to figure out how to write a new program feature. You know intuitively what must be done, what data and what objects come into play, but you have this underlying feeling that there is a more elegant and general way to write this program. In fact, you probably don’t write any code until you can build a picture in your mind of what the code does and how the pieces of the code interact. The more that you can picture this “organic whole,” or gestalt, the more likely you are to feel comfortable that you have developed the best solution to the problem. If you don’t grasp this whole right away, you may keep staring out the window for a time, even though the basic solution to the problem is quite obvious. In one sense you feel that the more elegant solution will be more reusable and more maintainable, but even if you are the sole likely programmer, you feel reassured once you have designed a solution that is relatively elegant and that doesn’t expose too many internal inelegancies. One of the main reasons that computer science researchers began to recognize design patterns is to satisfy this need for elegant, but simple, reusable solutions. The term “design patterns” sounds a bit formal to the uninitiated and can be somewhat offputting when you first encounter it. But, in fact, design patterns are just convenient ways of reusing objectoriented code between projects and between programmers. The idea behind design patterns is simple-write down and catalog common interactions between objects that programmers have frequently found useful.

The pattern concept

As mentioned above Design Patterns can be described as suggestions for object-oriented software design. Patterns are not trivial and they are not represented by features that are built into the language as inheritance for example. A pattern solves a certain type of problem in a specific context and gives a concept for the collaboration of objects, classes and methods. Although it is intended to support the programmers work it is more a layer of abstraction and meets in this point the basic concept of program design. The most compelling motivation behind this is to separate things that change from things that stay the same. Following this principle reduces the effort to make modifications on parts that could actually be unchanged - which causes lower costs and makes programs easier to understand. One of the main features of Design Patterns is that each pattern has a known name. The consistent use of these names and the patterns belonging to them increases the ability of each programmer in his work and also helps to communicate with his colleagues by the use of a common vocabulary. Terms like Factory, Iterator or Singleton are used quite often in software projects and should have the same meaning for all project members.

The authors of Design Patterns – Elements of Reusable Software divide patterns into three types: creational, structural and behavioural.

Creational Patterns give control over instantiation of objects. Using this type of pattern the programmer doesn’t need to instantiate objects directly and gives the program the ability to decide, which objects need to be created for a given case. Patterns belonging to this type are The Factory Pattern, The Abstract Factory Pattern, The Singleton Pattern, The Builder pattern and The Prototype Pattern.

Structural Patterns help to compose groups of objects into larger structures, such as complex user interfaces or accounting data. Examples for Structural Patterns are The Adapter Pattern, The Brdige Pattern, The Composite Pattern, The Decorator Pattern, The Façade Pattern, The Flyweight Pattern and The Proxy Pattern.

Behavioural Patterns help to define the communication between objects in the system and how the flow is controlled in a complex system. Known Behavioral Patterns are Chain of Responsibilty, The Command Pattern, The Interpreter Pattern, The Iterator Pattern, The Mediator Pattern, The Memento Pattern, The Observer Pattern, The State Pattern, The Strategy Pattern, The Template Pattern and The Visitor Pattern.

This post provide just an introduction to the world of design pattern.Stay tuned for more.

Saturday, January 10, 2009

Code Search Engines!!!

Searching for help is the human tendency, so is true for Developers (ofcourse, Developers are also humans:)).But as they say searching is an art in itself so far that the good developers are those who mastered the art of searching on Google or any other search engine. But most of you will agree with me that it is often quite difficult or rather cumbersome to search the right code snippet that will save that at least half an hour for that coffee break :). So, what about dedicated code search engines? sounds interesting, so here we have the complete list of the code search engines that I found out,ofcourse by searching...

codase.com

bytemycode.com

csourcesearch.net

google codesearch

freshmeat.net

gotapi.com

krugle.org

merobase.com

oreilly.com

microsoft.com

quickref.org

sourceforge.net

codeproject.com

Happy Coding!!!

Wednesday, January 7, 2009

Create Nested Subfolders in VBScript

Here is the simple vbscript to create nested subfolders depending upon the number entered by the user:


Dim fso,flr,resp,subFoldernum,nestedFoldernum,stuffTowrite,dateStamp,folderPath
dateStamp = Date()Const ForWriting = 8
stuffTowrite = "Whatever you want written " & dateStamp
Set fso = CreateObject("Scripting.FileSystemObject")
resp = InputBox("Enter Folder Name","Create Folder")
subFoldernum = InputBox("Enter Number of Subfolders","Create SubFolder")
If IsNumeric(subFoldernum) then
nestedFoldernum = InputBox("Enter Number of folders you want to create in the Subfolders","Create Nested SubFolders")
If IsNumeric(nestedFoldernum) then
Set Fldr=fso.createfolder(resp)
For x = 1 to subFoldernum
Set Fldr=fso.createfolder(resp & "\SubFolder"&x)
For y = 1 to nestedFoldernum
folderPath=resp& "\SubFolder"&x&"\nested"&y
Set Fldr=fso.createfolder(folderPath)
Set WriteStuff = fso.OpenTextFile(folderPath & "\test.txt", ForWriting,true)
WriteStuff.WriteLine(stuffTowrite)
WriteStuff.Close
Next
Next
else
msgbox("Whatever you submitted,is not a numeric value.")
end if
else
msgbox("Whatever you submitted,is not a numeric value.")
end if