By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

logic check please - not getting results wanted from IF

P: n/a
(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
*********************************

Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
==============================================


"D. Shane Fowlkes" <sh**********@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
(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
*********************************

Nov 18 '05 #2

P: n/a
Do you just have 1 record to display ?
If datTodayEvents.Read() Then This puts you in the first record.
While datTodayEvents.Read()
This puts you in the second record.

If there is no second record then your strHTML will be
empty.
Thanks,
Shan
-----Original Message-----
(still a .NET newbie)

I have a chunk of code in a Page_Load routine that pulls data from a SQLServer table. The Select statement looks for any "events" scheduled fortoday. Then my IF statement writes some HTML to a asp:label tag andconditionally fills it with HTML based upon any records were found. Seemssimple huh? It works fine if the recordset is null...or in .NET speak, ifthe 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 addagency 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
*********************************

.

Nov 18 '05 #3

P: n/a
That was it. Thanks!

--
*********************************
D. Shane Fowlkes - TMM
Saving the world, one web site at a time.
http://www.shanefowlkes.com
*********************************
"IbrahimMalluf" <Ib*****@malluf.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
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
==============================================


"D. Shane Fowlkes" <sh**********@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
(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
*********************************


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.