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 

Tuesday, January 29, 2013

Add Dynamic textboxes using Jquery

<head>
<script type="text/javascript">
function AddMoreTextboxes() {
    var counter = 1;
    var firstTextboxValue = $("#txtselectedValue").val();
    if (firstTextboxValue == '') {
        alert('Enter the value in First Textbox !');
        return;
    }
    else {
        if (counter > 5) {
            alert("Only 5 textboxes allow");
            return false;
        }
        var newTextBoxDiv = $(document.createElement('div'))
             .attr("id", 'TextBoxDiv' + counter);

        newTextBoxDiv.attr("class", "newTextBoxDiv");

        newTextBoxDiv.after('<label></label>' + '<input type="text" name="textbox' + counter +
            '" id="textbox' + counter + '" />').html('<label></label>' +
            '<input type="text" style="width:200px;" class="newTextbox" name="textbox' + counter + '" id="textbox' + counter + '" value="" />');

        newTextBoxDiv.appendTo("#TextBoxesGroup");
        counter++;
    }
}
</script>
</head>
<body>
<div>
<img src="../../Images/plus.png" width="16" height="16" id="ImgAddMoreTextbox" onclick="AddMoreTextboxes();" />
</div>
</body>

Monday, January 21, 2013

Delete all User-Defined Stored Procedures in SQL Server


declare @procName varchar(500)
declare cur cursor

for select [name] from sys.objects where type = 'p'
open cur
fetch next from cur into @procName
while @@fetch_status = 0
begin
    exec('drop procedure ' + @procName)
    fetch next from cur into @procName
end
close cur
deallocate cur