Tuesday, September 20, 2011

UpdateProgress dinamically centered



<script language="JavaScript" type="text/javascript">
        function adjustDivs() {           
            internal= document.getElementById('divCalculateInt');
            external= document.getElementById('divCalculate');
            dfs = interno.style;
           
            dfs.left = (external.innerWidth - internal.offsetWidth) / 2;
        }

        window.onload = adjustDivs;
        window.onresize = adjustDivs;
        window.onscroll = adjustDivs;
<script>

<asp:UpdatePanel ID="updXXX" runat="server">
<ContentTemplate>
       […]
<ContentTemplate>
<asp:UpdatePanel>

<asp:UpdateProgress ID="updprogXXX" runat="server" AssociatedUpdatePanelID="updXXX" DisplayAfter="150">
<ProgressTemplate>
             <div id="divCalculate" align="center" style="top: 200px; position: absolute; top:0; bottom:0; right:0; left:0;background-color:rgba(0,0,0,0.5)">
                    <div id="divCalculateInt" align="center" style="border: solid 1px #000000; background: #FFFFFF;padding: 5px; width: 320px; height: 50px;margin-top:300px">
                        <asp:Label runat="server" ID="lblLoading2" Text="Computing in progress..."><asp:Label>
                        <asp:Image ID="imgLoading" runat="server" ImageAlign="left" SkinID="imgLoadingBar" /><br /><br />
                        <asp:Label runat="server" ID="lblLoadingInfo" Text="This operation may last some minutes" Font-Size="X-Small" >asp:Label>
                    <div>
                <div>               
        <ProgressTemplate>
<asp:UpdateProgress>

Wednesday, May 4, 2011

Using C# and VB.net togheter in Asp.net

To use in the same Asp.net Web Application classes written in different languages, such C# and VB.net, you have to do this steps:

Create in the App_Code folder two subfolder, called for example VB and CS:

App_Code
->VB
->CS

Next, add in the web.config this lines:
<system.web>
[...]
<compilation>
<codeSubDirectories>
<
add directoryName="CS"/>
<add directoryName="VB"/>
codeSubDirectories>
[...]
compilation>
system.web>

And voila, its done!

PS: obviously, you have to put all vb.net classes in a folder and all the c# classes in the other folder...

Monday, December 6, 2010

SQL Server 2008 Log File Shrink for all databases on a server

After my previous post (24 june 2009), in which I proposed a script to Shrink log Files for all DB in a SQL Server 2005 machine, now you've got a script to do the same thing on SQL Server 2008 servera:

EXECUTE sp_msforeachdb
'USE ?;

Alter Database ? Set Recovery Simple;
Alter Database ? Set Recovery Full;

DECLARE @LogLogicalName nvarchar(100);
SELECT @LogLogicalName = file_name(2);

dbcc shrinkfile(@LogLogicalName, 1);

dbcc shrinkfile(@LogLogicalName, 1);

dbcc shrinkfile(@LogLogicalName, 1);

dbcc shrinkfile(@LogLogicalName, 1);'

Thursday, November 18, 2010

Compare string with Aspnet_Membership password

Small time ago I had a particular problem, I need to compare a password written by a user in a client WinForms software with an Asp.net managed password (stored in Aspnet_Membership table).

The main problem is that the Asp.net password is encrypted in db and it's not possible to decrypt. The only way to solve the problem is to encrypt given password in the same way as Asp.net and then compare password hashes. To do it, "Salt" is needed: the value of PasswordSalt column in Aspnet_Membership.

Here the code (I'm sorry but i haven't enought time to colorize the code :D ):

VB.Net


Public Function Compare(ByVal password As String, ByVal aspnetSalt As String, ByVal aspnetPassword As String) As Boolean
    Dim isEqual As Boolean = False
    If aspnetPassword = GenerateHash(password, aspnetSalt) Then
        isEqual = True
    End If
    Return isEqual
End Function

Private Function GenerateHash(ByVal pwd As String, ByVal salt As String) As String
    Dim p1 As Byte() = Convert.FromBase64String(salt)
    Return GenerateHash(pwd, p1)
End Function

Private Function GenerateHash(ByVal pwd As String, ByVal saltAsByteArray As Byte()) As String
    Dim sha As New System.Security.Cryptography.SHA1CryptoServiceProvider()
    Dim p1 As Byte() = saltAsByteArray
    Dim p2 As Byte() = System.Text.Encoding.Unicode.GetBytes(pwd)
    Dim data() As Byte = New Byte(((p1.Length + p2.Length)) - 1) {}
    p1.CopyTo(data, 0)
    p2.CopyTo(data, p1.Length)
    Dim result As Byte() = sha.ComputeHash(data)
    Dim rtn As String = Convert.ToBase64String(result)
    Return rtn
End Function


C#

public boolean Compare (string password, string aspnetSalt, string aspnetPassword)
{
    boolean isEqual = False
    If (aspnetPassword = GenerateHash(password, aspnetSalt))
    {
        isEqual = True
    }
    return isEqual
}

private string GenerateHash(string pwd, string saltAsBase64)
{
    byte[] p1 = Convert.FromBase64String(saltAsBase64);
    return GenerateHash(pwd, p1);
}

private string GenerateHash(string pwd, byte[] saltAsByteArray)
{
    System.Security.Cryptography.SHA1 sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();

    byte[] p1 = saltAsByteArray;
    byte[] p2 = System.Text.Encoding.Unicode.GetBytes(pwd);

    byte[] data = new byte[p1.Length + p2.Length];

    p1.CopyTo(data, 0);
    p2.CopyTo(data, p1.Length);

    byte[] result = sha.ComputeHash(data);

    string res = Convert.ToBase64String(result);
    return res;
}

Friday, November 12, 2010

Capitalize string in Sql Server

There is a very simple way to capitalize a string in Sql Server (T-SQL)

If your need is just in output, use this:

SELECT UPPER(LEFT(ColumnName,1)) + LOWER(SUBSTRING(ColumnName,2,LEN(ColumnName))) FROM TableName


If instead you need to modify and save values into db:

UPDATE ColumnName SET ColumnName=UPPER(LEFT(ColumnName,1)) + LOWER(SUBSTRING(ColumnName,2,LEN(ColumnName)))