Setting empty string to null automatically

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
hughespa
Posts: 81
Joined: Sat 23 Aug 2008 08:36
Location: W. Australia

Setting empty string to null automatically

Post by hughespa » Tue 26 Jan 2010 10:51

Hi,

D2010 > UniDAC > SQL Server 2008

Is there any way I can globally (in TUniConnection or TUniQuery) set it to force empty TWideString fields to nulls for posting to SQL Server (or other DBs).

I know I can set these at the field level before posting but I wanted to do it generally across the application.

If it's not possible, where should I look to amend the UniDAC source to achieve this effect please?

Regards, Paul.

tobias_cd
Posts: 56
Joined: Thu 18 Dec 2008 22:10

Post by tobias_cd » Tue 26 Jan 2010 19:49

I'd set the default values to null on the DB-level, not application level.

hughespa
Posts: 81
Joined: Sat 23 Aug 2008 08:36
Location: W. Australia

Post by hughespa » Wed 27 Jan 2010 05:10

Hi,

The problem there is, even with DEFAULT NULL on the DB fields, UniDAC sets the param value to '' (an empty string).

This is OK from the DB point of view since it's NOT NULL but also it doesn't cause the server to set it to NULL because it actually has a value.

What I was after was a way to set the param value used in an update command to NULL if it had a zero length string (after trimming, etc).

This is to stop users entering a few spaces or alternatively, deleting all characters from an existing field and posting '' to the DB.

I know the differences between NULL and '' but in this case, having a zero length string in the DB doesn't make sense.

I suppose I ought to add a CHECK contraint to ensure LEN(TRIM(field)) > 0

Regards, Paul.

Plash
Devart Team
Posts: 2844
Joined: Wed 10 May 2006 07:09

Post by Plash » Wed 27 Jan 2010 09:08

Please find the following line in SQLServerUniProvider.pas:

Code: Select all

    TOLEDBREcordSet(Obj).SetProp(prEnableEmptyStrings, True);
and replace True with False.

hughespa
Posts: 81
Joined: Sat 23 Aug 2008 08:36
Location: W. Australia

Post by hughespa » Wed 27 Jan 2010 09:38

Hi Plash,

Thanks for the info, that works just fine now.

Regards, Paul.

Post Reply