Strongly typed CUSTOM Dataset DBNull handling vs generated dataset

Strongly typed CUSTOM Dataset DBNull handling vs generated dataset

Post by Mark Scot » Fri, 16 May 2003 04:34:22



When I use a dataset generated from a .xsd file, and bind it to a DataGrid,
everything works fine.  Datagrid goes through the fields, and displays them,
and if it finds a DBNull it just displays a blank cell, even in cases where
you are talking about Integer, date, and other fields.  I know how to tell
the generated dataset to return String.Empty when it encounters a DBNull,
but that doesn't apply to strongly typed fields such as integer and
DateTime, etc.

I have a custom collection inheriting from the CollectionBase class, I can
bind it to the (web) DataGrid and it works fine until it gets a null value,
at which point it still calls my property accessors, which should return an
Int value, generating exception on a null value.  However, if I bind the
generated dataset to the Datagrid it works fine.  It doesn't appear that
when I bind the dataset directly that it even CALLS the datarow's accessors,
or calls the IsXYZNull() function to see if it is null.

Does anybody know at all what the DataRow/DataTable objects do that allow
the DataGrid to bind to it, and not call those accessors, thus allowing it
to always return String.Empty?  In doing research (and looking at mono.net
source code) it appears that since it inherits the IListSource Interface,
DataGrid calls GetList() which returns a DataView of itself.  Its not very
easy to track what exactly is happening though, since the VS.NET de*
can't step through any of the datarow/datatable code, so I can watch what
its doing).

Originally what I hoped would happen is that the DataGrid would call a
generic accessor, perhaps the Item, with a string value for the column, and
inside there if I saw DBNull I could return String.Empty, and the DataGrid
could just display that.

I've read through as many messages as I could find through google groups
that are remotely related to this issue, and I have anticipated some of the
questions/responses I will get

1a) Use annotations to make the underlying dataset return String.Empty
1b) This is not feasible with non-string fields

2a) Using the annotations, return a default integer value, such as zero
2b) In my database there is a huge difference between 0 and Null, putting 0
in the display would tell the user something that is highly incorrect

3a) Return an exception
3b) This already occurs, and is what I wish to avoid

4a) Return null
4b) This would cause an exception when it tried to cast a null as an Int32

If there is anybody that can shed light on this area of DataBinding to the
System.Web.UI.WebControls.DataGrid object, I would be eternally grateful, as
I have been fighting this issue for 2 weeks.

Thanks!

Mark E. Scott Jr.

 
 
 

Strongly typed CUSTOM Dataset DBNull handling vs generated dataset

Post by Ross Donal » Fri, 16 May 2003 13:23:50


Hi Mark,

I looked at this same problem a few days ago and went throught the same set
of questions as a part of implementing a strongly-typed DataReader.

I found that the DataTable class implements the IEnumerable interface and
the GetEnumerator() method for the DataTable returns a special enumerator
that handles DBNull without throwing exceptions.

I tested with a strongly-typed DataSet and found that the strongly-typed
properties were not called. Instead the binding name was directly matched
against a column name in the DataTable.

If you have an object that inherits from DataTable and want to implement
binding to grids you need to implement the IEnumerable interface and add the
following code.

  // C#
  public System.Collections.IEnumerator GetEnumerator()
  {
   //
   // Create a new DbEnumerator and set it to close automatically
   // after iterating through the rowset
   //
   return new DbEnumerator(this, true);
  }

To deal with nulls i added an IsMyColumnNull() method for each column that
could be null consistent with the ms auto-generated strongly-typed DataSet.

HTH

Ross Donald
RAD Software
http://www.veryComputer.com/


> When I use a dataset generated from a .xsd file, and bind it to a
DataGrid,
> everything works fine.  Datagrid goes through the fields, and displays
them,
> and if it finds a DBNull it just displays a blank cell, even in cases
where
> you are talking about Integer, date, and other fields.  I know how to tell
> the generated dataset to return String.Empty when it encounters a DBNull,
> but that doesn't apply to strongly typed fields such as integer and
> DateTime, etc.

> I have a custom collection inheriting from the CollectionBase class, I can
> bind it to the (web) DataGrid and it works fine until it gets a null
value,
> at which point it still calls my property accessors, which should return
an
> Int value, generating exception on a null value.  However, if I bind the
> generated dataset to the Datagrid it works fine.  It doesn't appear that
> when I bind the dataset directly that it even CALLS the datarow's
accessors,
> or calls the IsXYZNull() function to see if it is null.

