The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for November, 2012

Interesting: site “Your Online Choices | IAB” seems to help in disabling tracking cookies for advertisers

Posted by jpluimers on 2012/11/16

Is there anyone that has experience with the Your Online Choices | IAB site?

They seem to be able to turn off cookie tracking for selected advertisers.

I’m anxious to hear if this scam or not.

–jeroen

Posted in LifeHacker, Opinions | Leave a Comment »

Some notes on finding the cause of a .NET app generating a “application has generated an exception that could not be handled”

Posted by jpluimers on 2012/11/15

A while ago, one of the users at a client got an error in a .NET 1.1 app of which the sources were not readily available:

“application has generated an exception that could not be handled”

I think it is a e0434f4d  exception.

This particular site has very strict rules about what you can and cannot do as a developer. Which means that on a production system, you basically cannot do anything.

A few links that should help me finding the solution, and escalate far enough upstream to get someone with local admin rights to assist me:

If WinDbg is allows to be ran, these should help me:

–jeroen

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

Interesting: Visual Studio Project Renamer | ComVisible

Posted by jpluimers on 2012/11/14

Over the course of development time, each suite of projects is bound to get some renames.

Doing that from the Visual Studio IDE is a pain, so I was glad to find Visual Studio Project Renamer by ComVisible.

Though it only supports C# and VB.NET projects (so no solution rename or rename of F#, Database or Reporting Service projects, nor stuff outside of the Microsoft realm like Prism).

These Just geeks: Renaming a Visual Studio Project link led me to the project.

Renaming solutions still is largely a manual operation as it involves renaming directories. You have to re-add some (sometimes all) projects later where this tool can come in handy: CoolCommands by SharpToolbox.

–jeroen

via:

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, F#, Prism, Software Development, VB.NET, Visual Studio and tools | Leave a Comment »

.NET/C#: Cleaning up path names with invalid characters (via: validation – C# Sanitize File Name – Stack Overflow)

Posted by jpluimers on 2012/11/13

Thanks Andre for this cleanup code:

To clean up a file name you could do this

private static string MakeValidFileName(string name)
{
string invalidChars = Regex.Escape( new string( Path.GetInvalidFileNameChars() ) );
string invalidReStr = string.Format( @"[{0}]+", invalidChars );
return Regex.Replace( name, invalidReStr, "_" );
}

Next to GetInvalidFileNameChars, you have GetInvalidPathChars.

–jeroen

via: validation – C# Sanitize File Name – Stack Overflow.

Posted in .NET, C#, Development, Software Development | 2 Comments »

IIS on Windows 7: installing and opening the firewall

Posted by jpluimers on 2012/11/12

Usually, I run IIS only on server systems, but for the occasional time when I need it on a (development) workstation (as Cassinni only listens to localhost), these links come in handy:

  1. Installing IIS 7 on Windows Vista and Windows 7 : Installing IIS 7 : Installing and Configuring IIS : The Official Microsoft IIS Site.
    This helps you setup IIS 7, and make sure ASP.NET works on it
  2. Allowing Remote Connection in IIS on Windows 7 – Super User.
    This helps you open up the firewall to access IIS over http port 80.
  3. Make sure if you add virtual directories or applications, that they are based on Physical Paths. When not (for instance with a subst path), you will get an error like this:
    <<
    —————————
    Add Virtual Directory
    —————————
    The specified directory does not exist on the server.
    —————————
    OK
    —————————
    >>
  4. For ASP.NET applications, when creating a virtual directory or application (especially outside the C:\inetpub\wwwroot realm), make sure the rights are set correctly.
    The IIS configuration will warn you when testing a new virtual directory:
    <<
    The server is configured to use pass-through authentication with a built-in account to access the specified physical path. However, IIS Manager cannot verify whether the built-in account has access. Make sure that the application pool identity has Read access to the physical path. If this server is joined to a domain, and the application pool identity is NetworkService or LocalSystem, verify that <domain>\<computer_name>$ has Read access to the physical path. Then test these settings again.
    >>
    When in doubt, check out the rights set to C:\inetpub\wwwroot and use that as a point to get started.
    Usually the user is IIS_USRS, and the minimum rights looks like read+execute, but in fact is excute+read-data+read-attributes+read-extended-attributes.

–jeroen

Posted in Development, IIS, Software Development, Web Development | 2 Comments »

Some interesting USB devices to add more than 2 monitors to your PC or Mac (via: USB Graphics – Graphic solutions GeForce & Radeon)

Posted by jpluimers on 2012/11/09

Thanks to Matthijs ter Woord who pointed me to these.

They require a single Intel®, Nvidia®, or AMD® primary WDDM driver. That driver does the actual rendering, the USB device then gets the rendered parts over USB to the monitor.

