Data field references ?

W

WJ

..Net Experts,

I have this concept described below:

1. I have a large (4K+) data record called Health_R. This record also
contains Text/memo fields and Image.

2. The record layout is below:
*******************
//My beloved c#
//
using System;

namespace Health.HealthRec
{
[Serializable]
public class Health_R
{
private string PatientName; //NVarChar(80)
private string PresentIllness; //ntext --Memo Field (Filled by
Nurses/Doctor
private string PatientPresentImage; //Image --Patient Picture
private string DOB; //DateTime

public Health_R()
{
}
}
}
*********************

3. In my Asp.Net form, I "new" the above structure as: private Health_R
ph=new Health_R();

4. I then read the data (say one record) into my DataSet.

5. I then go into a for loop to extract each field from the returned DataSet
as follow:

string fn="";
for(int i=0;i<DataSet.Tables[0].Columns.Count;i++)
{
fn=DataSet.Tables[0].Columns.ColumnName;
if(fn=="PatientName") //This is tough - I want to get rid of
this hard code
{
ph.PatientName=DataSet.Tables[0].Rows[0][fn].ToString();
}
else if...
}

***********

My question is: How do I assign each field to instance "ph" using variable
"fn" (contains the name of the database table column) as I could with the
DataSet.Tables[0].Rows[0][fn].ToString() without hardcoding all the columns
names ?

I really like this concept because I can do this at runtime without changing
the source codes in the event the database structure is changed.

Thanks for your help,

BTW: I read about "Reflection" but could not find any reference about it.

John
 
W

William F. Robertson, Jr.

I changed your record layout to this. You should have the types be what
they should be. That will ensure type safety. And it beats parsing strings
all the time.

public class HealthRecord
{
public string PatientName;
public string PresentIllness;
public byte [] PatientPresentImage;
public DateTime DOB;

public HealthRecord()
{
}
}

Here is the code snippet I used. I set it up so you could actually define
you HealthRecord in another assembly and still load it, but for now it will
call GetExecutingAssembly() because I defined them together.

public static void FillHealthRecord()
{
Assembly ass = Assembly.GetExecutingAssembly();
Type t = ass.GetType( "tempcsharp.HealthRecord" );
FieldInfo field = null;
HealthRecord record = null;

DataTable table = GetDataTable(); //or in your case a DataSet
foreach( DataRow row in table.Rows )
{
record = ( HealthRecord ) Activator.CreateInstance( t );
foreach( DataColumn column in table.Columns )
{
field = t.GetField( column.ColumnName );

//you might want to check the FieldType to ensure it is the same as you
DataColumn.
if ( field != null )
field.SetValue( record, row[column] );
}

Console.WriteLine( record.PatientName );
Console.WriteLine( record.PresentIllness );
Console.WriteLine( record.PatientPresentImage );
Console.WriteLine( record.DOB.ToShortDateString() );
}
}

HTH,

bill



WJ said:
.Net Experts,

I have this concept described below:

1. I have a large (4K+) data record called Health_R. This record also
contains Text/memo fields and Image.

2. The record layout is below:
*******************
//My beloved c#
//
using System;

namespace Health.HealthRec
{
[Serializable]
public class Health_R
{
private string PatientName; //NVarChar(80)
private string PresentIllness; //ntext --Memo Field (Filled by
Nurses/Doctor
private string PatientPresentImage; //Image --Patient Picture
private string DOB; //DateTime

public Health_R()
{
}
}
}
*********************

3. In my Asp.Net form, I "new" the above structure as: private Health_R
ph=new Health_R();

4. I then read the data (say one record) into my DataSet.

5. I then go into a for loop to extract each field from the returned DataSet
as follow:

string fn="";
for(int i=0;i<DataSet.Tables[0].Columns.Count;i++)
{
fn=DataSet.Tables[0].Columns.ColumnName;
if(fn=="PatientName") //This is tough - I want to get rid of
this hard code
{
ph.PatientName=DataSet.Tables[0].Rows[0][fn].ToString();
}
else if...
}

***********

My question is: How do I assign each field to instance "ph" using variable
"fn" (contains the name of the database table column) as I could with the
DataSet.Tables[0].Rows[0][fn].ToString() without hardcoding all the columns
names ?

I really like this concept because I can do this at runtime without changing
the source codes in the event the database structure is changed.

Thanks for your help,

BTW: I read about "Reflection" but could not find any reference about it.

John
 
W

WJ

Bill,

Elegant it is! Thank you very much. I will try it out.

John Webb

William F. Robertson said:
I changed your record layout to this. You should have the types be what
they should be. That will ensure type safety. And it beats parsing
strings
all the time.

public class HealthRecord
{
public string PatientName;
public string PresentIllness;
public byte [] PatientPresentImage;
public DateTime DOB;

public HealthRecord()
{
}
}

Here is the code snippet I used. I set it up so you could actually define
you HealthRecord in another assembly and still load it, but for now it
will
call GetExecutingAssembly() because I defined them together.

public static void FillHealthRecord()
{
Assembly ass = Assembly.GetExecutingAssembly();
Type t = ass.GetType( "tempcsharp.HealthRecord" );
FieldInfo field = null;
HealthRecord record = null;

DataTable table = GetDataTable(); //or in your case a DataSet
foreach( DataRow row in table.Rows )
{
record = ( HealthRecord ) Activator.CreateInstance( t );
foreach( DataColumn column in table.Columns )
{
field = t.GetField( column.ColumnName );

//you might want to check the FieldType to ensure it is the same as you
DataColumn.
if ( field != null )
field.SetValue( record, row[column] );
}

Console.WriteLine( record.PatientName );
Console.WriteLine( record.PresentIllness );
Console.WriteLine( record.PatientPresentImage );
Console.WriteLine( record.DOB.ToShortDateString() );
}
}

HTH,

bill



WJ said:
.Net Experts,

I have this concept described below:

1. I have a large (4K+) data record called Health_R. This record also
contains Text/memo fields and Image.

2. The record layout is below:
*******************
//My beloved c#
//
using System;

namespace Health.HealthRec
{
[Serializable]
public class Health_R
{
private string PatientName; //NVarChar(80)
private string PresentIllness; //ntext --Memo Field (Filled by
Nurses/Doctor
private string PatientPresentImage; //Image --Patient Picture
private string DOB; //DateTime

public Health_R()
{
}
}
}
*********************

3. In my Asp.Net form, I "new" the above structure as: private Health_R
ph=new Health_R();

4. I then read the data (say one record) into my DataSet.

5. I then go into a for loop to extract each field from the returned DataSet
as follow:

string fn="";
for(int i=0;i<DataSet.Tables[0].Columns.Count;i++)
{
fn=DataSet.Tables[0].Columns.ColumnName;
if(fn=="PatientName") //This is tough - I want to get rid of
this hard code
{
ph.PatientName=DataSet.Tables[0].Rows[0][fn].ToString();
}
else if...
}

***********

My question is: How do I assign each field to instance "ph" using
variable
"fn" (contains the name of the database table column) as I could with the
DataSet.Tables[0].Rows[0][fn].ToString() without hardcoding all the columns
names ?

I really like this concept because I can do this at runtime without changing
the source codes in the event the database structure is changed.

Thanks for your help,

BTW: I read about "Reflection" but could not find any reference about it.

John

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top