> Does anybody know at all what the DataRow/DataTable objects do that allow
> the DataGrid to bind to it, and not call those accessors, thus allowing it
> to always return String.Empty?  In doing research (and looking at mono.net
> source code) it appears that since it inherits the IListSource Interface,
> DataGrid calls GetList() which returns a DataView of itself.  Its not very
> easy to track what exactly is happening though, since the VS.NET de*
> can't step through any of the datarow/datatable code, so I can watch what
> its doing).

> Originally what I hoped would happen is that the DataGrid would call a
> generic accessor, perhaps the Item, with a string value for the column,
and
> inside there if I saw DBNull I could return String.Empty, and the DataGrid
> could just display that.

> I've read through as many messages as I could find through google groups
> that are remotely related to this issue, and I have anticipated some of
the
> questions/responses I will get

> 1a) Use annotations to make the underlying dataset return String.Empty
> 1b) This is not feasible with non-string fields

> 2a) Using the annotations, return a default integer value, such as zero
> 2b) In my database there is a huge difference between 0 and Null, putting
0
> in the display would tell the user something that is highly incorrect

> 3a) Return an exception
> 3b) This already occurs, and is what I wish to avoid

> 4a) Return null
> 4b) This would cause an exception when it tried to cast a null as an Int32

> If there is anybody that can shed light on this area of DataBinding to the
> System.Web.UI.WebControls.DataGrid object, I would be eternally grateful,
as
> I have been fighting this issue for 2 weeks.

> Thanks!

> Mark E. Scott Jr.



 
 
 

Strongly typed CUSTOM Dataset DBNull handling vs generated dataset

Post by Mark Scot » Sat, 17 May 2003 06:48:54


Thanks for your reply.  I'm actually trying to avoid inheriting from the
DataTable and DataRow classes because they have functionality that I wish to
hide, and there is no way that I'm aware of to hide all of those functions
in one line, I would have to make them all private in the inherited class,
in which case they could still cast it as a DataTable and get to those
functions.

I have a Collection object:
public class EpisodeCollection : CollectionBase
 {
  public Episode this[int index]
  {
   get
   {
    return (Episode)List[index];
   }
   set
   {
    List[index] = value;
   }
  }

  public int Add(Episode value)
  {
   if (value != null)
   {
    return List.Add(value);
   }
   else
   {
    return -1;
   }
  }

  public void Insert(int index, Episode value)
  {
   if (value != null)
   {
    List.Insert(index, value);
   }
  }
 }

All it does is use the CollectionBase class to implement storage of Episode
objects, and adds a few typed functions to return Episode objects.  The
Episode object is where I have the actual data stored,
and I don't have it inheriting from anything, and wouldn't want to inherit
from a specialized DataRow object unless I could hide all the members except
those specifically that I want to give access to.

I tried overriding the GetEnumerator method that I was inheriting from
CollectionBase, but in setting a breakpoint there, the GetEnumerator
function never gets called.  Things would be so much easier if I could step
through what actually happens when DataGrid.DataBind() was called.

Thanks,

Mark E. Scott Jr.


> Hi Mark,

> I looked at this same problem a few days ago and went throught the same
set
> of questions as a part of implementing a strongly-typed DataReader.

> I found that the DataTable class implements the IEnumerable interface and
> the GetEnumerator() method for the DataTable returns a special enumerator
> that handles DBNull without throwing exceptions.

> I tested with a strongly-typed DataSet and found that the strongly-typed
> properties were not called. Instead the binding name was directly matched
> against a column name in the DataTable.

> If you have an object that inherits from DataTable and want to implement
> binding to grids you need to implement the IEnumerable interface and add
the
> following code.

>   // C#
>   public System.Collections.IEnumerator GetEnumerator()
>   {
>    //
>    // Create a new DbEnumerator and set it to close automatically
>    // after iterating through the rowset
>    //
>    return new DbEnumerator(this, true);
>   }

> To deal with nulls i added an IsMyColumnNull() method for each column that
> could be null consistent with the ms auto-generated strongly-typed
DataSet.

> HTH

> Ross Donald
> RAD Software
> http://www.veryComputer.com/



> > When I use a dataset generated from a .xsd file, and bind it to a
> DataGrid,
> > everything works fine.  Datagrid goes through the fields, and displays
> them,
> > and if it finds a DBNull it just displays a blank cell, even in cases
> where
> > you are talking about Integer, date, and other fields.  I know how to
tell
> > the generated dataset to return String.Empty when it encounters a
DBNull,
> > but that doesn't apply to strongly typed fields such as integer and
> > DateTime, etc.

