Tuesday, April 28, 2009

ASP.NET Client Side State Management

There are two types of state management - server side and client side. In this post I'm going to introduce the client side state management and explain when to choose client side or server side state management.

Client Side State Management Techniques

Client side state management include the following techniques:

  • ViewState - ASP.NET track the state of the controls on the pages through
    the ViewState. Also, ViewState is used to store small custom values.
    Don't hold big custom data object because it will affect the performance of
    your web page.
  • Hidden fields - hidden fields are html input control with a type of hidden -
    . They are used to store data in the html without presenting
    the data in the browser. The data of hidden field is available when the form is
    processed or when using javascript. The ViewState use hidden field to save its
    data. You can use the View Source operation (if enabled) to scan the web page's
    source and to look for hidden fields.
  • Query strings - query string state is stored in the URL that is sent to the browser.
    Unlike ViewState and hidden fields, the user can see the values without using
    special operations like View Source. The parameters are passed in the URL
    separated by the & symbol.
    For example, in the following URL the query string is built out of two data
    parameters, a and b, which hold the values 1 and 2 - http://www.srl.co.il?a=1;b=2.
  • Cookies - the cookies store their values in the user's browser and the browser
    send them back to the server every time a request is performed.
    Cookies are maintained during the session of a user in the web site and therefore
    can be used in multiple web pages of the site.
  • Control state - when building custom controls you should use the control state
    to save your state if EnableViewState property is set to false.
    This is the only reason to use this technique.

How do you know when to choose the client side or server side state management?

When to Choose Client Side State Management

Choose client side for better scalability and support multiple servers. The client side helps to get better scalability by storing state data in the user's browser instead of using the web server's memory. The client side support multiple servers because all the state is located in the browser. This way when you are redirected to another server you don't need to worry about your state.

The thing I wrote doesn't means that you can't use server side state management for the supporting of multiple server. To enable server side state management to be able to support multiple servers you'll have to use centralized state management (state server or store state in database) or you'll have to use techniques like sticky
connection for load balancing.

When to Choose Server Side State Management

Choose server side for better security and to reduce bandwidth and web page's size. Server side state management is more secure. The state is saved on the server and therefore isn't delivered to the client.
Confidential state data shouldn't be used with client side state management. Server side reduce the traffic to and from the client because data isn't sent to the browser and it's saved on the server. You should always remember that using client side state management sends data to the user's browser and that data is sent back to the server
every time. This situation increases bandwidth usage and therefore your application will be less responsive and you'll suffer from performance issues.

In the next post I'll drill down into the client side techniques and explain how to use them.

Undocumented SQL Server 2000 functions



Encryption in SQL server [pwdencrypt,pwdcompare]

There are some hidden functions in SQL server through which we can encrypt any string and store the same in the table. This will be very helpful in encrypting the user password and other sensitive user data. Encryption supported by SQL server is one way hash. One way hash is nothing but the string encrypted cannot be decrypted. The only way is to compare values with encrypted string.

DECLARE @ClearPIN varchar (255)
DECLARE @EncryptedPIN varbinary(255)
SELECT @ClearPIN = 'test'
SELECT @EncryptedPIN = CONVERT (varbinary (255), pwdencrypt (@ClearPIN))
SELECT pwdcompare(@ClearPIN, @EncryptedPIN, 0)

In the above example @EncryptedPIN will store the cipher Text. The data in this string is not the encrypted string instead it will return the hash code of the supplied plain string.

Saturday, February 28, 2009

SQL Server 2005 Locking Hints

ROWLOCK : Use row-level locks when reading or modifying data.

PAGLOCK : Use page-level locks when reading or modifying data.

TABLOCK : Use a table lock when reading or modifying data.

DBLOCK : Use a database lock when reading or modifying data.

UPDLOCK : UPDLOCK reads data without blocking other readers, and update it later with the assurance that the data has not changed since last read.

XLOCK : Use exclusive locks instead of shared locks while reading a table, and use hold locks until the end of the statement or transaction.

