Why is my if statement not firing - asp.net

I did a simple experiment to make sure I know what is going on as I'm new to programming. This has me stumped and I'm getting angry with my computer for lying to me. :)
Here is my vb.net
If table2.Rows.Count = 0 Then
LBLCannary.Visible = True
End If
Now if I hover over during debug it tells me the count is zero. ie table2.Rows.Count 0 but for some odd reason it never goes to the next line in the if. if the table is empty. Why won't it work?
Answer: Silly newbie mistake, All this is done after return and nothing happens after a function returns something.

Can you try the reverse?
If table2 IsNot Nothing AndAlso table2.Rows.Count > 0 Then
'do nothing
Else
'if no rows or if table is empty
LBLCannary.Visible = True
End If
or widen your condition to
If table2 Is Nothing OrElse table2.Rows.Count < 1 Then
LBLCannary.Visible = True
End If

Related

Why is this program not entering my ElseIf statement -- System.DBNull

I imported an Excel file into an ASP.NET DataTable. Some of the values are blank, specifically at indexes 2 and 3, and I would like to set those blank fields to 0.
When debugging, the values for row.item(2) and row.item(3) are both System.DBNull. Interestingly enough, the program enters the If statement and sets row.item(2) value to 0, but it never enters the ElseIf statement. What is going on? Here's my code:
If row.Item(2) Is Nothing OrElse row.Item(2).ToString = "" OrElse IsDBNull(row.Item(2)) Then
dt.Rows.Item(i).SetField(2, 0)
ElseIf row.Item(3) Is Nothing OrElse row.Item(3).ToString = "" OrElse IsDBNull(row.Item(3)) Then
dt.Rows.Item(i).SetField(3, 0)
End If
EDIT:
Per Tim's suggestion, I revised my code as follows:
If Not row.Field(Of Int32?)(2).HasValue Then
dt.Rows.Item(i).SetField(2, 0)
End If
If Not row.Field(Of Int32?)(3).HasValue Then
dt.Rows.Item(i).SetField(3, 0)
End If
However, this is throwing an error--Object cannot be cast from DBNull to other types
When debugging, in the first loop:
row.Item(2) = 17
row.Item(3) = 1
Theoretically, it should skip this iteration and proceed to the next. However, my program immediately catches the exception with the above error.
Since your if is executing, your else if will not execute.
If you want your else if logic to execute even if your if executes, just make it it's own if block:
If row.Item(2) Is Nothing OrElse row.Item(2).ToString = "" OrElse IsDBNull(row.Item(2)) Then
dt.Rows.Item(i).SetField(2, 0)
End If
If row.Item(3) Is Nothing OrElse row.Item(3).ToString = "" OrElse IsDBNull(row.Item(3)) Then
dt.Rows.Item(i).SetField(3, 0)
End If
You don't provide sample data so we just have to guess. But my guess is that you want two If statements.. not an ElseIf. ElseIf will only evaluate if the If statement returns false.

Custom Validator on phone number and further information on it

