ASP.Net and SQL Server Globalization - Setting Culture for Date and Time

· 0 comments

This is one of the initial difficulties I found dealing with date and time for websites with cultures set to anything but the default. No problem with US formatted dates.  However, with British formatted dates for example, you have to spend a lot of time formatting your strings within ASP.Net and your SQL server stored procedures. However, there is a way to do this as if you are programming with the default date and time format.  Here’s how…

 


1.       Connect to your server and expand Security node.


2.       Expand Logins node and locate your user.


3.       Right-click the user and click properties.


4.       Find default language and set to your language.  For example, I work mostly with British format dates (dd/mm/yyyy) so I set it to “British English”.






5.       In your website’s web.config, add the following entry under the <system.web> node.

 

<globalization culture="en-GB"/>

 

For other supported cultures go to http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx

 

Now, sql server will always expect your dates to be “en-GB” formatted or whatever culture you have set it to be.  No need to explicitly convert your date strings in ASP.Net or SQL Server.

 

 

ASP.Net: Better than Zip - File compression using .Tar.gz Part 1

· 0 comments

Previously I wrote about extracting files from a .Tar.gz archive in Asp.net. This however begs the question, “How do I put my files in a .tar.gz archive in the first place?”

 

This is a post to answer that question.  In my opinion, the best way to put files in a tar.gz archive is to use the executable files for reasons of performance and the fact that most archiving are done in the backend.  It is also easier implemented especially if automation is involved. But if you think that you must and want only to compress files in ASP.Net then skip ahead to Part 2 of this post.  If you want to try my recommendation then read on.

 

First, download the Gzip executable here. Just extract and place the file gzip.exe to your windows folder (or any folder on your system path). The objective is to be able to call the program from anywhere.

 

For the Tar executable you can download this one. Likewise, place it on your windows folder. For simplicity, it’s probably best to rename the file to just “tar.exe”.

 

You may then compress files from the command prompt like this.

 

    Tar –czf myarchive.tar.gz *.xml

 

Where the executable will compress all xml files in the current directory into an archive with filename myarchive.tar.gz

 

You may also  automate the process using vbscript like this.

 

    dim WshShell

    Set WshShell = WScript.CreateObject("WScript.Shell")

    dim oZip

    set oZip = WshShell.Exec("tar -czf myarchive.tar.gz *.xml")

    do while oZip.Status = 0

        wscript.Sleep(100)

    loop

    set WshShell = nothing

 

Don’t forget to read my post on how to extract files from .TAR.GZ files using ASP.Net if you haven’t done so yet. Continue to Part 2

ASP.Net: Better than Zip - File compression using .Tar.gz Part 2

· 0 comments

(Cont’d from Part 1)

 

Maybe you’re just renting web space or want to compress file using windows forms or simply don’t like anything not done in ASP.Net then here’s how you do it.

 

Like in my previous posts, import the namespaces….

 

Imports ICSharpCode.SharpZipLib.Tar

Imports ICSharpCode.SharpZipLib.GZip

Imports System.IO

 

The paste the following method in your code…

 

   Sub AddToArchive(ByVal Archive As String, ByVal FolderPath As String, ByVal Files As String)

        Dim gZipStream As New GZipOutputStream(File.Create(Archive))

        Dim TarFile As New TarOutputStream(gZipStream)

 

        For Each s As String In Directory.GetFiles(FolderPath, Files)

            Dim fiEntry As New FileInfo(s)

            Dim fsEntry As FileStream = File.OpenRead(s)

            Dim b(fsEntry.Length - 1) As Byte

            fsEntry.Read(b, 0, b.Length)

 

            Dim thEntry As New TarHeader

            thEntry.Name = fiEntry.Name

            thEntry.Size = fiEntry.Length

            thEntry.ModTime = Now

            Dim TarFileEntry As New TarEntry(thEntry)

            TarFile.PutNextEntry(TarFileEntry)

            TarFile.Write(b, 0, b.Length)

            TarFile.CloseEntry()

            b = Nothing

            TarFileEntry = Nothing

            fsEntry.Close()

            fsEntry.Dispose()

        Next

        TarFile.Finish()

        TarFile.Close()

        TarFile.Dispose()

        gZipStream.Close()

        gZipStream.Dispose()

    End Sub

 

You may then call the method like this…

 

        Dim sArchive As String = "d:\temp\logs.tar.gz"

        Dim sFolder As String = "D:\logs"

        Dim sFiles As String = "*.log"

        AddToArchive(sArchive, sFolder, sFiles)

 

Where  d:\temp\logs.tar.gz is the output filename and directory, D:\logs is the folder where the files to be compressed are located and *.log is the search pattern whereby all files with .log extension will be compressed.

 

 

Better than Zip - Extracting .TAR.GZ files in ASP.Net

· 0 comments

I once had a requirement to compress 100,000 log files into one archive and be able to extract a single file for processing. Zip is simply not up to the challenge. It can’t take that many files. That would have made my life easier. There are, after all, many resources in the internet on how to do this. After some research I find that .TAR.GZ files (TAR files that were GZipped) seem the best candidate amongst many products.  Mainly because it’s free. They also provide superb compression and frankly I haven’t explored the limits yet of how many files in could pack.

 

The drawback is the limited documentation on how to handle them in ASP.Net. If you’re willing to spend a couple of bucks then you can indeed have it easy.  It is perhaps more economical to just buy components rather than spend hours working with a free one. But then, if someone tells you how work with the free one then you wouldn’t have to buy would you?

 

