Tuesday, April 20, 2010

Message Box in Web(Asp.Net)

WebMsgBox class is a message box for ASP.NET.


Recently, I needed a Windows MessageBox like class for ASP.NET. Doing some search on Google, I found some code for message box in VB.NET and converted code from VB.NET to C# and made some changes. Unfortunately, I forgot the URL where I found the code.


WebMsgBox Class


WebMsgBox class represents a message box for ASP.NET applications. This class has a static method Show, which is used to display a message box. The Show method takes a single argument of string type, which is the message you want to display.


using System;
using Microsoft.VisualBasic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWebMsgApp
{
public class WebMsgBox
{
protected static Hashtable handlerPages = new Hashtable();

private WebMsgBox()
{

}

public static void Show(string Message)
{
if (!(handlerPages.Contains(HttpContext.Current.Handler)))
{
Page currentPage = (Page)HttpContext.Current.Handler;
if (!((currentPage == null)))
{
Queue messageQueue = new Queue();
messageQueue.Enqueue(Message);
handlerPages.Add(HttpContext.Current.Handler, messageQueue);
currentPage.Unload += new EventHandler(CurrentPageUnload);
}
}
else
{
Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
queue.Enqueue(Message);
}
}

private static void CurrentPageUnload(object sender, EventArgs e)
{
Queue queue = ((Queue)(handlerPages[HttpContext.Current.Handler]));
if (queue != null)
{
StringBuilder builder = new StringBuilder();
int iMsgCount = queue.Count;
builder.Append("");
handlerPages.Remove(HttpContext.Current.Handler);
HttpContext.Current.Response.Write(builder.ToString());
}
}
}
}



How to use WebMsgBox?

Copy the attached WebMsgBox.cs and add it to your project. You will have to change the namespace of the class. Change the following line to your project's namespace in WebMsgBox.cs file:

namespace MyWebMsgApp

Now call WebMsgBox.Show method when you need to display a message box. For example, if you want to display a message box on a button click, add the following code to the button click event handler.

WebMsgBox.Show("Your message here");

Thursday, April 15, 2010

Find Row Count in Table – Find Largest Table in Database – T-SQL

This script inform the number of rows are existing in the table. This script will gives row numbers for every tables in same database.

SELECT OBJECT_NAME(OBJECT_ID) TableName, st.row_count
FROM sys.dm_db_partition_stats st
WHERE index_id < 2
ORDER BY st.row_count DESC

Monday, April 12, 2010

Find Last intex value in sql

Declare @report_file varchar(50)
Set @report_file = 'murli@indigo.co.in'
Declare @rpt_name varchar(50)
--Set @rpt_name = Substring(@report_file,1,(DATALENGTH(@report_file)-CHARINDEX('.',REVERSE(@report_file))))
Set @rpt_name = Substring(@report_file,1,(DATALENGTH(@report_file)-CHARINDEX('.',REVERSE(@report_file))))
Print @rpt_name

Saturday, April 3, 2010

Backup and Restore Database Via Query

Now, lets get the database backup from query

BACKUP DATABASE AdventureWorks
TO DISK = 'C:\Backup\MultiFile\AdventureWorks.bak',

Now, let us see how we can split one database into two different database files. This method is very similar to taking a single-file backup. By simply adding an additional DISK option we can split the files backup files.

BACKUP DATABASE AdventureWorks
TO DISK = 'C:\Backup\MultiFile\AdventureWorks1.bak',
DISK = 'C:\Backup\MultiFile\AdventureWorks2.bak',
DISK = 'C:\Backup\MultiFile\AdventureWorks3.bak'

Finaly restore the backup files in your database

RESTORE DATABASE AdventureWorks
FROM DISK = 'C:\Backup\MultiFile\AdventureWorks.bak',


Now let us see an example where we restore a database from a split file. This method is very similar to restoring a database from a single file; just add an additional DISK option.

RESTORE DATABASE AdventureWorks
FROM DISK = 'C:\Backup\MultiFile\AdventureWorks1.bak',
DISK = 'C:\Backup\MultiFile\AdventureWorks2.bak',
DISK = 'C:\Backup\MultiFile\AdventureWorks3.bak'
WITH REPLACE

Create Temp table and temp varible table

-- Create Temp Table and insert single row
CREATE TABLE #TempTable (Col1 VARCHAR(100))
INSERT INTO #TempTable (Col1) VALUES('Temp Table - Outside Tran');
-- Create Table Variable and insert single row
DECLARE @TableVar TABLE(Col1 VARCHAR(100))
INSERT INTO @TableVar (Col1) VALUES('Table Var - Outside Tran');
-- Check the Values in tables
SELECT Col1 AS TempTable_BeforeTransaction FROM #TempTable;
SELECT Col1 AS TableVar_BeforeTransaction FROM @TableVar;