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,839 other subscribers

Archive for the ‘Development’ Category

.NET/C#: DBNull explained

Posted by jpluimers on 2014/02/11

The DBNull type is a very special type in .NET. It represents null values in databases, which are slightly different than null values in .NET.

The biggest confusion that people have with it is that it won’t convert to anything. Which means that you see a lot of questions like “System.InvalidCastException: Conversion from type ‘DBNull’ to type” “is not valid”.

You’d think the full name would be System.Data.DBNull, it is actually named System.DBNull. The reason is that various other functionality of the System namespace depend on it, for instance the System.Convert class.

DBNull was already present in .NET 1.x, so it predates nullable types that were introduced in C# 2 / .NET 2.

A null value on the database side will result in a DBNull instance.

If you want to explicitly pass a null value to a database, you use a DBNull.Value, which is a singleton.

Why DBNull

There is a very interestin question/answer series on StackOverflow about this: via .net – What is the point of DBNull? – Stack Overflow.

There are a few good arguments both for and against DBNull.

But the baseline is that DBNull predates the introduction in the .NET framework of genuine nullable types. Both their behaviour is slightly different, so DBNull had to stay.

Which means you have to deal with it every now and then.

Invalid casts

A bit more background on the invalid casts.

It is thrown like this:

            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));

from

Convert.DefaultToType();

which is called from the DBNull method

    object IConvertible.ToType(Type type, IFormatProvider provider);

All other IConvertible methods are implementated like

bool IConvertible.ToBoolean(IFormatProvider provider)
{
    throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromDBNull"));
}

So these all throw the same exception:

    bool IConvertible.ToBoolean(IFormatProvider provider);
    byte IConvertible.ToByte(IFormatProvider provider);
    char IConvertible.ToChar(IFormatProvider provider);
    DateTime IConvertible.ToDateTime(IFormatProvider provider);
    decimal IConvertible.ToDecimal(IFormatProvider provider);
    double IConvertible.ToDouble(IFormatProvider provider);
    short IConvertible.ToInt16(IFormatProvider provider);
    int IConvertible.ToInt32(IFormatProvider provider);
    long IConvertible.ToInt64(IFormatProvider provider);
    sbyte IConvertible.ToSByte(IFormatProvider provider);
    float IConvertible.ToSingle(IFormatProvider provider);
    ushort IConvertible.ToUInt16(IFormatProvider provider);
    uint IConvertible.ToUInt32(IFormatProvider provider);
    ulong IConvertible.ToUInt64(IFormatProvider provider);

–jeroen

via:

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 »

Android: manually install Google Play Services 4.2.39 so AllCast and others can stream to Chromecast

Posted by jpluimers on 2014/02/09

Over the years, Google Play Services has started to play a much bigger role, arguably more important than the Android version (also making it impossible to fork Android: Neither Microsoft, Nokia, nor anyone else should fork Android. It’s unforkable. | Ars Technica).

This shows again with the publication of the new Google cast SDK with public Chromecast support release last weak (I think the SDK name being broader than Googlecast means there will be much more to come in the feature).

Though most current Android users are still at version 4.1.32 of the Google Play Services, Google is rolling out a 4.2.39 version to enable that SDK. After it has rolled out to enough devices, then the Play Services library version 15 part of the SDK will be released for Android as well (which will complement the existing support for the iOS and Chrome platforms).

The odd thing: it looks like *some* developers already have this SDK, as AllCast already has been updated and it does not work with Google Play Services 4.1.32, but it does work with Google Play Services version 4.2.39. Below I show you how I tested this, and how to manually upgrade your Android device to use Google Play Services 4.2.39. Read the rest of this entry »

Posted in Android, Android Devices, Chrome, Chromecast, Development, Google, iOS Development, Mobile Development, Nexus 4, Power User, Software Development | 1 Comment »

Jonas Bandi – Google+ – Blogged: Not happy with Agile, but why?

Posted by jpluimers on 2014/02/09

Yesterday there was this interesting post from Jonas Bandi – Google+ – Blogged: Not happy with Agile, but why?.

The content of his post is the opposite the title suggests: most developers love Agile, but it often is highly incompatible of management in bigger companies.

Well balanced post, much worth reading it.

–jeroen

Posted in Agile, Development, Software Development | Leave a Comment »

.NET/C#: When XmlSerializer throws “Unable to generate a temporary class (result=1).” / “error CS2001”

Posted by jpluimers on 2014/02/06

A while ago, I had this error when running an application on a hardened server:

Unhandled Exception:System.InvalidOperationException:
Unable to generate a temporary class (result=1).
error CS2001: Source file 'C:\windows\TEMP\0hocq2nq.0.cs' could not be found  error CS2008: No inputs specified
at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, String defaultNamespace, String location)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, Type[] extraTypes)

Usually I’m not the first with strange errors, but searching for “Unhandled Exception:System.InvalidOperationException: Unable to generate a temporary class” didn’t get many results.

This was a program running from SSIS under a non-system domain account with very little access.

My first guess was the right now: the XmlSerializer wants to generate a temporary C# file, then compile it into a temporary assembly. Since it cannot generate the C# file because the account does not have access to %windir\TEMP%, the compiler cannot find the (not generated) C# file.

After a few tries, I searched for XmlSerializer without GenerateAssembly, where the first hit ended at Changing where XmlSerializer Outputs Temporary Assemblies – Scott Hanselman.

