Beyond the boundaries of ASP.Net development

Validating a Custom Server Control 

Friday, December 02, 2011 8:06:40 AM

If you need to make a custom server control target of ASP.NET Validation, just add the Attribute

[ValidationProperty("Text")]
public class CustomControl : CheckboxList
    { yadayada... }

to the class definiton of the control to tell the ASP.Net validation framework what value to validate...simple, huh? Just replace the "Text" in the live above with the Property of your custom control to be validated.

Microsoft Report Viewer generating chinese PDF 

Tuesday, September 27, 2011 4:56:12 AM

In case you have trouble rendering chinese characters in PDF reports using the Microsoft Report Viewer 2008: Either hack your way around the assembly or Upgrade to Microsoft Report Viewer 2010!

Script to Backup Zip and FTP SQL Server Database 

Thursday, September 22, 2011 4:48:34 PM

Here is a sql server script that makes compressed backups and moves them to an FTP. Quick and drity solution for the paranoic lone developer who's system just crashed...

There are only 2 preconditions: 7-zip installed and xp-cmdshell enabled (see this).

 

USE MASTER

CREATE procedure spBackupAndFtpDB
@FTPServer    varchar(128) ,
@FTPUser    varchar(128) ,
@FTPPWD        varchar(128) ,
@FTPPath    varchar(128) = '/' ,
@DBList            varchar(MAX),
@Delimiter    varchar(MAX) = ',',
@ZipBackup    bit = 1,
@DelteLocalFiles bit = 1,
@workdir    varchar(128) = 'C:\Windows\Temp\',
@7zipdir    varchar(255) = 'C:\Program Files\7-zip\'

as
    -- deal with special characters for echo commands
    select @FTPServer = replace(replace(replace(@FTPServer, '|', '^|'),'<','^<'),'>','^>')
    select @FTPUser = replace(replace(replace(@FTPUser, '|', '^|'),'<','^<'),'>','^>')
    select @FTPPWD = replace(replace(replace(@FTPPWD, '|', '^|'),'<','^<'),'>','^>')
    select @FTPPath = replace(replace(replace(@FTPPath, '|', '^|'),'<','^<'),'>','^>')
    
    declare    @cmd varchar(1000)
    declare @workfilename varchar(128)
    
    DECLARE @SourceFile varchar(255)
    
    create table #a (id int identity(1,1), s varchar(1000))
           

    --start loop
    DECLARE @CSVString varchar(MAX);
    SET @CSVString = @DBList

    DECLARE @pos INT;
    DECLARE @DB VARCHAR(MAX);

    SELECT @pos = 1;

    WHILE @pos!= 0
    BEGIN
        SET @pos = CHARINDEX(@Delimiter,@CSVString);
        IF @pos != 0
            SET @DB = LTRIM(RTRIM(LEFT(@CSVString, @pos - 1)));
        ELSE
            SET @DB = LTRIM(RTRIM(@CSVString));

        IF( LEN(@DB) > 0)
        BEGIN

            --backup    
            SET @SourceFile = @DB + CONVERT(VARCHAR(6), GETDATE(), 12) + '.bak'
            SELECT @cmd = 'BACKUP DATABASE [' +   @DB + '] TO  DISK = N''' + @workdir + @SourceFile + ''' WITH NOFORMAT, INIT,  NAME = N''' + @DB + '-Full Database Backup'', SKIP, NOREWIND, NOUNLOAD,  STATS = 100'
            EXEC (@cmd)



            --compress
            IF @ZipBackup = 1 BEGIN
                SELECT @cmd = '"' + @7zipdir +'7z" a ' + @workdir + @SourceFile + '.7z ' + @workdir + @SourceFile
                print @cmd
                insert #a
                exec master..xp_cmdshell @cmd
               
                IF @DelteLocalFiles = 1
                BEGIN
                    --remove backup
                    SELECT @cmd = 'del "' + @workdir + @SourceFile + '"'
                    print @cmd
                    insert #a
                    exec master..xp_cmdshell @cmd
                END
           
                --set zipped sourcefile
                SELECT @SourceFile = @SourceFile + '.7z'
            END
           
           

            --ftp
            select @workfilename = 'ftpcmd.txt'
           
            select    @cmd = 'echo '                    + 'open ' + @FTPServer
                    + ' > ' + @workdir + @workfilename
            exec master..xp_cmdshell @cmd
            select    @cmd = 'echo '                    + @FTPUser
                    + '>> ' + @workdir + @workfilename
            exec master..xp_cmdshell @cmd
            select    @cmd = 'echo '                    + @FTPPWD
                    + '>> ' + @workdir + @workfilename
            exec master..xp_cmdshell @cmd
            select    @cmd = 'echo '                    + 'put ' + @workdir + @SourceFile + ' ' + @FTPPath + @SourceFile
                    + ' >> ' + @workdir + @workfilename
            exec master..xp_cmdshell @cmd
            select    @cmd = 'echo '                    + 'quit'
                    + ' >> ' + @workdir + @workfilename
            exec master..xp_cmdshell @cmd
           
            print @cmd
            select @cmd = 'ftp -s:' + @workdir + @workfilename
           
            insert #a
            exec master..xp_cmdshell @cmd

           
