473,424 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,424 software developers and data experts.

Data field references ?

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[i].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

Nov 19 '05 #1
3 1453
WJ
It seems that I have no other alternative but hashtable.

John Webb
Nov 19 '05 #2
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" <Jo*******@HotMail.Com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
.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[i].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

Nov 19 '05 #3
WJ
Bill,

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

John Webb

"William F. Robertson, Jr." <theman_at_fdrsucks.com> wrote in message
news:OV****************@TK2MSFTNGP10.phx.gbl...
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" <Jo*******@HotMail.Com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
.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[i].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


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ronnie | last post by:
Hi All, A newbie here having a hard time figuring out how to parse out the City, State and Zip from a text field. I have a text field called "Registration" with a size of 40. In this field...
2
by: Rooksarii | last post by:
Hello folks, Let me first apologize for any impropper terminology I may use as I am presently trying to broaden my Office knowledge by diving into Access head on. My specific problem is this....
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
4
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I...
2
by: x | last post by:
hi i am a pilot by profession. i want to create a database of my logbook using ms access 2002. i am facing a problem regarding the format of time field. when i select "Data/Time" data type for my...
5
by: DBQueen | last post by:
I have a database where the user will have the opportunity to set up a number of Tests. I want the user to be able to enter the equation if a field for a particular test is to be a calculated...
23
by: Bjorn | last post by:
Hi. Every time i post data in a form the contents are being checked for validity. When i click the back-button, all data is gone and i have to retype it. It's obvious that only a few or none of...
20
by: hippomedon | last post by:
Hello everyone, I'm looking for some advice on whether I should break the normalization rule. Normally, I would not consider it, but this seems to be a special case. I have created an...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.