logic check please - not getting results wanted from IF

  • Thread starter Thread starter D. Shane Fowlkes
  • Start date Start date
D

D. Shane Fowlkes

(still a .NET newbie)

I have a chunk of code in a Page_Load routine that pulls data from a SQL
Server table. The Select statement looks for any "events" scheduled for
today. Then my IF statement writes some HTML to a asp:label tag and
conditionally fills it with HTML based upon any records were found. Seems
simple huh? It works fine if the recordset is null...or in .NET speak, if
the If Not DataReader.Read()..but when data IS found....nothing happens.
All it writes to the page is literally "<ul></ul>". D'oh!

Could someone spot check this please? It seems fine to me......


'==================================
'Display any events happening today....

Dim strTodayEventsSQL As String
Dim datTodayEvents As SQLDataReader

strHTML = ""
strTodayEventsSQL = "SELECT * FROM IntranetCalendar WHERE CalMonth = " &
Month(Today()) & " AND CalDay = " & Day(Today()) & " AND CalYear = " &
Year(Today()) & " AND Display = 1;"

cmdSQLCommand = New SQLCommand(strTodayEventsSQL, objDbConn)
datTodayEvents = cmdSQLCommand.ExecuteReader()

strHTML = strHTML & "<ul>"

If datTodayEvents.Read() Then

'****NOTHING SEEMS TO HAPPEN HERE*********
While datTodayEvents.Read()
strHTML = strHTML & "<li><a href=" & CHR(34) &
"calendar/default.aspx" & CHR(34) & ">" & datTodayEvents("CalTitle") &
"</a></li>"
End While

Else
strHTML = strHTML & "<li><a href=" & CHR(34) & "calendar/default.aspx"
& CHR(34) & ">There is nothing entered in for today - however, you can add
agency related events to the DRPT Calendar here.</a></li>"
End If

strHTML = strHTML & "</ul>"

lblTodayEvents.Text = strHTML

datTodayEvents.Close()
'==================================


--


*********************************
D. Shane Fowlkes - TMM
Saving the world, one web site at a time.
http://www.shanefowlkes.com
*********************************
 
Hello Shane


Get rid of the "If datTodayEvents.Read()"


just use

strHtml=""

While datTodayEvents.Read()
strHTML = strHTML & "<li><a href=" & CHR(34) &
"calendar/default.aspx" & CHR(34) & ">" & datTodayEvents("CalTitle")
&
"</a></li>"
End While

If strHtml.Length <1 Then
strHTML = strHTML & "<li><a href=" & CHR(34) &
"calendar/default.aspx"
& CHR(34) & ">There is nothing entered in for today - however, you
can add
agency related events to the DRPT Calendar here.</a></li>"
End If


The way you are doing it you are throwing away the first row. If your test
situation has only one row returned, you will never get that row doing it
the way you where doing it.

also you should change your concatination to:

strHTML += "<li><a href=" & CHR(34) &
"calendar/default.aspx" & CHR(34) & ">" & datTodayEvents("CalTitle")
&
"</a></li>"

Old habits die hard :-)



--
Ibrahim Malluf
http://www.malluf.com
==============================================
MCS Data Services Code Generator
http://64.78.34.175/mcsnet/DSCG/Announcement.aspx
==============================================
 
Do you just have 1 record to display ?
This puts you in the first record.
This puts you in the second record.

If there is no second record then your strHTML will be
empty.


Thanks,
Shan
 
Back
Top