The really cool thing is: they work on a PC with Windows XP and higher, and on  Mac with OS X Tiger or better.

The chipsets are based on DisplayLink technology; they have their own USB devices as well.

USB2.0 to DVI-I Graphics

CSV-2000D – SenseVision USB Graphics – USB2.0 to DVI-I

USB2.0 to DVI-I graphics devices let you easily add an additional monitor to your notebook PC, desktop and MacBook®. The Club 3D SenseVision USB2.0 to DVI-I Graphics allows you to extend your desktop display beyond 1080p HD resolution displays. … View Details

USB2.0 to HDMI Graphics

CSV-2000H – SenseVision USB Graphics – USB2.0 to HDMI

USB2.0 to HDMI graphics devices let you easily add an additional monitor to your notebook PC, desktop and MacBook®. The Club 3D SenseVision USB2.0 to HDMI Graphics allows you to extend your desktop display beyond 1080p HD resolution displays. … View Details

–jeroen

via: USB Graphics – Graphic solutions GeForce & Radeon.

Posted in Apple, Mac, Mac OS X / OS X / MacOS, Mac OS X 10.4 Tiger, Mac OS X 10.5 Leopard, Mac OS X 10.6 Snow Leopard, Mac OS X 10.7 Lion, MacBook, MacBook-Air, MacBook-Pro, Power User, Windows, Windows 7, Windows 8 | Leave a Comment »

.NET/C#: Drives, Directories, Paths and Filenames in Windows is hard, let alone cross platform (: via: .net – Why Path.Combine doesn’t add the Path.DirectorySeparatorChar after the drive designator? – Stack Overflow)

Posted by jpluimers on 2012/11/08

Handling names of drives and paths (directories, filenames) is hard in Windows, as both C:myfile.ext and C:\myfile.ext are valid – but potentially different – filenames, C is a valid driveletter, C: and C:\ are valid – but also potentially different – directory names.

This leads into confusion as how Path.Combine behaves.

Part of the confusion comes from the meaning of the absence or presence of the leading DirectorySeparatorChar as explained by user Peter van der Heijden:

C:filename is a valid path and is different from C:\filename. C:filename is the file filename in the current directory on the C: drive whereas C:\filename is the file filename in the root of that drive. Apparently they wanted to keep the functionality of refering to the current directory on some drive.

This behaviour is described here in MSDN

Another oddity is that Path.Combine will only use the drive portion of the left argument when the right argument contains an absolute path.