In this post, I’m going to talk about extracting .TAR.GZ files in ASP.Net. However, don’t ask me the details of the code. Like I said, there’s not much documentation around. I came up with this code after hours of trial and error.

 

The critical component for this sample is SharpZipLib.  Download the dll file here.

 

After downloading, copy ICSharpCode.SharpZipLib.dll from the net-20 folder and place on your website’s Bin folder.

 

Import the namespaces System.IO, ICSharpCode.SharpZipLib.Tar and ICSharpCode.SharpZipLib.GZip on your code-behind or class file.

 

Imports ICSharpCode.SharpZipLib.Tar

Imports ICSharpCode.SharpZipLib.GZip

Imports System.IO

 

Or like this if you’re using inline code

 

<%@ Import Namespace="System.IO" %>

<%@ Import Namespace="ICSharpCode.SharpZipLib.Tar" %>

<%@ Import Namespace="ICSharpCode.SharpZipLib.GZip" %>

 

Then copy the method below

 

    Sub ExtractFile(ByVal Archive As String, ByVal Filename As String, ByVal DestinationFolder As String)

        Dim gZipStream As New GZipInputStream(File.OpenRead(Archive))

        Dim ZipFile As New TarInputStream(gZipStream)

        Dim ZipFileEntry As TarEntry

        ZipFileEntry = ZipFile.GetNextEntry

        Dim bFound As Boolean = False

        Do While ZipFileEntry IsNot Nothing

            If ZipFileEntry.Name = Filename Then

                bFound = True

                Exit Do

            End If

            ZipFileEntry = ZipFile.GetNextEntry

        Loop

        If bFound = False Then

            ZipFile.Close()

            ZipFile.Dispose()

            gZipStream.Close()

            ZipFile.Dispose()

        End If

        Dim fs As New FileStream(DestinationFolder & Filename, FileMode.Create)

        ZipFile.CopyEntryContents(fs)

        ZipFile.Close()

        ZipFile.Dispose()

        fs.Close()

        fs.Dispose()

    End Sub

 

The method above is designed for general use. It will extract one file and save it to disk.  For example….

 

        Dim sFilename As String = "pic1.jpg"

        Dim sArchive As String = "D:\temp\pics.tar.gz"

        Dim sDestinationFolder As String = "D:\"

        ExtractFile(sArchive, sFilename, sDestinationFolder)

 

This will extract the file “pic1.jpg” from the archive “D:\temp\pics.tar.gz” and save it under the folder “D:\”. From there you may do a couple of things like redirect to the file or do some processing.  That depends on your requirement. It’s perfect for archiving pictures, old content, or whatever you see fit to save space.  You will have to maintain an index of that somewhere of course.

Don't miss this post on file compression using .Tar.Gz in ASP.Net

 

 


An ASP.Net developer's Assesment on Chrome's 1st week

· 0 comments

Everyone’s all the rave for the release of Google’s Chrome. At least, in IT circles it is. But here’s my 2 cents for what it’s worth.

1. It’s fast.

I have a 1.4Mb page (in html) with a variety of javascript, textboxes, dropdown controls and many others.
IE – 8 secs
Firefox – 4 secs.
Chrome – 1sec.

However, users of Asp.net websites will most likely see this error a lot.

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

It’s rendering the visual elements so fast, users will most likely cause a postback (click a button or link) before the ViewState controls are rendered on the page.

2. Stable. I have yet to see it crash.

3. Aesthetically pleasing.

You would think this is a Microsoft product because of its office 2007 blue theme. There’s also a couple of themes out there. Just google for “Chrome Themes”.

4. Not so many plug-ins yet. Probably a bad thing for Bloggers and Social Bookmarkers.

5. Resource Hog?

At least my friend thinks so when he’s watching something on YouTube. I tested it with this video and it seems to work fine.

Chrome – 12 % CPU, 20MB memory
IE – 1-2% CPU with occasional burst to 45%. 50MB memory
Firefox – 12-15%. 50MB memory

It’s even performing better than firefox. However, try to doing an "inspect element" (right click on any page) and the inspector would use so much CPU.

6. Fonts are horrible.

At least, Firefox fonts are awful too. But if you’ve got ClearType. This solves the problem with both Firefox and Chrome.

7. AJAX does not work 100%.

I can’t make this code work but it does with Firefox and IE. I guess it’s attempting to use an ActiveX control?


function GetXmlHttp()
{
var oXmlHttp=false;

// -----> This method was provided from Jim Ley's website
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
oXmlHttp = false;
}
}
/*@end @*/


if (!oXmlHttp && typeof XMLHttpRequest!='undefined')
{
oXmlHttp = new XMLHttpRequest();
}

return oXmlHttp;
}


XMLHttpRequest() Object works on this page

AJAX Control Toolkit seems to work as well as other AJAX extensions controls.

If it manages to capture a large chunk of the market, this would force developers to recode. At this point, they have to make us happy. Others are also reporting problems with AJAX on Chrome

8. Not an IE killer. This study says so. And I would agree.

These are crude tests of course. Hopefully, somebody more qualified with more resources can do a more scientific one. At this point, Chrome is the browser I would use for fun but not for work. But then, how many percent of people out there uses the browser for work? I bet they’re the 1.15% that downloaded Chrome this week. So overall, it’s showing a lot of promise. (Sniff) I shall have to recode soon…