Page 1 of 1

Setting empty string to null automatically

Posted: Tue 26 Jan 2010 10:51
by hughespa
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.

Posted: Tue 26 Jan 2010 19:49
by tobias_cd
I'd set the default values to null on the DB-level, not application level.

Posted: Wed 27 Jan 2010 05:10
by hughespa
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.

Posted: Wed 27 Jan 2010 09:08
by Plash
Please find the following line in SQLServerUniProvider.pas:

Code: Select all

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

Posted: Wed 27 Jan 2010 09:38
by hughespa
Hi Plash,

Thanks for the info, that works just fine now.

Regards, Paul.