abt datareader

mayank18

Member
Joined
Mar 31, 2007
Messages
5
Programming Experience
Beginner
hi,
can any body tell how to access the column of the row retrived using datareader.....
for eg. the data reader retrives the row having 5 columns and i want the value of 3rd column of that row, how it can b done?:(
 
reader.GetString(2) i think

beware of using hard coded column indexes.. if the schema shifts, your program breaks. try to use column names instead
 
one more query about data reader

is there is any way to check whether data reader contain some valid data or not?

acctually i m gving a select Query...
and i want to check whether data reader contains the valid ans......
what is isdnNull method?

my code is suppose to be.......

if no data then
msgbox("no data present")
else
msgbox("data present")
end if
 
isdnNull()? never heard of it

If you mean IsDbNull(integer) method, it tells you whether the value of the given column is a null (empty) or not.

You can find this info in the Object Viewer you know.. Press F2 (not F1) and type IsDbNull in the search box
 
if u want to check the data reader returns any row?
use

if datareader.read()=false then
'datanot present
else
'data present
end if


if u want to knw if data retrieved is null you can use

if isdbnull(datareader.item("column_name")) then

' data not present

else

'data present

end if
 
if isdbnull(datareader.item("column_name")) then

' data not present

Not quite.

reader's IsDbNull() takes either the int column index or the string column name to test whether that value of that column is null for this row.

datareader.item("column_name") returns the data item (as an object) found at that named column. Obviously you shouldnt pass this to the IsDbNull() and Option Strict = on wont let you. If you code with Strict off (which you shouldnt ever do) then it will probably try to convert the object to a string which may return the value found, and then try to call the IsDbNull for that as the column name (which it is not


If youre confused, then heres an example:

Your datareader contains one column, FRUIT
This row right now, has the value "apple" in the FRUIT column

Your code will call:
if IsDbNull("apple")


When you need to call:
if reader.IsDbNull("FRUIT") ...


additionally, IsDbNull as a function on its own, is old VB (a legacy function implemented to help VB6 code work when pasted into .NET). Please use the new .NET functions in your code samples for newbies. You dont really help them by teaching them old VB..
Please test your code examples before you post them, or at least think carefully about what youre writing. Broken code is no use to anyone trying to learn
 
Back
Top