Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a booking form, i want to compare the entered date in the textbox(text mode is date) and all the dates in the database. So if there is an order already booked on that day, it will display already booked select another date otherwise it will carry on the form filling.(in vb.net-visual studio 2012)

What I have tried:

VB
Protected Sub tbdate_TextChanged(sender As Object, e As EventArgs) Handles tbdate.TextChanged

        Dim adaptor As New SqlDataAdapter
        Dim ds As New DataSet
        Try
            objConn.Open()
            Dim sqlcmd As New SqlCommand("select * from bookorder where=' " & tbdate.Text & "'", objConn)
            sqlcmd.ExecuteNonQuery()
            adaptor.SelectCommand = sqlcmd
            adaptor.Fill(ds)
            If ds.Tables(0).Rows.Count > 0 Then
                Label8.Visible = True
                Label8.Text = "Enter  different date"
            End If
            adaptor.Dispose()
            ds.Clear()

        Catch
            e.ToString()
        Finally
            objConn.Close()
        End Try
    End Sub
End Class
Posted
Updated 20-Mar-17 22:49pm
v2

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

In this case, it gets even worse, because not only can your user destroy your database just by typing, but it's very prone to errors as well, as the date format the SQL expects is quite likely to be different from what the user entered.

Convert the user input to a DateTime value using DateTime.TryParse (reporting errors to the user) and then pass that dateTime value to SQL via a parameterised query.
Be aware that you may need to CONVERT your database stored value to a DATE for comparison if it has a time component, as the equality comparison expects an exact match on the whole date and time value: 2017-03-21 00:00:00.000 is not the same as 2017-03-21 00:00:00.001 and will not match! CONVERTing it to a DATE strips out the time info so it matches.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900