HOLDLOCK : Use a hold lock to hold a lock until completion of the transaction, instead of releasing the lock as soon as the required table, row, or data page is no longer required.

NOLOCK : This does not lock any object. This is the default for SELECT operations. It does not apply to INSERT, UPDATE, and DELETE statements.


Sunday, April 27, 2008

Different Options for Importing Data into SQL Server

In addition to using the Import / Export wizards and/or DTS or SSIS to move data into SQL Server there are also a few other options for doing this that are built into SQL Server. Some these other options include bcp, BULK INSERT, OPENROWSET as well as others. The following examples show you some of these different options for importing data and how you can use some of these inline with your T-SQL code as well as others that can be run from the command line.

BCP
This is one of the options that is mostly widely used. One reason for this is that it has been around for awhile, so DBAs have come quite familiar with this command. This command allows you to both import and export data, but is primarily used for text data formats. In addition, this command is generally run from a Windows command prompt, but could also be called from a stored procedure by using xp_cmdshell or called from a DTS or SSIS package.

Here is a simple command for importing data from file C:\ImportData.txt into table dbo.ImportTest.

bcp dbo.ImportTest in 'C:\ImportData.txt' -T -SserverName\instanceName

BULK INSERT
This command is a T-SQL command that allows you to import data directly from within SQL Server by using T-SQL. This command imports data from file C:\ImportData.txt into table dbo.ImportTest.

BULK INSERT dbo.ImportTest
FROM 'C:\ImportData.txt'
WITH ( FIELDTERMINATOR =',', FIRSTROW = 2 )

OPENROWSET
This command is a T-SQL command that allows you to query data from other data sources directly from within SQL Server. By using this command along with an INSERT INTO command we can load data from the specified data source into a SQL Server table.

This command will pull in all data from worksheet [Sheet1$]. By using the INSERT INTO command you can insert the query results into table dbo.ImportTest.

INSERT INTO dbo.ImportTest
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\ImportData.xls', [Sheet1$])

Here is another example where data is pulled from worksheet [Sheet1$] by using a SELECT * FROM command. Again, by using the INSERT INTO command you can insert the query results into table dbo.ImportTest. The query can be any valid SQL query, so you can filter the columns and rows by using this option.

INSERT INTO dbo.ImportTest
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\ImportData.xls', 'SELECT * FROM [Sheet1$]')

OPENDATASOURCE
This command is a T-SQL command that allows you to query data from other data sources directly from within SQL Server. This is similar to the OPENROWSET command.
INSERT INTO dbo.ImportTest
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\ImportData.xls;Extended Properties=Excel 8.0')...[Sheet1$]

OPENQUERY
Another option is OPENQUERY. This is another command that allows you to issue a T-SQL command to select data and again with the INSERT INTO option we can load data into our table. There are two steps with this process, first a linked server is setup and then second the query is issued using the OPENQUERY command. This option allow you to filter the columns and rows by the query that is issued against your linked data source.
EXEC sp_addlinkedserver 'ImportData',
'Jet 4.0', 'Microsoft.Jet.OLEDB.4.0',
'C:\ImportData.xls',
NULL,
'Excel 8.0'
GO

INSERT INTO dbo.ImportTest
SELECT *
FROM OPENQUERY(ImportData, 'SELECT * FROM [Sheet1$]')

Linked Server
Here is yet another option with setting up a linked server and then issuing a straight SQL statement against the linked server. This again has two steps, first the linked server is setup and secondly a SQL command is issued against the linked data source.
EXEC sp_addlinkedserver 'ImportData',
'Jet 4.0', 'Microsoft.Jet.OLEDB.4.0',
'C:\ImportData.xls',
NULL,
'Excel 8.0'
GO

INSERT INTO dbo.ImportTest
SELECT * FROM ImportData...Sheet1$

Friday, December 21, 2007

Sending Emails with Dynamic Content

I want the message of the email to like
Hi , Thank you for sending this email. See you soon. Good Bye,

Code :
try

