Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for December, 2009

Please wait on button click

Posted by Viral Sarvaiya on December 29, 2009


protected void Button1_Click(object sender, System.EventArgs e)
ClientScript.RegisterClientScriptBlock(this.GetType, “reset”, (“document.getElementById(\’”
+ (Button1.ClientID + “\’).disabled=false;”)), true);

protected void Page_Load(object sender, System.EventArgs e) {
Button1.Attributes.Add(“onclick”, (ClientScript.GetPostBackEventReference(Button1, “”) + “;this.value=\’Please wait…\’;this.disabled = true;”));

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


ClientScript.RegisterClientScriptBlock(Me.GetType, _ “reset”, “document.getElementById(’” & Button1.ClientID & “‘).disabled=false;”, True)

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Button1.Attributes.Add(“onclick”, ClientScript.GetPostBackEventReference(Button1, “”) _ & “;this.value=’Please wait…’;this.disabled = true;”)

End Sub

Posted in ASP.NET | Tagged: , , | Leave a Comment »

Insert Null values into datetime column of SQL SERVER using

Posted by Viral Sarvaiya on December 29, 2009

Sometimes we will get the situation where we must insert NULL into datetime column in sql server.

Follow these steps to do that:

1) First Include the namespace:

using System.Data.SqlTypes;

2)  Declare

System.Data.SqlTypes.SqlDateTime getDate;
getDate = SqlDateTime.Null;

3) Insert using command Parameters

cmd11.Parameters.AddWithValue(“@encashed_date”, getDate);

with this procedure you can insert the NULL value in datetime datatype column.

enjoy coding….

Posted in ASP.NET | Tagged: , , , , | 2 Comments »

Add Page as a bookmark

Posted by Viral Sarvaiya on December 28, 2009

just copy and paste the below code for bookmark your page.

function bookmark(url,title){
if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
} else if (navigator.appName == "Netscape") {
} else {
alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");

<input type="button" value="Bookmark us!"
onclick="bookmark('','JavaScript For Bookmark')">

Posted in ASP.NET, Javascript | Tagged: , , | Leave a Comment »

Encryption/Decryption with .NET

Posted by Viral Sarvaiya on December 23, 2009

Encryption and Decryption

The System.Security.Cryptographic namespace within the Microsoft .NET Framework provides a variety of tools to aid in encryption and decryption. The CryptoStream class is used here to demonstrate the encryption and decryption with System.Security.Cryptographic.SymmetricAlgorithm, such as DESCryptoServiceProvider, RC2CryptoServiceProvider, and RijndaelManaged classes.

for more detial

Posted in ASP.NET | Tagged: , , , , , , , , | Leave a Comment »

Drop all the constraints on a table

Posted by Viral Sarvaiya on December 19, 2009

This stored procedure takes table name as parameter and dynamically creates a drop script for all the constraints of given table and executes it. Earliar today I was searching scripts concerning the same but I mostly found scripts related to information schema of the database. As a small developer I thought it was risky to put a hand on that. I was able to create this simple script which just involves a cursor on sysobjects table.

CREATE PROCEDURE DeleteConstraints
@tableName VARCHAR(100)


SET @con = CURSOR FOR SELECT name FROM sysobjects WHERE parent_obj=(SELECT id FROM sysobjects WHERE name=@tableName) AND type IN ('C','D','F','K')

OPEN @con
FROM @con INTO @conName
SET @sql = 'IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N''[' + @conName + ']'') AND type IN (''C'',''D'',''F'',''K'')
ALTER TABLE [dbo].[' + @tableName  + '] DROP CONSTRAINT [' + @conName + ']

FROM @con INTO @conName
CLOSE @con


Posted in Sql Server | Tagged: , , , , | 1 Comment »

Difference between ExecuteQuery And Execute NonQuery

Posted by Viral Sarvaiya on December 1, 2009

ExecuteReader expects to run a query command or a stored procedure that selects records. It expects to have one or more resultsets to return.
SqlDataReader dr = cmd.ExecuteReader();
// process the resultset(s) here

You access the selected records using the SqlDataReader object and use the method Read to loop through them. You move to the next resultset using the NextResults method.

ExecuteNonQuery expects to run a command, or a stored procedure, that affects the state of the specified table. This means anything but a query command. You normally use this method to issue an INSERT, UPDATE, DELETE, CREATE, and SET statement.

ExecuteNonQuery returns only the number of rows affected by the command execution, or –1 should this information be unavailable. It doesn’t give you a chance to access any result set generated by the statement or the stored procedure. Actually, there’s really nothing to prevent you from using this method for a query command, but in this case you get neither the resultset nor the number of the affected rows.
nRecsAffected = cmd.ExecuteNonQuery();
// check the record(s) affected here

The number of affected rows is also made available through the RecordsAffected property of the SqlCommand object. This property equals –1 in case of errors or if a query command is executed.

ExecuteScalar expects to run a query command, or more likely a stored procedure, that returns data. However, this method is different from ExecuteReader in that it just makes available, as a scalar value, the first column on the first row of the selected resultset.
Object o = cmd.ExecuteScalar(); cmd.Connection.Close();
// work on the scalar here

The method returns the value as a boxed object. It’s then up to you to unbox or cast that value to the proper, expected type.

ExecuteScalar turns out to be particularly useful when you have statistical or aggregate operations to accomplish on a certain amount of data. In these and similar circumstances, there is just one value that you might want to return back to the caller. Because of its use cases, you normally use this method on more or less complex stored procedures rather than on single SQL statements.

ExecuteXmlReader builds up and returns an XmlReader object after a SELECT command that exploits XML features in SQL Server 2000 has been issued.

in short :

Execute NonQuery..
1.It will not return any data.
2.It is used with insert and update.
3.It returns only the number of rows affected.

Execute Scaler..
1.It returns only one value.
2.That value will the first column first row value.

Execute Query..
1.Its for command objects.
2.It returns the value given by database through select statement.

Posted in Sql Server | Tagged: , , , , , | 4 Comments »

%d bloggers like this: