jimmyglo

hi,

I'm tryin to copy all rows from a table in MS Acess, to dataRow arrray, so i can do some further operations on it, and i won't work.

Here's the code:

OleDbDataAdapter da = new OleDbDataAdapter(comm, conn);

DataTable dt;

dt = new DataTable();

da.Fill(dt);

DataRow[] dr = new DataRow[dt.Rows.Count];

dt.Rows.CopyTo(dr,0);

foreach (DataRow row in dr)

{

MessageBox.Show(row.ToString());

}

The problem is that instead of showing in messagebox values of rows, i get "System.Data.DataRow" written there.

Thanx




Re: .NET Framework Data Access and Storage dataTable, dataRow... with CopyTo() Method

Bill Lin - MSFT

DataRow is a collection. You can access its elements using dr[index].




Re: .NET Framework Data Access and Storage dataTable, dataRow... with CopyTo() Method

Matt Neerincx

The code is working fine. You have a DataRow, so you need to index into it to get to each column's value, like so:

OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\nw99.mdb;");

OleDbDataAdapter da = new OleDbDataAdapter("select * from customers", conn);

DataTable dt;

dt = new DataTable();

da.Fill(dt);

DataRow[] dr = new DataRow[dt.Rows.Count];

dt.Rows.CopyTo(dr,0);

foreach (DataRow row in dr)

{

MessageBox.Show(row[0].ToString()); // This will display first field's value...

}