logic check please - not getting results wanted from IF

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
*********************************
 
I

IbrahimMalluf

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
==============================================
 
S

Shan

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top