Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Advice for assigning data from a DataReader to object properties

Reply
Thread Tools

Advice for assigning data from a DataReader to object properties

 
 
Froefel
Guest
Posts: n/a
 
      07-13-2007
Hi group

I am creating a web application that uses a simple DAL as an
ObjectDataSource.
To retrieve data from the database, I use a DataReader object from
which I then assign the various fields to properties in an object,
like so:

in the DB, the fields are defined as follows:
ProjectID int NOT NULL
Description varchar(50) NULL
Status int NULL
Active bit

My object looks like this:
public class Project
{
uint ProjectID;
string Description;
int Status;
bool Active;
}

Project p = new Project();
p.ProjectID = (uint) dr["ProjectID"];
p.Description = dr["Description"].ToString();
p.Status = (int) dr["Status"];
p.Active = (bool) dr["Active"];

I am experiencing a significant amount of problems when dealing with
nullable fields.
Because Description and Status are nullable, I have to check for that
prior to assigning it to the properties, otherwise an exception will
occur.

if (dr["Description"].ToString().Length > 0) p.Description =
dr["Description"].ToString();
if (dr["Status"].ToString().Length > 0) p.Status = (int) dr["Status"];


It seems to me that this is overly complex and very inefficient... Is
there a best practice that I'm missing to address these kind of
conversions and checks?

-- Hans

 
Reply With Quote
 
 
 
 
bhar
Guest
Posts: n/a
 
      07-13-2007

Hi,

I hope you get the idea.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim SelectStatement As String ' 2.
The SelectStatement Used for SQL
Dim NorthwindDataReader As SqlDataReader ' 3.
The Datareader that will return from the DataAccess Object
Dim Con As String =
System.Configuration.ConfigurationManager.AppSetti ngs("Northwind")
SelectStatement = "Select * From AccountsTable"

'Instantiate the Data Access Oject
Dim localSQLServer As New DataServer(Con)
'Call the runSQLDataSet Function that takes the SQL String, an
optional Table name and returns the DataSet
NorthwindDataReader =
localSQLServer.runSQLDataReader(SelectStatement)
'We can now populate the Grid with the returning DataSet's
DataTable
'DataGridView1.DataSource = NorthwindDataReader
'DataGridView1.DataBindings()

If NorthwindDataReader.Read = True Then

TextBox1.Text = NorthwindDataReader(0)
TextBox2.Text = NorthwindDataReader(1)
End If

Dim intCol As Integer
With NorthwindDataReader
If .HasRows Then
DataGridView1.Rows.Clear()
'Add column definition: FieldName, and ColumnName
For intCol = 0 To .FieldCount - 1
DataGridView1.Columns.Add(.GetName(intCol),
GetName(intCol))
Next
'Base column width on header text width
DataGridView1.AutoSizeColumnsMode = _
DataGridViewAutoSizeColumnsMode.ColumnHeader
While .Read
'Get row data as an Object array
Dim objCells(intCol) As Object
GetValues(objCells)
'Add an entire row at a time
DataGridView1.Rows.Add(objCells)
End While
End If
End With
End Sub



--
bhar
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      07-13-2007
the main problem is value types cannot be null. use the new nullable
type. then check for DBNull being returned.

public class Project
{
uint? ProjectID;
string Description;
int? Status;
bool? Active;
}

