Forna Forna - 6 months ago 6
Vb.net Question

What if I have to handle a primary key that's equal to String.Empty in vb.net?

I have a Table in my VB.NET code that map an alias with his real name, with alias as a key.

Problem is that I have to handle the case of alias being an empty string and that causes an exception because

String.Empty
equals
Nothing
in VB.NET

How am I supposed to handle this case? Is there a way I could handle this without editing the underlying DB definition from something like this:

CREATE TABLE [tablename] (
[alias] NVARCHAR (100) NOT NULL,
[name] NVARCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([alias] ASC)
);


to something like this:

CREATE TABLE [tablename] (
[alias] NVARCHAR (100) NULL, UNIQUE
[name] NVARCHAR (100) NULL
);

Answer

Okay, string.Empty = Nothing equals True, string.Empty Is Nothing equals False.

Fiddle Here.

Don't use = to test strings for equality with Nothing.