Help with Access Database code

K

Keith Smith

How come this code doesn't work...?

myConn.Open();
DataSet dtSet = new DataSet();
myCmd.Fill( dtSet, "security" );
DataTable dTable = dtSet.Tables[0];
string str;
str = (string)dtSet.Tables["security"].Row[0].["name"];
MessageBox.Show(str);

I get the message "Identifier expected".

In case you are wondering, I am trying to read the first row of a table
called "security" in an MS Access database. The column name is "name".
 
C

Colin Stutley

Take the '.' out of the column reference;
str = (string)dtSet.Tables["security"].Row[0]["name"];
not:
str = (string)dtSet.Tables["security"].Row[0].["name"];

- Colin.
 
K

Keith Smith

Take the '.' out of the column reference;
str = (string)dtSet.Tables["security"].Row[0]["name"];
not:
str = (string)dtSet.Tables["security"].Row[0].["name"];

- Colin.

Thanks! That worked!

Is there a way to set the value of "str" to be the "next value in line" as
opposed to having to specify the particular row number? I know in the past
when I programmed in ASP I could create a dataset and then just scroll
through the dataset.
 
C

Colin Stutley

Try;
foreach(DataRow vRow in dtSet.Tables["security"].Rows)
str = (string)vRow["name"];

You even have a 'Select' method to process only those rows out of the
'security' table that meet a particular criteria.

You may want to look at strongly typed datasets, which allow you to move
away from late binding of table/field names. You can then refer to the
'name' field as Security.Name - saves hours of grief with finding a type
embedded in quotes, which will only show up at runtime (a typo on a stong
typed name will show up at compile time). Just MPOV.

- Colin

Keith Smith said:
Take the '.' out of the column reference;
str = (string)dtSet.Tables["security"].Row[0]["name"];
not:
str = (string)dtSet.Tables["security"].Row[0].["name"];

- Colin.

Thanks! That worked!

Is there a way to set the value of "str" to be the "next value in line" as
opposed to having to specify the particular row number? I know in the past
when I programmed in ASP I could create a dataset and then just scroll
through the dataset.
 

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

Top