Monday, May 20, 2013

SQL Query to List All Columns alphabetically


select c.name from
    sys.columns c
    join sys.tables t on c.object_id = t.object_id
where t.name = 'Article'
Order by
    c.name

Tuesday, April 2, 2013

Create Database from the .dbml file in .Net

If you have .dbml file, you just have to write two lines which are as follows:

YourDBMLDataContext objContext = new YourDBMLDataContext();
objContext.CreateDatabase();

Note: Make sure, you have given the right ConnectionString to the .dbml file.

By running the above code, New database will be created in the database server.

Monday, March 25, 2013

Generic Error occured in GDI+ while saving bitmap image

Most of the time, when this kind of issue occurs, then it must be due to the directory you are trying to save to, doesn't have proper permissions. You need to grant write permissions to that directory. 

Thursday, February 21, 2013

How to validate the File Size using JQuery


As the IE is not supporting the HTML 5 File API then what is the solution to check the file size. Well, with IE you can use FileSystem ActiveX object to get the file size. But the problem with latest IE versions (7,8 and 9) is that by default they don't allow ActiveX objects from security reason perspective. If you want to run this, then you have to explicitly allow ActiveX object by changing the settings of IE.

To allow ActiveX -> Go to Tools->Internet Options-> Security->Custom Level->Choose Enable or Prompt ActiveX.

But this is not a perfect solution as you can't ask your end-users to do these kind of settings. Hope that IE 10 will support the HTML 5 API.

So the complete code looks like below code. It first checks if browser is IE or not. If it's IE then using ActiveX object gets the file size. If not then get the file size using HTML 5 API.
$(document).ready(function() {
   $("#flUpload").change(function () 
   { 
     var iSize = 0;
     if($.browser.msie)
     {
        var objFSO = new ActiveXObject("Scripting.FileSystemObject");
        var sPath = $("#flUpload")[0].value;
        var objFile = objFSO.getFile(sPath);
        var iSize = objFile.size;
        iSize = iSize/ 1024;
     }
     else
        iSize = ($("#flUpload")[0].files[0].size / 1024); 

     if (iSize / 1024 > 1) 
     { 
        if (((iSize / 1024) / 1024) > 1) 
        { 
            iSize = (Math.round(((iSize / 1024) / 1024) * 100) / 100);
            $("#lblSize").html( iSize + "Gb"); 
        }
        else
        { 
            iSize = (Math.round((iSize / 1024) * 100) / 100)
            $("#lblSize").html( iSize + "Mb"); 
        } 
     } 
     else 
     {
        iSize = (Math.round(iSize * 100) / 100)
        $("#lblSize").html( iSize  + "kb"); 
     }    
  }); 
});
//Code Ends

Saturday, February 16, 2013

Regular Expressions for Validations in Jquery


1. Allow alphabets only:
var regExpressions = "^([a-zA-Z]+(_[a-zA-Z]+)*)(\s([a-zA-Z]+(_[a-zA-Z]+)*))*$";

2. var validEmail = "^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$"

Wednesday, February 13, 2013

Large File Upload in asp.net


I was trying to upload a larger file to my website (which was recently shifted to IIS7 from IIS6) when I start getting weird error and most common was
"404 not found
The requested document was not found on this server."


with almost a blank white page. I checked my web.config for maxRequestLength attribute and which was properly set that is maxRequestLength = 10240 i.e. I was allowing 10 MB file to be uploaded whereas the file I was trying to upload was just 3 MB, and it was known that files under 10 MB were successfully uploaded in past.

I started googling for the error but unfortunately there wasn't much about this error over the internet.
Started thinking that what special was done with the website recently. There wasn't anything special but the shift from IIS6 to IIS7.

Now when I searched for setting file size in IIS7 I came to know that maxRequestLength is no more functional inIIS7. We have to set maxAllowedContentLength under . Made the required change and whoaaa it start working again.
A thing to remember is that maxAllowedContentLength takes value in Bytes whereas maxRequestLengthaccepts value in Kilo Bytes.
So to set maxAllowedContentLength for 10MB you have to set maxAllowedContentLength = 10485760.
Following is the web.config code which should be set for IIS7 to allow 10MB(say) files.

<­system­.­webServer­>
<­security­>
<­requestFiltering­>
<­requestLimits maxAllowedContentLength="10485760"­/­>
<­/­requestFiltering­>
<­/­security­>
<­/­system­.­webServer­>

Wednesday, January 30, 2013

Query for fetching the records in Parent-Child DB table in SQL Server

DECLARE @FileTreeId INT = 300 (For instance)

;WITH FileTreeHierarchy AS
(
    SELECT FileTreeID, Company, Parent, Name, 1 AS 'Level'
    FROM FileTree 
    WHERE FileTreeID = @FileTreeId

    UNION ALL

    SELECT F.FileTreeID, F.Company, F.Parent, F.Name, 
    FH.Level + 1 AS 'Level'
    FROM FileTree F
    INNER JOIN FileTreeHierarchy FH ON FH.Parent = F.FileTreeID
)
 
SELECT FileTreeId, Company CompanyId, Parent, Name, 
[Level] = CAST([Level] as int) 
FROM FileTreeHierarchy Order By [Level] Desc