none
dataTable, dataRow... with CopyTo() Method RRS feed

  • Question

  • 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

    Monday, April 23, 2007 3:05 PM

Answers

  • DataRow is a collection. You can access its elements using dr[index].
    Monday, April 23, 2007 4:30 PM
  • 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...

    }

    Monday, April 23, 2007 7:28 PM

All replies

  • DataRow is a collection. You can access its elements using dr[index].
    Monday, April 23, 2007 4:30 PM
  • 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...

    }

    Monday, April 23, 2007 7:28 PM