--Not logged in.
--Login failed.
--File not found

            IF @DelteLocalFiles = 1
            BEGIN
                --remove backup
                SELECT @cmd = 'del "' + @workdir + @SourceFile + '"'
                print @cmd
                insert #a
                exec master..xp_cmdshell @cmd
            END
           
           
            --report results
            SELECT id, ouputtmp = s FROM #a WHERE s IS NOT NULL
           
            IF EXISTS(SELECT * FROM #a WHERE s LIKE '%Not logged in%' OR s LIKE '%Login failed%' OR s LIKE '%File not found%' OR s like '%WARNINGS for files%')
            BEGIN
                SET @cmd = 'An Error occured during Backup and FTP. Details: '
                SELECT @cmd = @cmd + '; ' + ISNULL(s, '') FROM #a WHERE s IS NOT NULL
                RAISERROR (@cmd, -- Message text.
                   16, -- Severity.
                   1 -- State.
                   );
            END
           

        END

        SET @CSVString = RIGHT(@CSVString, LEN(@CSVString) - @pos);
    END
    
    
    RETURN 0
    
go

DotNetCharting CacheKey generator and .IsCached Property 

Wednesday, July 06, 2011 8:23:30 AM

DotNetCharting Maps can be painfully slow when zooming and panning around a bit. Using the built in caching can be a good booster. Unfortunately there is no way to find out wether a chart already has been cached as the .FileManger.Cached Property does not work. After some dis-assembling I came up with this Extension simulating dotNetCharting's propreitary Hash algorithm to find out whether a chart is already in the cache so you can skip your data fetching etc and save some time:

Here is the code:

public bool Cached(this Chart c)
{
    string s = c.UniqueID + HttpContext.Current.Request.Path + HttpContext.Current.Request.QueryString.ToString();
    byte[] bytes = new UnicodeEncoding().GetBytes(s);
    byte[] value = ((System.Security.Cryptography.HashAlgorithm)
System.Security.Cryptography.CryptoConfig.CreateFromName(GetHash("첀잂낄", 15))).ComputeHash(bytes);

    string cachekey = BitConverter.ToString(value);
    return HttpContext.Current.Cache(cachekey) != null;
}

public static string GetHash(string InputString, int length)
{
    char[] array = InputString.ToCharArray();
    int num = 2127053169 + length;
    int arg_47_0;
    int arg_14_0;
    if ((arg_14_0 = (arg_47_0 = 0)) < 1)
    {
        goto IL_47;
    }
IL_14:
    int num2;
    int expr_14 = num2 = arg_14_0;
    char[] arg_44_0 = array;
    int arg_44_1 = num2;
    char expr_1B = array[num2];
    byte b = (byte)((int)(expr_1B & 'ÿ') ^ num++);
    byte b2 = (byte)((int)((int)expr_1B >> 8) ^ num++);
    byte arg_3C_0 = b2;
    b2 = b;
    b = arg_3C_0;
    arg_44_0[arg_44_1] = (char)((int)b2 << 8 | b);
    arg_47_0 = expr_14 + 1;
IL_47:
    if ((arg_14_0 = arg_47_0) >= array.Length)
    {
        return string.Intern(new string(array));
    }
    goto IL_14;
}

 

Custom SiteMapDataSource Menu 

Saturday, March 12, 2011 2:47:18 AM

I just ran into a problem with a weird historically grown ASP.NET website that has been originally written in ASP. Therefore little of the markup and code was REAL ASP.NET. For example <form runat="server"> tags were only rudimentary implemented. <asp:Menu> Control wouldn't work outside a <form> tag. So before messing with the site (as messy as it was - it worked) I created my own <ul> based menu control which was apparently very easy:
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SiteMapDataSource1" EnableViewState="false">
                                    <HeaderTemplate><ul id="menu" class="menu"></HeaderTemplate>
                                    <ItemTemplate>
                                        <li>
                                            <a class="menulink" href='<%#Eval("url")%>'><span><%#Eval("Title")%></span></a>
                                            <ul>
                                                <asp:Repeater ID="bar" DataSource='<%#Container.DataItem.ChildNodes()%>' runat="server">
                                                    <ItemTemplate>
                                                        <li><a class='<%# String.Format("{0}", IIF(Container.DataItem.HasChildNodes(), "sub", String.Empty)) %>' href='<%#Eval("url")%>'><span><%#Eval("Title")%></span></a>
                                                             <ul>
                                                                <asp:Repeater ID="bar" DataSource='<%#Container.DataItem.ChildNodes()%>' runat="server">
                                                                    <ItemTemplate>
                                                                        <li><a class='<%# String.Format("{0}", IIF(Container.DataItem.PreviousSibling() IS Nothing, "topline", String.Empty)) %>' href='<%#Eval("url")%>'><span><%#Eval("Title")%></span></a></li>
                                                                    </ItemTemplate>
                                                                </asp:Repeater>
                                                            </ul>
                                                        </li>
                                                    </ItemTemplate>
                                                </asp:Repeater>
                                            </ul>
                                        </li>
                                    </ItemTemplate>
                                    <FooterTemplate></ul></FooterTemplate>
                                </asp:Repeater>

Note: No Codebehind needed! And jsut add a little .js and .css found here and my menu was able to render without a <form> tag. Hooray. That saved the day!

Inline Select Comma Separated List (CSV) fom Table  

Tuesday, October 26, 2010 4:44:12 AM

in SQL Server without using a function as seen on Sql Authority...I just love it's simplicity

(SELECT STUFF((SELECT ', ' + Type FROM dbo.Reactor r FOR XML PATH('')),1,1,''))

"The custom tool 'MSLinqToSQLGenerator' failed. Unspecified error" 

Monday, October 18, 2010 3:26:11 AM

When you have a custom partial class attached to the LINQ2SQL file, Visual Studio will delete the corresponding ".designer.cs" file for your data context and nothing will compile after that point. You can either rename that file every time you make changes or
move the "using" statements of your custom partial class INSIDE the namespace declaration - and then it works!

namespace Data
{
    using System.Data.Linq.Mapping;
    using System.Data.Linq;
    using System.Reflection;

    partial class MyDataContext
    {

...

}

zh invariant chinese culture 

Friday, July 30, 2010 7:48:14 AM

I just ran into a problem with auto generated Resource Classes for .resx files in the App_GlobalResources folder. By default the runtime generates Proxy classes to access the content of neutral sample.resx files by code. If you add other languages (e.g. German like sample.de.resx) this file is recognized as a language specific version of the neutral file and no proxy is generated. This is fine.

Problem: If you add a sample.zh.resx for the Chinese version you get an error "...already contains a definition for..." because there is no invariant zh culture! So the runtime tries to generate a neutral proxy...for most languages there is an invariant version (http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28VS.71%29.aspx) but not for Chinese. You have to choose a specific culture instead. e.g. zh-CHS for traditional Chinese.

Checking .NET Framework version installed on webserver 

Monday, March 29, 2010 2:21:12 AM

To check your target web server environment for installed CLR versions you can use the follogin script which runs through the registry to determine the installes CLR versions on a host. Just paste the text below into a .aspx file. That' all.

<%@ Page Language="VB" %>
<%@ Import Namespace="Microsoft.Win32" %>

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim componentsKeyName As String = "SOFTWARE\Microsoft\NET Framework Setup\NDP"
        Dim componentsKey As RegistryKey = Registry.LocalMachine.OpenSubKey(componentsKeyName)
        Dim instComps As String() = componentsKey.GetSubKeyNames()
        For Each instComp As String In instComps
            Dim key As RegistryKey = componentsKey.OpenSubKey(instComp)
            If instComp IsNot Nothing AndAlso instComp.StartsWith("v") Then
                lblVersion.Text = lblVersion.Text & instComp & " SP " & key.GetValue("SP") & ", "
            End If
        Next
    End Sub
</script>

<html>
<head runat="server">   
    <title>CLR Versions</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="lblVersion" runat="server">Your server is running ASP.NET and the versions are: </asp:Label>
    </form>
</body>
</html>





 

Full Text Search for PersonalizationProvider 

Thursday, December 10, 2009 1:06:51 AM

With custom Personalization Providers it's easy to create your custom store (DB, XML, flat file, etc) eg. for editable Web Parts. But you get only a BLOB created by the WebPartManager to deal (store, retrieve, clear, etc) with and cannot read the actual content of the Personalization. This is why you cannot create a Full Text Search over personalized Web Pages easily. To achieve this you have to deserialize the WebPartManager's BLOB which format is kind of cryptic and not documented. So I used Lutz Roeder 's .Net Reflector before it was bought by Red Get sneaked into Microsoft's Code and extracted the relevant code for Serialization and Deserialization of these BLOBs.

Here is a schematic overview of what you can do with this Full Text Personalization Provider Framework. Download sample Project to support Full Text Search. All you need to do is implement your custom storage: CustomPersonalization.zip


 
Site Map | Printable View | Design by creative & it consulting | © 2009 - 2012 Florian Morrenth