> > I have a custom collection inheriting from the CollectionBase class, I
can
> > bind it to the (web) DataGrid and it works fine until it gets a null
> value,
> > at which point it still calls my property accessors, which should return
> an
> > Int value, generating exception on a null value.  However, if I bind the
> > generated dataset to the Datagrid it works fine.  It doesn't appear that
> > when I bind the dataset directly that it even CALLS the datarow's
> accessors,
> > or calls the IsXYZNull() function to see if it is null.

> > Does anybody know at all what the DataRow/DataTable objects do that
allow
> > the DataGrid to bind to it, and not call those accessors, thus allowing
it
> > to always return String.Empty?  In doing research (and looking at
mono.net
> > source code) it appears that since it inherits the IListSource
Interface,
> > DataGrid calls GetList() which returns a DataView of itself.  Its not
very
> > easy to track what exactly is happening though, since the VS.NET
de*
> > can't step through any of the datarow/datatable code, so I can watch
what
> > its doing).

> > Originally what I hoped would happen is that the DataGrid would call a
> > generic accessor, perhaps the Item, with a string value for the column,
> and
> > inside there if I saw DBNull I could return String.Empty, and the
DataGrid
> > could just display that.

> > I've read through as many messages as I could find through google groups
> > that are remotely related to this issue, and I have anticipated some of
> the
> > questions/responses I will get

> > 1a) Use annotations to make the underlying dataset return String.Empty
> > 1b) This is not feasible with non-string fields

> > 2a) Using the annotations, return a default integer value, such as zero
> > 2b) In my database there is a huge difference between 0 and Null,
putting
> 0
> > in the display would tell the user something that is highly incorrect

> > 3a) Return an exception
> > 3b) This already occurs, and is what I wish to avoid

> > 4a) Return null
> > 4b) This would cause an exception when it tried to cast a null as an
Int32

> > If there is anybody that can shed light on this area of DataBinding to
the
> > System.Web.UI.WebControls.DataGrid object, I would be eternally
grateful,
> as
> > I have been fighting this issue for 2 weeks.

> > Thanks!

> > Mark E. Scott Jr.


 
 
 

Strongly typed CUSTOM Dataset DBNull handling vs generated dataset

Post by Ross Donal » Sat, 17 May 2003 18:16:13


If you implement IEnumerable on your EpisodeCollection instead of trying to
override the base class you might have more success. You will need to create
a custom enumerator class that implements IEnumerator.

When i implemented IEnumerable and called DataGrid.DataBind() the
GetEnumerator() method was called.

--
Ross Donald
RAD Software
http://www.veryComputer.com/


> Thanks for your reply.  I'm actually trying to avoid inheriting from the
> DataTable and DataRow classes because they have functionality that I wish
to
> hide, and there is no way that I'm aware of to hide all of those functions
> in one line, I would have to make them all private in the inherited class,
> in which case they could still cast it as a DataTable and get to those
> functions.

> I have a Collection object:
> public class EpisodeCollection : CollectionBase
>  {
>   public Episode this[int index]
>   {
>    get
>    {
>     return (Episode)List[index];
>    }
>    set
>    {
>     List[index] = value;
>    }
>   }

>   public int Add(Episode value)
>   {
>    if (value != null)
>    {
>     return List.Add(value);
>    }
>    else
>    {
>     return -1;
>    }
>   }

>   public void Insert(int index, Episode value)
>   {
>    if (value != null)
>    {
>     List.Insert(index, value);
>    }
>   }
>  }

> All it does is use the CollectionBase class to implement storage of
Episode
> objects, and adds a few typed functions to return Episode objects.  The
> Episode object is where I have the actual data stored,
> and I don't have it inheriting from anything, and wouldn't want to inherit
> from a specialized DataRow object unless I could hide all the members
except
> those specifically that I want to give access to.

> I tried overriding the GetEnumerator method that I was inheriting from
> CollectionBase, but in setting a breakpoint there, the GetEnumerator
> function never gets called.  Things would be so much easier if I could
step
> through what actually happens when DataGrid.DataBind() was called.

> Thanks,

> Mark E. Scott Jr.



> > Hi Mark,

> > I looked at this same problem a few days ago and went throught the same
> set
> > of questions as a part of implementing a strongly-typed DataReader.

> > I found that the DataTable class implements the IEnumerable interface
and
> > the GetEnumerator() method for the DataTable returns a special
enumerator
> > that handles DBNull without throwing exceptions.

> > I tested with a strongly-typed DataSet and found that the strongly-typed
> > properties were not called. Instead the binding name was directly
matched
> > against a column name in the DataTable.

> > If you have an object that inherits from DataTable and want to implement
> > binding to grids you need to implement the IEnumerable interface and add
> the
> > following code.