{
StreamReader sr=new StreamReader("MyContent.htm");
sr = File.OpenText("MyContent.htm");
string result = sr.ReadToEnd();
sr.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
return;
}
string MessageBody=result.Replace("", txtFriendName.Text);
MessageBody=MessageBody.Replace("", txtMyName.Text);
MessageBody= MessageBody.Replace("",DateTime.Today.ToString());
MailMessage mail = new MailMessage();
mail.Body = MessageBody;
mail.BodyFormat = MailFormat.Html;
mail.From = "YOUR EMAIL ID";
mail.To = "YOUR FRIEND'S EMAIL ID";
mail.Subject = "Dynamic Content Email From "+ txtMyName.Text;
SmtpMail.SmtpServer = "your email server";
SmtpMail.Send(mail);

Message Box in Asp.Net

public void ShowMessageBox(string message)
{
Page currentPage = (Page)HttpContext.Current.Handler;
if (currentPage != null)
{
ScriptManager manager = ScriptManager.GetCurrent(currentPage);
string _msg = "alert('" + message + "');";
if (manager != null && manager.IsInAsyncPostBack)
{
ScriptManager.RegisterClientScriptBlock(currentPage, currentPage.GetType(), "msg", _msg, true);
}
else
{
currentPage.ClientScript.RegisterStartupScript(currentPage.GetType(), "msg", _msg, true);
}
}
}

Sunday, December 16, 2007

When I See You !!

Nothing explains,

The rush of blood

Pumping through my veins

I hope you know,

That I like You.

I hope you say

That you do too.

I knew that

Iwould like to be,

Protectedby someone like you.

How clear can I

Make it all now,

What can I say

Where why and how.

I cannot think

Words unspoken

The way i thought

My heart was broken.
Missing u a lot today…. dont […]





Why do men love women?


The feminine sex is one of the most refreshing things ever hit the humanity. I mean, come to think of it, at the root of every historical event, there is always a women involved. Even in fairytales, mythologies and legends, no fable is complete without the touch of the feminine.
Man is the residue of Woman. They are born in the blood, kicking and screaming, because they’ve been wrenched from the best place in existence. So much that Man has to spend the rest of his life in pursuit of the recreation of that consciousness. Right from a warm bed, to a cosy home, to a cushy job, to a luxury car to a… you name it.
A woman is the best concept God has ever sold to a Man. They always talk about the male getting in touch with his feminine side. How come they don’t say that about woman? Hehehe . They don’t need to discover the masculine side, as long as they have something masculine by their side. Women are like cats, you can never own them. But if you treat them like the royalty they are, then their kingdom is yours.
Men need women to fall in love with; women just don’t need a mirror. God created man before woman. But you always make a rough draft before the final masterpiece.
Women are great at communication, the best in fact. A woman can say more in a sigh than a man can in a speech. They get the last word in every argument. Anything a man says after that is the beginning of a new argument. Funny thing is , men can argue forever; only women can make them stop. All she needs to do is start crying.
A man needs a woman to make money, If women didn’t exist then all the money in the world would have no meaning. Think about it, a world without women.. no television, no chocolate, no breakfast, no channel No 5, no Bipasha Basu, no Brad Pitt.. no Madonna, and the ultimate horror – no Playboy !
A man spends his whole life trying to be a man, a woman spends hers trying not to be woman. She’s a mother, a wife, a daughter, a sister, a grandmother etc. A man will always try to be a man first. It is this conflict that is his mystery.
All art serves the feminine. There are very few female poets, that’s because most they become the poem. There are more paintings of women than there are of any other subject. That’s probably because it’s the only time a man can take his won sweet time with various women without the guilt.
Men love naked women. Men are supposed to love them! How’s that for getting in touch with your feminine side? Most “married” or “faithful” men love to look at pictures of naked women. That doesn’t mean they’re cheating, it’s just that they can’t get enough of the feminine.
A woman will not own just one pair of shoes, will she?
So will man ever “figure” out women? I don’t think so. We’re not supposed to. Man is like the Earth, he just needs to keep on spinning to keep things going. The woman is the Galaxy, she keeps the orbit right, the seasons changing, the weather favorable and the sun always shining.