TypeDefaultValue not being returned

Posts   
 
    
Steve
User
Posts: 24
Joined: 20-Dec-2004
# Posted on: 17-Jun-2008 04:36:53   

After executing the following:

entEmail.Subject = "this seems to be a new issue with LLBLGen 2.5" entEmail.Subject = null,

entEmail.Subject still has the value of "this seems to be a new issue with LLBLGen 2.5"

TypeDefaultValue is not being called (breakpoint is not being hit).

The Subject field is a varchar(50) and does not allow Nulls.

project properties: ConvertNulledReferenceTypesToDefaultValue = True GenerateNullalbeFieldsAsNullableTypes = True

(however, this second setting shouldn't matter since the Subject field does not allow Nulls in the db anyway)

It's like the ConvertNulledReferenceTypesToDefaultValue flag being ignored or something.

Any suggestions?

config: LLBLGen Pro 2.5 with SQL Svr 2005 (have the same issue with LLBLGen Pro 2.6) SelfServicing target platform .NET 3.5 using the 2.0 runtimes

Thanks !

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Jun-2008 05:30:09   

Hi Steve, as the field isn't nullable, a null assignment is not allowed, isn't?

David Elizondo | LLBLGen Support Team
Steve
User
Posts: 24
Joined: 20-Dec-2004
# Posted on: 17-Jun-2008 18:03:13   

Thanks for the quick reply. You're right, null will not be allowed back into the db. But at runtime, I do need the value to be able to be set to null in memory, or at least be set to empty string from typedefaultvalue. Either would be ok. I was expecting null to be disallowed and typedefaultvalue to return string.empty.

Is this behavior intended, or am I missing something?

SetNewFieldValue() also produces same result.

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 17-Jun-2008 20:38:48   

Try using System.DBNull instead. You could as well follow the practical approach to this issue, just setting the value to String.Empty would do the work.

Steve
User
Posts: 24
Joined: 20-Dec-2004
# Posted on: 17-Jun-2008 21:38:56   

strings can't be set to DBNull.Value, ether directly or with SetNewFieldValue()

What is causing the value to be set to null is databinding in a .NET DataGridView, so it's not something that's happening programatically.

I need some way to allow

myEnt.Subject = null

to not be ignored.

LLBLGen didn't used to do this. Is this an intentional change in behavior?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 17-Jun-2008 23:04:18   

Steve wrote:

strings can't be set to DBNull.Value, ether directly or with SetNewFieldValue()

What is causing the value to be set to null is databinding in a .NET DataGridView, so it's not something that's happening programatically.

I need some way to allow

myEnt.Subject = null

to not be ignored.

LLBLGen didn't used to do this. Is this an intentional change in behavior?

What exactly do you mean 'didn't used to do this' ? Which version did allow you to set a non-nullable field to null?

Anyway, TypeDefaultValue is used when a value is retrieved from the field (this is a change in v2.5). Before the field was set to the default value, this isn't the case for some time already (since 2.5 at least, I've to look it up but it might be already since 2.0).

How exactly is the field set to null through databinding? You empty a cell and it is then passing null instead of an empty string to the field, which then fails? So you want to pre-process the value set to the field?

Frans Bouma | Lead developer LLBLGen Pro
Steve
User
Posts: 24
Joined: 20-Dec-2004
# Posted on: 17-Jun-2008 23:48:51   

I went back to an app using v2.0 to doublecheck. I was wrong about previous behavior. A property would only make use of TypeDefaultValue when attempting to set it to null if the field itself allowed nulls in the database.

To answer your databinding question, yes, apparently the .NET DataGridView passes null to the datasource property when the user simply clears the text from a cell. We previously used the ComponentOne grid, so we never ran into this problem.

So as it is now, when user attempts to clear the text from a cell and then leaves the cell, the prior value reappears. disappointed

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 18-Jun-2008 12:13:19   

Would you please attach a simple repro solution? (Better to be based on northwind)

Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 18-Jun-2008 20:31:16   

I think it also can be done with a CellFormatting event handler (which handles that event from the DataGridView), and in there, the value is checked, if it's null, it's changed to String.Empty.

Another solution might be an override of PreProcessValueToSet in a partial class of the entity. (see reference manual for details, is virtual method in EntityBase/EntityBase2 )

In there you can convert null to empty string if you want to, it then is set properly

Frans Bouma | Lead developer LLBLGen Pro
Steve
User
Posts: 24
Joined: 20-Dec-2004
# Posted on: 18-Jun-2008 21:21:33   

I like the CellFormatting idea. I'll give it a shot.

Thanks for the ideas and quick responses.

Steve
User
Posts: 24
Joined: 20-Dec-2004
# Posted on: 19-Jun-2008 19:51:49   

Unfortunately the CellFormatting event fires after the grid passes null to the bound entity collection.

The solution to this problem is actually to make use of the DefaultCellStyle.DataSourceNullValue property of the DatGridView columns (which isn't visible in the CellStyleBuilder in the designer):

foreach (DataGridViewColumn col in grid1.Columns) col.DefaultCellStyle.DataSourceNullValue = myLLBLGenProj.HelperClasses.TypeDefaultValue.GetDefaultValue(col.ValueType);

Thanks again for the suggestions. Your product is great.