adaam
adaam

Reputation: 3706

Evaluating whether the contents of a textbox is nothing

I am trying to evaluate the contents of a ASP Label to see whether it is null / nothing, and then filling another label with an error message.

Here is the evaluation (EDIT: Updated to include working code):

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If String.IsNullOrEmpty(lb_showcoordinates.Text) Then
        nocoordinatesmessage.Text = "Please validate your meeting location above using the <b>Validate Address</b> button"
        Console.WriteLine("okay")
    Return
    End If
    Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
    conn.Open()
    Dim cmd As New OleDbCommand("INSERT INTO Events (EventTitle, EventDescription, EventDate,EventCategory, CreatedBy, StreetAddress, Town, Country, Coordinates) VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8,@f9)", conn)
    cmd.Parameters.AddWithValue("@f1", tb_eventtitle.Text)
    cmd.Parameters.AddWithValue("@f2", tb_eventdescription.Text)
    cmd.Parameters.AddWithValue("@f3", DateTime.ParseExact(tb_eventdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture))
    cmd.Parameters.AddWithValue("@f4", dd_eventcategory.SelectedIndex + 1)
    cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
    cmd.Parameters.AddWithValue("@f6", txtStreetAddress.Text)
    cmd.Parameters.AddWithValue("@f7", txtSuburb.Text)
    cmd.Parameters.AddWithValue("@f8", txtCountry.Text)
    cmd.Parameters.AddWithValue("@f9", lb_showcoordinates.Text)
    cmd.ExecuteNonQuery()
    conn.Close()
    Response.Redirect("calendar.aspx")
End Sub

Unfortunately I am not getting the desired results; the contents of my label have been left empty (on purpose) so I was wondering what I am doing wrong? What is the correct expression in VB.NET to evaluate whether the contents of a textbox/label/any other text based asp control (i.e. .Text) is empty?

Here is my debug process screenshot:

nothing or maybe not?

As you can see the contents of the lb_showcoordinates.Text is empty (or at least "")

Upvotes: 1

Views: 688

Answers (2)

Jeff B
Jeff B

Reputation: 545

You could also add a requiredfieldvalidator which will require the user to enter the field and then you know it's filled in by the time the button click is called.

Upvotes: 0

nmat
nmat

Reputation: 7591

You should use String.IsNullOrEmtpy:

If String.IsNullOrEmpty(lb_showcoordinates.Text) Then
    lb_nocoordinatesmessage.Text = "Please validate your meeting location above using the <b>Validate Address</b> button"
    Console.WriteLine("okay")
End If

When a textbox is empty its contents are an empty string, so comparing to Nothing won't work.

Upvotes: 2

Related Questions