p.ProjectID = dr.IsDBull(dr.GetOrdinal("ProjectID") : null ? (uint)
dr["ProjectID"];


i'd write a helper routine

-- bruce (sqlwork.com)

Froefel wrote:
> Hi group
>
> I am creating a web application that uses a simple DAL as an
> ObjectDataSource.
> To retrieve data from the database, I use a DataReader object from
> which I then assign the various fields to properties in an object,
> like so:
>
> in the DB, the fields are defined as follows:
> ProjectID int NOT NULL
> Description varchar(50) NULL
> Status int NULL
> Active bit
>
> My object looks like this:
> public class Project
> {
> uint ProjectID;
> string Description;
> int Status;
> bool Active;
> }
>
> Project p = new Project();
> p.ProjectID = (uint) dr["ProjectID"];
> p.Description = dr["Description"].ToString();
> p.Status = (int) dr["Status"];
> p.Active = (bool) dr["Active"];
>
> I am experiencing a significant amount of problems when dealing with
> nullable fields.
> Because Description and Status are nullable, I have to check for that
> prior to assigning it to the properties, otherwise an exception will
> occur.
>
> if (dr["Description"].ToString().Length > 0) p.Description =
> dr["Description"].ToString();
> if (dr["Status"].ToString().Length > 0) p.Status = (int) dr["Status"];
>
>
> It seems to me that this is overly complex and very inefficient... Is
> there a best practice that I'm missing to address these kind of
> conversions and checks?
>
> -- Hans
>

 
Reply With Quote
 
Froefel
Guest
Posts: n/a
 
      07-17-2007
Hi Bruce,

I was hoping there was another way, but if the nullable data types are
what you're proposing, then that's what I'll go for.

I do have some more questions about it though... I think the answers
will very depending on whom I'm talking to, but that's just what I'm
after, so I can get an initial repository of paths to follow.

1. When using nullable types, would you recommend a best practice to
use nullable types only for the private fields inside the object
(those that get their data from the DB), and work with standard types
for the public properties?
For example:
private int? _status
public int Status
{
get
{
if (_status != DBNull)
return _status;
else
// return some default value
}
set
{
_status = value;
}
}

public void GetData()
{
//get DataReader object -- code omitted --
_status = (int?) dr["Status"];
}

2. what's your take on allowing NULL in the database? Thus far I've
always allowed it for fields that could be empty. But for fields that,
if empty, should take a default value, would it be better to set that
default value in the DB and set the field as NOT NULL. Or is it better
to provide the default value in the property set of the object class
(like in the example above)? I know there's no definitive answer to
this, but I'm looking for reasonings for and against nullable DB
fields.

-- Hans



On Jul 13, 6:00 pm, bruce barker <nos...@nospam.com> wrote:
> the main problem is value types cannot be null. use the new nullable
> type. then check for DBNull being returned.
>
> public class Project
> {
> uint? ProjectID;
> string Description;
> int? Status;
> bool? Active;
> }
>
> p.ProjectID = dr.IsDBull(dr.GetOrdinal("ProjectID") : null ? (uint)
> dr["ProjectID"];
>
> i'd write a helper routine
>
> -- bruce (sqlwork.com)
>
> Froefel wrote:
> > Hi group

>
> > I am creating a web application that uses a simple DAL as an
> > ObjectDataSource.
> > To retrieve data from the database, I use a DataReader object from
> > which I then assign the various fields to properties in an object,
> > like so:

>
> > in the DB, the fields are defined as follows:
> > ProjectID int NOT NULL
> > Description varchar(50) NULL
> > Status int NULL
> > Active bit

>
> > My object looks like this:
> > public class Project
> > {
> > uint ProjectID;
> > string Description;
> > int Status;
> > bool Active;
> > }

>
> > Project p = new Project();
> > p.ProjectID = (uint) dr["ProjectID"];
> > p.Description = dr["Description"].ToString();
> > p.Status = (int) dr["Status"];
> > p.Active = (bool) dr["Active"];

>
> > I am experiencing a significant amount of problems when dealing with
> > nullable fields.
> > Because Description and Status are nullable, I have to check for that
> > prior to assigning it to the properties, otherwise an exception will
> > occur.

>
> > if (dr["Description"].ToString().Length > 0) p.Description =
> > dr["Description"].ToString();
> > if (dr["Status"].ToString().Length > 0) p.Status = (int) dr["Status"];

>
> > It seems to me that this is overly complex and very inefficient... Is
> > there a best practice that I'm missing to address these kind of
> > conversions and checks?

>
> > -- Hans



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning Profile Properties When A User Registers With CreateUserWizard Nathan Sokalski ASP .Net 5 03-11-2009 05:33 PM
Class properties and object properties SuperZE Python 5 10-06-2008 02:14 PM
slicing - copying or assigning derived object to base object subramanian100in@yahoo.com, India C++ 1 04-22-2008 02:55 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
Problem assigning an Array object to an Array-subclass object Richard Lionheart Ruby 27 05-04-2004 06:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57