DbValue and types

Posts   
 
    
Jackk100
User
Posts: 48
Joined: 11-Jan-2005
# Posted on: 23-Jul-2005 12:05:53   

Hi,

I'm converting some code from using DataTables to using LLBLGen-erated Entities. I'm working on converting this chunk of code:

(form.Properties is a Hashtable)


DataColumnCollection cols = ds.Tables[0].Columns;
foreach(DataColumn c in cols)
{
    if(!(results.IsNull(c.ColumnName)) && c.AutoIncrement != true)
    {
        form.Properties[c.ColumnName] = results[c.ColumnName];
    }
}

This is what I'm trying:

(survey is an LLBLGen-erated entity)


for(int i=0;i<survey.Fields.Count;i++)
{
    if (!survey.Fields[i].IsPrimaryKey)
    {
        form.Properties[survey.Fields[i].Name] = (! survey.Fields[i].IsNull) ? survey.Fields[i].DbValue : null;
    }
}

That code runs without error, but a problem occurs in the code that then accesses the data in the form.Properties Hashtable, which worked before my change. Basically, it seems like doing this:

= results[c.ColumnName];

vs doing this:

= survey.Fields[i].DbValue

causes a different type of object to be put in the hash, and the code that then uses that data doesn't like it and throws an error. I'm thinking I need to cast DbValue in some way, so that the type matches the expected type, but am not sure about that whole line of code in general.

Any ideas?

Thx, Jack

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 23-Jul-2005 12:15:45   

in a datatable, a NULL value is stored as System.DBNull.Value. In LLBLGen Pro, a null value is stored in DBValue as 'null' or 'nothing (vb.net)'.

Frans Bouma | Lead developer LLBLGen Pro
Jackk100
User
Posts: 48
Joined: 11-Jan-2005
# Posted on: 23-Jul-2005 19:27:41   

Hmm, not sure that solves the problem I'm trying to solve. You mean instead of:

(! survey.Fields[i].IsNull)

I should do:

survey.Fields[i].DbValue != null

? That part already seems to work OK (I think). The problem I'm having is ensuring that the oject types placed into the Hashtable are the correct type (the type the code that consumes the hash is already expecting). IE, the code that consumes the hash (which I did not write but have to use without modification) does stuff like:

Convert.ToInt32(myHash["surveyID"])

the hash get an object placed into from survey.Fields[i].DbValue, but the ToInt32 throws an error (or so it seems). I guess my question is mainly "do I need to identify the system type that the DbValue is made from and covert it to the system type, or is DbValue already going to be one of the system types (instead of a custom LLBLGen type).

Not sure I'm explaining it very well, since I don't have a full grip on what's causing the issue, but TIA.

  • Jack
Jackk100
User
Posts: 48
Joined: 11-Jan-2005
# Posted on: 23-Jul-2005 21:02:42   

More on this.... it looks like the issue I'm having is due to "default" objects/values that would have been put into the hash from:

= results[c.ColumnName]

IE, if the value of that was DBNull.Value, an object of a specific type with a "default" value would still be placed into the hash, which is now getting set to null, because I don't know what type and what default value to place into the hash where the DbBValue is null.

Making any sense?

thx, Jack

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 25-Jul-2005 10:05:09   

Jackk100 wrote:

Hmm, not sure that solves the problem I'm trying to solve. You mean instead of:

(! survey.Fields[i].IsNull)

I should do:

survey.Fields[i].DbValue != null

? That part already seems to work OK (I think). The problem I'm having is ensuring that the oject types placed into the Hashtable are the correct type (the type the code that consumes the hash is already expecting). IE, the code that consumes the hash (which I did not write but have to use without modification) does stuff like:

Convert.ToInt32(myHash["surveyID"])

the hash get an object placed into from survey.Fields[i].DbValue, but the ToInt32 throws an error (or so it seems). I guess my question is mainly "do I need to identify the system type that the DbValue is made from and covert it to the system type, or is DbValue already going to be one of the system types (instead of a custom LLBLGen type).

Not sure I'm explaining it very well, since I don't have a full grip on what's causing the issue, but TIA. - Jack

I too have a problem grasping what the problem is, but I think you want to store solely 'int32's (for example) into a hashtable or in a construct because the logic only works with ints, and now you're storing a null as well, which makes the logic fail and you're wondering how to prevent that, correct?

If so, I don't know. int's don't have a null value, neither do the other value types.

Frans Bouma | Lead developer LLBLGen Pro