datareaders and primary keys

G

Guest

How do I capture the data in a primary key?

strsql="select nameID,fname from tblname where fname = '" & txtFname.text &
"' and lname = '" & txtlname.text & "'"

objOleDBCommand.CommandText = strsql
objoledbreader = objOleDbCommand.ExecuteReader()
intname = objoledbreader.item("nameID")

error "No data exists for the row/column
I go to the tblname on sql server and there is data in table when above
query is run with appropriate names.

I have tried intname =
objoledbreader.getstring(objoledbreader.getordinal("nameID"))

no data returned.

I need the number of the primary key to insert in a related table as a
foreign key.
 
M

Marina Levit [MVP]

When you call ExecuteReader, the current row pointer is positioned *before*
the first row.

You have to call the .Read method, which tells you if it was able to move to
the next row. The first time you call it, it would attempt to position on
the first row if the query returned any rows.
 

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