I'm looking for a good site that explains CustomValidator in detail. If anyone has a go to please let me know. The following code checks that there are at least 10 numbers in the result. However, I'm looking to also have it validate that the values are numbers. Using CustomValidator in vb.net is there a way to do this as an "and if" statement?
Thank you
Sub AtLeastTenNumbers_ServerValidate(ByVal source As Object, _
ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
If area_code.Text.Length + phone_1.Text.Length + phone_2.Text.Length > 9 Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
This is a bit of an old article, but Scott Mitchell knows his stuff:
https://web.archive.org/web/20211020145934/https://www.4guysfromrolla.com/articles/073102-1.aspx
is there a way to do this as an "and if" statement?
You can always nest an if statement inside of your current statement.
If area_code.Text.Length + phone_1.Text.Length + phone_2.Text.Length > 9 Then
args.IsValid = True
'Check to see if this part is numeric
If IsNumeric(phone_1.Text) Then
' Do Logic here
End If
Else
args.IsValid = False
End If

How do I correctly check DBNull in VB?

Why does the following code:
A = not IsDBNull(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
results in the following error:
Conversion from type 'DBNull' to type 'String' is not valid.
When AndAlso is supposed to short-circuit according to this article:
http://support.microsoft.com/kb/817250
You are correct. AndAlso is short circuiting.
However, the error comes by calling CurRow("GuyBook") (verify this in a debugger to make sure I'm not a liar or making some crazy assumptions or just misremembering* ;-). Before you ask for a value, you need to ask the DataRow if it has a value. That is, use:
CurRow.IsNull("BuyBook")
Happy coding.
*One should just be able to compare with DBNull.Value or use IsDBNull. However, I am fairly certain that I ran into a row before that threw these exceptions instead of returning a DBNull object. Start by finding out -- in the Immediate Window of the Debugger -- exactly which expression throws the exception.
Have you tried comparing like this:
If CurRow("BuyBook") Is DBNull.Value Then
'...
End If
Instead of not IsDBNull(CurRow("BuyBook")), use NOT (CurRow("BuyBook")) is System.Dbnull.Value). Try this:
A = (NOT (CurRow("BuyBook")) is System.Dbnull.Value) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
OR
A = not string.IsNullOrEmpty(CurRow("BuyBook")) AndAlso CType(CurRow("BuyBook"), string) = "Yes"
If dt.Rows(0)("BuyBook") = DBNull.Value Then
End If
Hope it helps.
In vb.net I usually do something like this:
A = (CurRow("BuyBook") & "" = "Yes")
Old trick from vb6 era. If CurRow("BuyBook") is null, vb.net will consider it as empty string when concatenating it with another string.

how do I compare two dates?

We would like to compare a date from the db with current date.
Here is what I have tried so far:
'Obtain current date
Dim curdate As String = Now.ToString("yyyy")
'Now compare curdate with date from db called eventdate. Event date is of dateTime datatype.
If drv("eventdate").ToString = "" And Year(drv("eventdate").ToString) = curdate Then
'Then do some processing
End if
When I run this code, I get following error:
Conversion from string "" to type 'Date' is not valid.
Please help!
Thank you very much experts.
If Today.CompareTo(CDate(drv("eventdate")).Date) = 0 Then
'Do something
End If
I'm making the assumption here that you only care about the day, and not the time. Otherwise, the odds of having a match within a given day would be about 1 in 2.5 million.
Based on the comment:
If Today.Year.CompareTo(CDate(drv("eventdate")).Year) = 0 Then
'Do something
End If
or
If Today.Year = CDate(drv("eventdate")).Year Then
'Do something
End if
And one more thing: it really sounds like this is a check that should be done by your database. Make it part of the query that returns the results. This will be much faster, and it's where that kind of check belongs. All you'd need is add a check to your where clause like so:
Year(eventdate) = Year(current_timestamp)
If drv has an instance of DataReader then verify DBNull.
IF Not drv.IsDBNull(column_index_of_eventdate) Then
...
If drv.GetDateTime(column_index).Year=Date.Now.Year Then
...
End If
End If
I believe you have either a null or empty value, also your comparisn is wrong, try:
If drv("eventdate").ToString <> "" Andalso Year(drv("eventdate").ToString) = curdate Then
'Then do some processing
End if

Dynamic ID name in ASP.NET VB.NET

I have about 20 asp:labels in my ASP page, all with ID="lbl#", where # ranges from 0 to 22. I want to dynamically change what they say. While I could write
lbl1.Text = "Text goes here"
for all 23 of them, I'd like to know if there is a way to loop through all of them and change their text.
I thought of creating an array with all my labels, and then just do a For Each loop, but I also have to check if the element exists with IsNothing before I change its text, so I got stuck there.
If anyone can help me, I'd really really appreciate it!
Thank you so so much for your help!!
You can dynamically look up controls on the page by using the System.Web.UI.Page.FindControl() method in your Page_Load method:
Dim startIndex As Integer = 0
Dim stopIndex As Integer = 22
For index = startIndex To stopIndex
Dim myLabel As Label = TryCast(FindControl("lbl" + index), Label)
If myLabel Is Nothing Then
Continue For
End If
myLabel.Text = "Text goes here"
Next
Something like this may work, but you would likely need to tweak it (its from memory, so its not exactly 100% syntactically correct)
For Each _ctl as Control In Me.Controls()
If (TypeOf(_ctl) Is Label) = False Then
Continue For
End If
'add additional filter conditions'
DirectCast(_ctl, Label).Text = "Text Goes Here"
Next
You can also do something similar on the client side using jQuery selectors.

Resources