> >   // C#
> >   public System.Collections.IEnumerator GetEnumerator()
> >   {
> >    //
> >    // Create a new DbEnumerator and set it to close automatically
> >    // after iterating through the rowset
> >    //
> >    return new DbEnumerator(this, true);
> >   }

> > To deal with nulls i added an IsMyColumnNull() method for each column
that
> > could be null consistent with the ms auto-generated strongly-typed
> DataSet.

> > HTH

> > Ross Donald
> > RAD Software
> > http://www.veryComputer.com/



> > > When I use a dataset generated from a .xsd file, and bind it to a
> > DataGrid,
> > > everything works fine.  Datagrid goes through the fields, and displays
> > them,
> > > and if it finds a DBNull it just displays a blank cell, even in cases
> > where
> > > you are talking about Integer, date, and other fields.  I know how to
> tell
> > > the generated dataset to return String.Empty when it encounters a
> DBNull,
> > > but that doesn't apply to strongly typed fields such as integer and
> > > DateTime, etc.

> > > I have a custom collection inheriting from the CollectionBase class, I
> can
> > > bind it to the (web) DataGrid and it works fine until it gets a null
> > value,
> > > at which point it still calls my property accessors, which should
return
> > an
> > > Int value, generating exception on a null value.  However, if I bind
the
> > > generated dataset to the Datagrid it works fine.  It doesn't appear
that
> > > when I bind the dataset directly that it even CALLS the datarow's
> > accessors,
> > > or calls the IsXYZNull() function to see if it is null.

> > > Does anybody know at all what the DataRow/DataTable objects do that
> allow
> > > the DataGrid to bind to it, and not call those accessors, thus
allowing
> it
> > > to always return String.Empty?  In doing research (and looking at
> mono.net
> > > source code) it appears that since it inherits the IListSource
> Interface,
> > > DataGrid calls GetList() which returns a DataView of itself.  Its not
> very
> > > easy to track what exactly is happening though, since the VS.NET
> de*
> > > can't step through any of the datarow/datatable code, so I can watch
> what
> > > its doing).

> > > Originally what I hoped would happen is that the DataGrid would call a
> > > generic accessor, perhaps the Item, with a string value for the
column,
> > and
> > > inside there if I saw DBNull I could return String.Empty, and the
> DataGrid
> > > could just display that.

> > > I've read through as many messages as I could find through google
groups
> > > that are remotely related to this issue, and I have anticipated some
of
> > the
> > > questions/responses I will get

> > > 1a) Use annotations to make the underlying dataset return String.Empty
> > > 1b) This is not feasible with non-string fields

> > > 2a) Using the annotations, return a default integer value, such as
zero
> > > 2b) In my database there is a huge difference between 0 and Null,
> putting
> > 0
> > > in the display would tell the user something that is highly incorrect

> > > 3a) Return an exception
> > > 3b) This already occurs, and is what I wish to avoid

> > > 4a) Return null
> > > 4b) This would cause an exception when it tried to cast a null as an
> Int32

> > > If there is anybody that can shed light on this area of DataBinding to
> the
> > > System.Web.UI.WebControls.DataGrid object, I would be eternally
> grateful,
> > as
> > > I have been fighting this issue for 2 weeks.

> > > Thanks!

> > > Mark E. Scott Jr.


 
 
 

1. How do you handle DBNull and strongly typed datasets?

I'm in the process of developing some data classes that
are very similar to strongly typed datasets and I'm
wondering how everyone else who's doing this handles NULL
values in their dataset? Obviously if you have something
like:

public int MyStringValue
{
        get
        {
                return Convert.ToInt32(this
["MyStringField"]);
        }

You're going to end up with an exception (DBNull will
throw the exception if cast to anything else) if the
value in MyStringField is null. So how does everyone else
handle this? Tons of if/else statements? Exceptions?

Thanks,

Paul

2. Mex via Borland 5.3

3. Strongly typed dataset - can't find strongly typed row type

4. Error message with Outlook: out of memory or system resources.

5. Experiences with DataReader \ DataSet \ Custom Strongly Type DataSet

6. DON'T MISS THIS DEAL!!

7. Automatically generating Strongly Typed Datasets in VS.Net

8. Boiling discs?

9. Strongly Typed DataSet, Date Field and DBNull

10. How are you handling DBNull exceptions in Typed Datasets?

11. Major Problem downcasting from DataSets to Strongly typed datasets across dlls

12. Problem Casting from Dataset to Strongly Typed Dataset

13. OO Question on DataSet and Strongly Typed DataSet