ADO.net question (access field with it's name).

  • Thread starter Thread starter Ivan Sammut
  • Start date Start date
I

Ivan Sammut

Hi,

I have just started using ADO so this question may look a
bit dump.

I created a query and everything works fine.

ListViewItem iItem = null;
OleDbConnection conn = new OleDbConnection
("Provider=Microsoft.Jet.OleDb.4.0; Data
Source=c:\\program files\\visual dataflex 8.2
\\bin\\pda\\pda.mdb");
OleDbCommand cmd = null;
OleDbDataReader rec1 = null;
conn.Open();
cmd = new OleDbCommand("select * from
pusers",conn);
rec1 = cmd.ExecuteReader();
while (rec1.Read())
{
iItem = new ListViewItem();
iItem.Text = rec1.GetValue(1).ToString
();
listView1.Items.Add(iItem);
}


The problem is this line "iItem.Text = rec1.GetValue
(1).ToString();"

is there a way by which instead of a field index I can use
the field name??


Thanks
Ivan Sammut
 
Hi Ivan,

Before the loop you might retrieve column index by using GetOrdinal(string)
method.
Later you can use it for retrieving data.
And, btw, there is also a GetString method.
 
Just as a note the datareader also has an indexer, so just
reference as follows:
iItem.Text = rec1.["fieldname"].ToString();

hth
Chris Torgerson
 
Back
Top