Discover innovative solutions, best practices, and cutting-edge technologies in enterprise architecture
Saturday, September 19, 2009
Getting The Real IP Of Your Users(Track IP Address)
The Following code will get the IP Address of visitor/ client.
------------------------------------------------------
C# code to track IP Address
------------------------------------------------------
string ipaddress;
// Look for a proxy address first
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
// If there is no proxy, get the standard remote address
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
-----------------------------------------------
VB Code to track IP Address
-----------------------------------------------
' Look for a proxy address first
Dim _ip As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
' If there is no proxy, get the standard remote address
If (_ip = "" Or _ip.ToLower = "unknown") Then _
_ip = Request.ServerVariables("REMOTE_ADDR")
-----------------------------------------------------------
Explaination of above Code
-----------------------------------------------------------
The HTTP_X_FORWARDED_FOR variable is to check that there isn't a non-transparent proxy in the way.When users are behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine. Hence first we need to check HTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address. If there are multiple proxy servers the IP Addresses of all of them are appended to the client machine IP Address.
Hence we need to first check HTTP_X_FORWARDED_FOR and then REMOTE_ADDR.
Note: While running this application on your machine it will show IP Address 127.0.0.1 since your client and server is the same machine. when you will run this application on live server it will display the right information.
------------------------
Happy Programming
Friday, September 18, 2009
Bulk Insert From CSV OR Text File In SQL SERVER 2005
So that i will have give her following store procedure.
Create Proc BulkInsertFromFile
(
@FileName Varchar(500), -- depends on path
@ColumnDelimier Varchar(10), -- delimiter for columns
@RowDelimier Varchar(10)--, -- delimier for Rows
--@Output varchar(50) -- used to specify error or success msg
)
As
DECLARE @doesExist INT
SET NOCOUNT ON
EXEC xp_fileexist @FileName, @doesExist OUTPUT
SET NOCOUNT OFF
-- i m creating Temp Table to demonstrate the example
-- Temp table holds the Results copies from FIle
Create Table #Temp
(
[CategoryId] [int],
[UserId] [int] NULL,
[ParentId] [int] NULL,
[CategoryName] [varchar](500),
[Description] [varchar](1000),
[Active] [bit] NULL,
[ImagePath] [varchar](max),
[CreatedDate] [datetime] NULL,
[DeleteStatus] [bit] NULL,
)
IF @doesExist = 1
BEGIN
Declare @SQL nVarchar(max)
Declare @Param nVarchar(500)
Set @Param='@FileName Varchar(500),@ColumnDelimier Varchar(20),@RowDelimier Varchar(20)'
SET @SQL = 'BULK INSERT #Temp FROM '''+@FileName+'''
WITH
(
FIELDTERMINATOR ='''+@ColumnDelimier+''',
ROWTERMINATOR ='''+@RowDelimier+''',
FIRE_TRIGGERS
)'
print @SQL
EXEC sp_executesql @SQL,@param,@FileName,@ColumnDelimier,@RowDelimier
-- Set @Output='Bulk insert Completed'
END
--Else
--Begin
-- Set @Output='File does not exist'
--End
---- u can also use the output param to specify the error msg
Select * From #Temp
Drop table #Temp
What are the difference between DDL, DML and DCL commands
Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:
* CREATE - to create objects in the database
* ALTER - alters the structure of the database
* DROP - delete objects from the database
* TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
* COMMENT - add comments to the data dictionary
* RENAME - rename an object
DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:
* SELECT - retrieve data from the a database
* INSERT - insert data into a table
* UPDATE - updates existing data within a table
* DELETE - deletes all records from a table, the space for the records remain
* MERGE - UPSERT operation (insert or update)
* CALL - call a PL/SQL or Java subprogram
* EXPLAIN PLAN - explain access path to data
* LOCK TABLE - control concurrency
DCL
Data Control Language (DCL) statements. Some examples:
* GRANT - gives user's access privileges to database
* REVOKE - withdraw access privileges given with the GRANT command
TCL
Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
* COMMIT - save work done
* SAVEPOINT - identify a point in a transaction to which you can later roll back
* ROLLBACK - restore database to original since the last COMMIT
* SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use
Monday, September 14, 2009
How to trace visitor information in ASP.NET?
be easily done in ASP.NET using the Server variables and the Request information available. Various attributes
such as remote host name, IP address, browser type and version etc can be known using the Server variables.
Source Code to trace Visitor:
<%
Response.Write("<b>Name:</b> " + Request.ServerVariables["REMOTE_HOST"] + "<br />");
Response.Write("<b>IP:</b> " + Request.ServerVariables["REMOTE_ADDR"] + "<br />");
Response.Write("<b>User agent:</b> " + Request.ServerVariables["HTTP_USER_AGENT"] + "<br />");
Response.Write("<b>Language:</b> " + Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"] + "<br />");
Response.Write("<b>Browser:</b> " + Request.Browser.Browser + "<br />");
Response.Write("<b>Type:</b> " + Request.Browser.Type + "<br />");
Response.Write("<b>Version:</b> " + Request.Browser.Version + "<br />");
Response.Write("<b>Major version:</b> " + Request.Browser.MajorVersion + "<br />");
Response.Write("<b>Minor version:</b> " + Request.Browser.MinorVersion + "<br />");
Response.Write("<b>Beta:</b> " + Request.Browser.Beta + "<br />");
Response.Write("<b>Cookies:</b> " + Request.Browser.Cookies + "<br />");
Response.Write("<b>Frames:</b> " + Request.Browser.Frames + "<br />");
Response.Write("<b>Tables:</b> " + Request.Browser.Tables + "<br />");
Response.Write("<b>ActiveX:</b> " + Request.Browser.ActiveXControls + "<br />");
Response.Write("<b>Java Applets:</b> " + Request.Browser.JavaApplets + "<br />");
Response.Write("<b>JavaScript:</b> " + Request.Browser.JavaScript + "<br />");
Response.Write("<b>VBScript:</b> " + Request.Browser.VBScript + "<br />");
Response.Write("<b>Platform:</b> " + Request.Browser.Platform + "<br />");
Response.Write("<b>Crawler:</b> " + Request.Browser.Crawler + "<br />");
%>
Output of above Code:
Name: 119.160.194.218
IP: 119.160.194.218
User agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Language: en-us,en;q=0.5
Browser: Firefox
Type: Firefox3.5.3
Version: 3.5.3
Major version: 3
Minor version: 0.5
Beta: False
Cookies: True
Frames: True
Tables: True
ActiveX: False
Java Applets: True
JavaScript: True
VBScript: False
Platform: WinXP
Crawler: False
The output will vary depending on your browser selection.
Thnx.
Happy programming!
Thursday, September 3, 2009
Open New Tab/Window (Response.Redirect open in new web page)
Thursday, August 27, 2009
What’s the difference between <span> and <div> tags?
Tuesday, August 18, 2009
Programming the web.config File Using C#
What Is web.config?
The web.config file is the application’s configuration file. It is typically used to configure an ASP.NET Web application and define the configuration settings for the Web application. It typically contains the application-wide settings, such as database connection string, culture settings, authentication, and authorization information, etc. In ASP.NET 1.x, much effort was required to manipulate the web.config file programmatically. With ASP.NET 2.0 however, this can be done quite easily and efficiently. The following section discusses how this can be achieved.
The Configuration API in ASP.NET 2.0
The configuration API of ASP.NET 2.0 adds a great level of flexibility in that it allows us to add or edit a configuration file seamlessly in ASP.NET. The WebConfigurationManager class in the System.Web.Configuration namespace has the OpenWebConfiguration method that can be used to open the configuration file of the application as a Configuration object for reading from or writing to the configuration file. The virtual path to the configuration file is specified to this method as a parameter. The following code snippet displays all the keys of the appSettings section of the web.config file:
Configuration configuration = WebConfigurationManager.OpenWebConfiguration(”~”); AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection(”appSettings”); if (appSettingsSection != null) { foreach (string key in appSettingsSection.Settings.AllKeys) { Response.Write(key); } } The following method can be used to modify a specific key — value pair of the web.config file — programmatically using C#:
public void Modify(string key, string value) { Configuration configuration = WebConfigurationManager.OpenWebConfiguration(”~”); AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection(”appSettings”); if (appSettingsSection != null) { appSettingsSection.Settings[key].Value = value; config.Save(); } }
The following method can be used to delete a specific key in the web.config file programmatically using C#:
public void Remove(string key) { Configuration configuration = WebConfigurationManager.OpenWebConfiguration(”~”); AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection(”appSettings”); if (appSettingsSection != null) { appSettingsSection.Settings.Remove(key); config.Save(); } }
Conclusion
Even if modifying a web.config file programmatically can be a handy solution in some situations, it is not recommended to do so frequently in a Web application, as any change in the web.config file will restart the Web server and refresh the cache entries.