How do I pass one record, one row to a Method?

  • Thread starter Thread starter DaBrain
  • Start date Start date
D

DaBrain

Win32 C++/Delphi Developer here moving to .NET C# (Yes, I'm a newbie
again)

Thank you all for any replies in advance.

I am developing a class that accesses a record, one record, and sets
the Row values into a Properties, this is of course is based on many
conditions, there is a lot of data validation etc in there before the
property is set, thus this method is required. I have developed this
because we have to access this method from all over the place.

I developed this method passing in a RecordID, got the record then did
my stuff, great it all works!

The Problem:
I want to be able to use this Method say in a while loop from a Query
sending each row to the method and reading the properties..

The Newbie Question:
How do I pass one record, one row to a Method?
and
How do i access the individual fields by name after passed to the
method?
 
The first thing depend on wich type of DataStore you are using into your
application. For example if you are using DataTable and you want to pass a
valid row

private void pass(DataTableRow row)
{
//here you must retreive data
String field = row["Field_name"].ToString();
}
 
It sounds like you are filling objects from a database - in which case I
would tend to use a DataReader... this provides access to each row (in turn,
forwards only), and access to the values within that row either by name or
ordinal. Perhaps google for some SqlDataReader examples?

Marc
 
How do I pass one record, one row to a Method?
and
How do i access the individual fields by name after passed to the
method?

The key is to understand the OOP model of a DataSet. A DataSet includes
a number of possible objects, some of which can be DataTables. A
DataTable includes a collection of rows and columns. You often don't
need a full DataSet, especially in ADO.NET 2.0, since a single
DataTable often has the functionality you need.

A DataTable contains both DataRows and DataColumns. One oddity of
ADO.NET is that a DataRow is NOT a collection of DataColumns, and this
is where most Delphi coders have a problem at first. A DataRow can't
stand alone: it only has a meaning in the context of a DataTable.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Create a table using the DataTable(string TableName)
constructor
DataTable dt = new DataTable("Employees");

// Add 2 columns to the table
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name", typeof(string));

// Add a primary key based on a column
dt.PrimaryKey = new DataColumn[] { dt.Columns["ID"] };

// Now create some new rows with data in the new columns
DataRow dr;

dr = dt.NewRow();
dr["ID"] = 600040;
dr["Name"] = "Smith";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["ID"] = 205;
dr["Name"] = "Jones";
dt.Rows.Add(dr);

// Iterate over the rows of the table - this shows how to pass
rows to another function
foreach (DataRow row in dt.Rows)
showRow(dt, row);
}

private static void showRow(DataTable dt, DataRow row)
{
// now iterate over the columns in a single row
foreach (DataColumn dc in dt.Columns)
Console.WriteLine("Column: {0}, value: {1}", dc.ColumnName,
row[dc].ToString());
Console.WriteLine(""); // blank line after each row
}
}
}
 
If you go to
http://sholliday.spaces.live.com/?_...ogview&_c=blogpart&partqs=amonth=5&ayear=2006
or
http://sholliday.spaces.live.com/ and find the May 2006 blog entry.

Download the code.

And look for thsi method:


private Collections.CustomerCollection SerializeCustomers(IDataReader
dataReader, bool deep)
{
}


Notice this is private. Basically, I have a common method to create a
Collection, but the dataReader could have 1 , 100 , 1000 Customers in it.
The method which calls this method decides how to populate the dataReader.


As far as accessing the fields by name, find the code for the SafeDataReader
http://www.lhotka.net/Article.aspx?id=9280bc86-c706-4d2d-8993-8b5bda6bad22
and
http://www.lhotka.net/cslanet/download10.aspx


Again, if you get the code (from my blog), you can see the concept in
action, where SerializeCustomers is reused over and over again.
 

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

Back
Top