If you understand the above, then dealing with cross platform directory and path separators, spaces in filenames and UNC path names are peanuts (:

–jeroen

via: .net – Why Path.Combine doesn’t add the Path.DirectorySeparatorChar after the drive designator? – Stack Overflow.

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

Wally and testing the new 3-D printer – via: Dilbert comic strip for 11/05/2012

Posted by jpluimers on 2012/11/07

Once every while a Dilbert strip is so great, you *have* to re-publish it.

So here it goes: Wally and testing the new 3-DD printer.

Wally and testing the new 3-D printer (Dilbert comic strip for 11/05/2012 from the official Dilbert comic strips archive.)

–jeroen

via: Dilbert comic strip for 11/05/2012 from the official Dilbert comic strips archive..

Posted in Comics | Leave a Comment »

.NET/C#: SqlClient ConnectionString keys and their equivalences

Posted by jpluimers on 2012/11/07

A while ago I needed to shorten SqlClient ConnectionStrings. One way to do that is to use the shortest Key for each property (and not use the default key names that are much longer).

I beefed up the code to show you both the shortest and all equivalent keys (a few of the Microsoft exams want you to memorize most of these).

The HTML table below (similar to the huge and therefore hard to read table on MSDN) comes directly from the C# code at the bottom of the post. The only post-editing I did was making the header row bold.

Key ShortesEquivalentKey EquivalentKeys
Application Name app Application Name,app
ApplicationIntent ApplicationIntent ApplicationIntent
Asynchronous Processing async Asynchronous Processing,async
AttachDbFilename AttachDbFilename AttachDbFilename,extended properties,initial file name
Connect Timeout timeout Connect Timeout,connection timeout,timeout
Connection Reset Connection Reset Connection Reset
Context Connection Context Connection Context Connection
Current Language language Current Language,language
Data Source addr Data Source,addr,address,network address,server
Encrypt Encrypt Encrypt
Enlist Enlist Enlist
Failover Partner Failover Partner Failover Partner
Initial Catalog database Initial Catalog,database
Integrated Security trusted_connection Integrated Security,trusted_connection
Load Balance Timeout connection lifetime Load Balance Timeout,connection lifetime
Max Pool Size Max Pool Size Max Pool Size
Min Pool Size Min Pool Size Min Pool Size
MultipleActiveResultSets MultipleActiveResultSets MultipleActiveResultSets
MultiSubnetFailover MultiSubnetFailover MultiSubnetFailover
Network Library net Network Library,net,network
Packet Size Packet Size Packet Size
Password pwd Password,pwd
Persist Security Info persistsecurityinfo Persist Security Info,persistsecurityinfo
Pooling Pooling Pooling
Replication Replication Replication
Transaction Binding Transaction Binding Transaction Binding
TrustServerCertificate TrustServerCertificate TrustServerCertificate
Type System Version Type System Version Type System Version
User ID uid User ID,uid,user
User Instance User Instance User Instance
Workstation ID wsid Workstation ID,wsid

The code below uses a few techniques referenced as StackOverflow links:

  1. Sorting enumerable strings using LINQ.
  2. Generating CSV from an enumerable strings using LINQ and string.Join.
  3. Converting a DataTable to an HTML Table using an ASP.NET DataGrid and HtmlTextWriter to do the rendering.
  4. Getting a private static field by name using reflection.

Both the main program and the SqlConnectionStringBuilderHelper class are less than 70 lines of code (each about 50 when excluding comments and empty lines).

The SqlConnectionStringBuilderHelper uses the internals of the SqlConnectionStringBuilder class (all DbConnectionStringBuilder descendants I have seen work in a similar way):

  • each DbConnectionStringBuilder instance has a public Keys property that exposes the static _validKeywords field of the descendant. It contains all possible keys that can appear in a generated ConnectionString.
  • the SqlConnectionStringBuilder class (and other DbConnectionStringBuilder descendants) has a static private property _keywords that maps all possible keyword strings (including equivalents) to an enumerator (which indexes into the Keys property).
    Mono uses the same mechanism.
  • The trick is to walk the _keywords property and search for equivalent keywords.
  • For a list of equivalent keywords, you find the shortest one.

Related:

Enjoy the code: Read the rest of this entry »

Posted in .NET, ASP.NET, C#, C# 3.0, C# 4.0, C# 5.0, CSV, Development, LINQ, Software Development | Leave a Comment »

C# Code fragments of the week

Posted by jpluimers on 2012/11/06

Boy, was I astonished to see the code fragments below in production apps. It is far more Daily WTF than Coding Horror.

However, it did make debugging the production problem at hand a lot worse than it should be.

First a few short pieces:

        private void method(Word.Application objNewDoc, string stringWithCsvSeparatedDotFileNames)
        {
            char c = char.Parse(&quot;,&quot;);
            string[] wordAddIns = stringWithCsvSeparatedDotFileNames.ToString().Split(c);
        }

It took me almost a minute to understand what happened here. Especially because the names of parameters actually were pretty meaningless.

                foreach (string sFilename in attachments)
                {
                    Word.Application mailDocument = new Word.Application();

                    string[] filePath = sFilename.Split('\\');

                    string tempDirectory = GetTempDirectoryFromConfigFile();
                    object fileName = tempDirectory + filePath[filePath.Length - 1];

                    File.Copy(sFile, (string)fileName, true);
                    // some code that actually does something with the attachment
                }

It took me more than a few minutes to realize that:

  1. The tempDirectory needs to end with a backslash
  2. mailDocument (not a document, see below), will stay alive when File.Copy(…) throws an exception.
        internal virtual bool Method(/* parameters */, Word.Application objDoc)
        {
            // lots of pre-initialized empty string variables that are used at the very end of the method

            Word.Application objNewDoc;
            if (objDoc != null)
            {
                objNewDoc = objDoc;
            }
            else
            {
                objNewDoc = new Word.Application();
            }

            // lots of Object variables for COM, including:
            Object missing = Missing.Value;
            Object bFalse = false;

            try
            {
                // lots of code that does not use objNewDoc
            }
            catch (IOException IOex)
            {
                objNewDoc.Quit(ref bFalse, ref missing, ref missing);
                objNewDoc = null;
                throw IOex;
            }
            catch (Exception ex)
            {
                objNewDoc.Quit(ref bFalse, ref missing, ref missing);
                objNewDoc = null;
                throw new Exception(&quot;Some error message.&quot;, ex);
            }
            finally
            {
                // empty finally block
            }

            try
            {
                // actual coder that does use objNewDoc
            }
            catch (Exception ex)
            {
                objNewDoc.Quit(ref bFalse, ref missing, ref missing);
                objNewDoc = null;
                throw new Exception(&quot;Some error message.&quot;, ex);
            }
            return true;
        }

I rewrote the whole piece into separate methods.

Luckily the person who wrote this got promoted away from programming a few years ago.

–jeroen

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »