strange occurence in DataMapper - Src.value = nothing

strange occurence in DataMapper - Src.value = nothing

Old forum URL: forums.lhotka.net/forums/t/3262.aspx


Jav posted on Wednesday, July 25, 2007

csla 2.1.4

I have scores of WebForms that have been working just fine. Since yesterday I have run into a puzzeling problem.  A DetailsView gets data from a CslaDataSource.  Any fileds that are populated from the DB behave just fine, as do fields that were blank in the DB and are not touched in the DetailsView.  However, if I delete a populated TextBox's contents, the value of that field is being read back into the object as 'Nothing' (which generates an arror in my LoadParameters method which will accept an empty string but not Nothing)

If instead of completely deleting the text from the textbox, I replace a with a space character, everything works fine.

I am pretty sure it's I who is doing something wrong somewhere but nothing appears out of place to my eyes.  Would appreciate if anybody has an explanation for this weird behavior. BTW, all the text fields of my newly generated objects are always set to "" and this object is no exception.

TIA

jav

 

RockfordLhotka replied on Wednesday, July 25, 2007

Odds are you accidentally used a .NET string function instead of a VB one in your property set.

For example, this works:

Set(ByVal value As String)
  CanWriteProperty(...)
  If _name <> value Then
  ' ...

But this will fail:

Set(ByVal value As String)
  CanWriteProperty(...)
  If _name.Equals(value) Then
  ' ...

The first one allows VB to automatically convert Nothing to "", but the second one invokes a .NET function that goes boom.

Same with Len(x) vs x.Length and many other functions.

One answer is to follow the C# forumla for a setter:

Set(ByVal value As String)
  CanWriteProperty(...)
  If String.IsNullOrEmpty(value) Then value = ""
  If _name.Equals(value) Then
  ' ...

That de-nulls the inbound value and allows you to use VB or .NET functions as you choose.

Jav replied on Thursday, July 26, 2007

Thanks Rocky.  That is of great help.  I am surprised I didn't encounter this before.  I think I am going to start using your last suggestion as it appears to be the most bullet proof.

Jav

Copyright (c) Marimer LLC