That post indicated I should try looking for tempFilesLocation in the XmlSerializer context.

That got me these posts: Read the rest of this entry »

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 »

.NET/C#/VB.NET – Default value for generics (via: Stack Overflow)

Posted by jpluimers on 2014/02/05

Looking for the pesky little differences between C# and VB.NET, I stumbled over this nice question by Micah Martin on default values for generics in VB.NET as compared to C#. Actually there were 3 questions, so I did a bit of post-editing:

How do I create the default for a generic in VB.NET? in C# I can call:
T variable = default(T);

  1. How do I do this in VB?
  2. If this just returns null (C#) or nothing (VB.NET) then what happens to value types?
  3. Is there a way to specify for a custom type what the default value is? For instance what if I want the default value to be the equivalent to calling a parameterless constructor on my class.

User Konrad Rudolph – Stack Overflow. promptly gave three answers:

Question 1:

Dim variable As T ‘ or ‘ Dim variable As T = Nothing ‘ or ‘ Dim variable As New T() Notice that the latter only works if you specifiy either the New or the Structure constraint for the generic type.

Question 2:

For value types all members of the struct are “nulled” out, i.e. all reference type members are set to null (Nothing) and all value types are in turn nulled out. And no, since string is a reference type, it does not result in "" for strings as suggested in the other answer.

Question 3:

No, there’s no way to specify this. There are some threads about this on Stack Overflow already, e.g. here. Jon has posted an excellent explanation why this is.

–jeroen

via: c# – Default value for generics – Stack Overflow.

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, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0 | Leave a Comment »

How can you force SQL Server 2008 R2 to accept an ORDER BY on a column that is not part of a table? – Stack Overflow

Posted by jpluimers on 2014/02/04

I’m sure there are many organizations that only upgrade things until they absolutely have to (i.e. long after mainstream support has ended, often even after extended support has ended). This was from last year: upgrading away from SQL Server 2000 just before extended support ended. While migrating a bunch of applications we inherited from SQL Server 2000 to SQL Server 2008 R2, I came across an ORDER BY style that failed. The queries are generated by an kind of SQL generation layer, so not easy to change. the main questions were:

  • is it possible to force SQL Server 2008 R2 to accept this kind of queries and perform the SQL Server 2000 behaviour (so we can fix the SQL generation layer, and perform regression on it)?
  • why would SQL Server 2000 happily accept this kind of queries?

First two possible fixes, then the full stack overflow question I posted about the migration.

Aaron Bertrand very quickly posted two fixes, which I paraphrased and extended. Read the rest of this entry »

Posted in Database Development, Development, SQL Server, SQL Server 2000, SQL Server 2008 R2 | Leave a Comment »

Why the Heck is Git so Hard? | Probably Done Before

Posted by jpluimers on 2014/02/04

Why the Heck is Git so Hard? | Probably Done Before is wonderful, especially as it points to workspace • Git Cheatsheet • NDP Software.

And I really like this hacker news comment on it: Read the rest of this entry »

Posted in Development, DVCS - Distributed Version Control, git, Source Code Management | 2 Comments »

Ready to cast: Chromecast now open to developers with the Google Cast SDK #interesting #fun (via: Chromium Blog)

Posted by jpluimers on 2014/02/03

With a more decent internet connection during our trip, it is time to catch up on some recent news.

The first that caught my eye was that the Chromecast API now is open to all developers.

Developer information is available through Casting Your Content to the Big Screen – Google Cast — Google Developers.

You need to pay a one-time USD 5 fee per development account (how’s that Apple?) for the Google Cast SDK Developer Console.

After that, you will have to register your Chromecast device(s) and custom or styled media receiver apps.

Interesting. Fun.

I hope this can be combined with the new Smart Mobile Studio 2.0 release or the Delphi for Android support.

Will get back on that as soon as I’m back home.

Resources:

More links:

–jeroen

via: Chromium Blog: Ready to cast: Chromecast now open to developers with the Google Cast SDK.

Posted in Chrome, Chromecast, Delphi, Delphi XE5, Development, Google, JavaScript/ECMAScript, Pascal, Power User, Scripting, Smart Mobile Studio, Software Development | 1 Comment »

Hanselman’s Newsletter of Wonderful Things: December 17th, 2013 – Scott Hanselman

Posted by jpluimers on 2014/02/03

Always an interesting read, and usually posted to his blog a while after the email blast gets out.

There is too much information to fully re-post here, so here is an abstract of the entries I already had benefits from, or have a high interest in:

(BTW: don’t you love the that the bug report about WordPress.com adding backslashes in the “Press This” functionality still got no official WordPress response after 3 months?)

–jeroen

via: Hanselman’s Newsletter of Wonderful Things: December 17th, 2013 – Scott Hanselman.

Posted in Development, Power User, Software Development, Windows | Leave a Comment »

idank/explainshell: explainshell.com – match command-line arguments to their help text

Posted by jpluimers on 2014/02/03

This is brilliant for learning *nix shell programming:

explainshell.com – match command-line arguments to their help text

explainshell is a tool (with a web interface) capable of parsing man pages, extracting options and explain a given command-line by matching each argument to the relevant help text in the man page.

It has open source code at gitub too: idank/explainshell.

–jeroen

Posted in *nix, Development, Linux, Power User, Scripting, Sh Shell, Software Development, SuSE Linux | Leave a Comment »