SqlDataReader.Read - If Then

ICW

Active member
Joined
Mar 27, 2006
Messages
25
Programming Experience
Beginner
Hi, I need help with an if/then

I have some code pulling out the top line from a sql query (see below)

HTML:
      Dim comd As SqlCommand = SqlConnection1.CreateCommand
            comd.CommandType = CommandType.Text
            comd.CommandText = "Select top 1 txtLeftAvgeMeanIMT,txtLeftAvgeMaxIMT,txtRightAvgeMeanIMT,txtRightAvgeMaxIMT from HACAROTIDSCAN WHERE DATEDIFF(day, datescandate, getdate()) =0 and patientid = '" & lblPatientID.Text & "'order by scanid desc"
            SqlConnection1.Open()
            Dim dr As SqlDataReader = comd.ExecuteReader
            dr.Read()
            txtLeftAvgeMeanIMT.Text = CStr(dr.GetValue(0))
            txtLeftAvgeMaxIMT.Text = CStr(dr.GetValue(1))
            txtRightAvgeMeanIMT.Text = CStr(dr.GetValue(2))
            txtRightAvgeMaxIMT.Text = CStr(dr.GetValue(3))
I would like to say... If there is a result i.e. a value is returnd then do this thing otherwise try something else.
I am not sure how to do the 'IF' bit, how do I say
'IF this query produces a result'

Thanks
 
dr.Read() attempts to read a value. In doing so it advances the reader on by one then attaches to the new input
If there is no new input, because the end of the stream was reached, dr.Read() returns false


Think of it like:

dr.SucceededAtTryingToReadSomething()

success? true/false


PS the docs could ahve told you this

Press F2
Search for DataReader
Click System.Data.Common.DbDataReader
Look int he right, click the Read() method


-
also you can write this in your code,
right click it and choose DEFINITION
code jumps to the method..

I jsut did it now and saw this:
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#008000]//
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]// Summary:
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]// Advances the reader to the next record in a result set.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]//
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]// Returns:
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]// true if there are more rows; otherwise false.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]abstract[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]bool[/COLOR][/SIZE][SIZE=2] Read();[/SIZE]
[SIZE=2]
[/SIZE]

yes, i work in c# but its the same for VB
 
Back
Top