Monday, July 20, 2009

ASP NET max file size check

This is the code below to redirect the same page if “Max Request Length Exception” occur. Just write this code on global.asax.

If the page content size is greater than maxRequestLength then this code redirect the page to the same page with query string action=exception. Just read this query string value and show the proper message the client browser.

protected void Application_BeginRequest(Object sender, EventArgs e)

{

HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");

//Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size

int maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;

//This code is used to check the request length of the page and if the request length is greater than

//MaxRequestLength then retrun to the same page with extra query string value action=exception

HttpContext context = ((HttpApplication)sender).Context;

if (context.Request.ContentLength > maxRequestLength)

{

IServiceProvider provider = (IServiceProvider)context;

HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));

// Check if body contains data

if (workerRequest.HasEntityBody())

{

// get the total body length

int requestLength = workerRequest.GetTotalEntityBodyLength();

// Get the initial bytes loaded

int initialBytes = 0;

if (workerRequest.GetPreloadedEntityBody() != null)

initialBytes = workerRequest.GetPreloadedEntityBody().Length;

if (!workerRequest.IsEntireEntityBodyIsPreloaded())

{

byte[] buffer = new byte[512000];

// Set the received bytes to initial bytes before start reading

int receivedBytes = initialBytes;

while (requestLength - receivedBytes >= initialBytes)

{

// Read another set of bytes

initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);

// Update the received bytes

receivedBytes += initialBytes;

}

initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);

}

}

// Redirect the user to the same page with querystring action=exception.

context.Response.Redirect(this.Request.Url.LocalPath + "?action=exception");

}

}

Tuesday, July 14, 2009

SQL Server EXEC sp_executesql

DECLARE @parameters nvarchar(max)
SELECT @parameters = '@FromDate datetime,
@ToDate datetime,
@Adults int
@CountryId int'

EXEC('EXEC sp_executesql N''' + @sql + ''', N''' + @parameters + ''',

@FromDate = N''' + '2010-01-01' + ''',
@ToDate = N''' + '2010-01-10' + ''',
@Adults = N''' + 2 + ''',
@CountryId = N''' + 123 + '''')

SQL Server Case Sensitive Search

select [Name] from suppliers where [name] like '%Test%'
RESULTs-3
1. Sampath test
2. Sampath Test
3. Sampath Test_view

--WITH CASE SENSITIVE SEARCH--------------------------------
select [Name] from suppliers where [name] like '%Test%' COLLATE SQL_Latin1_General_CP1_CS_AS

RESULTs-2
1. Sampath Test
2. Sampath Test_view

Saturday, July 11, 2009

SQL Server Error Handle in C#

public void DisplaySqlErrors(SqlException exception)
{

for (int i = 0; i <>
{

Console.WriteLine("Index #" + i + "\n" +
"Source: " + exception.Errors[i].Source + "\n" +

"Number: " + exception.Errors[i].Number.ToString() + "\n" +
"State: " + exception.Errors[i].State.ToString() + "\n" +
"Class: " + exception.Errors[i].Class.ToString() + "\n" +
"Server: " + exception.Errors[i].Server + "\n" +
"Message: " + exception.Errors[i].Message + "\n" +
"Procedure: " + exception.Errors[i].Procedure + "\n" +
"LineNumber: " + exception.Errors[i].LineNumber.ToString());
}

Console.ReadLine();
}

SQL Cursor

DECLARE @CountryId AS int
DECLARE @Name AS varchar(50)


DECLARE cur CURSOR FOR SELECT c.CountryId, ci.[Name]
FROM City ci

INNER JOIN HotelCountry hc
ON ci.CountryId = hc.country_id

INNER JOIN Country c
ON hc.country_name = c.[Name]


OPEN cur

FETCH NEXT FROM cur INTO @CountryId, @Name


WHILE @@FETCH_STATUS = 0

BEGIN
-- Do what ever you want.

UPDATE City
SET CountryId = @CountryId
WHERE [Name] = @Name


-- Next.

FETCH NEXT FROM cur INTO @CountryId, @Name

END

CLOSE cur

DEALLOCATE cur