Conversion From String To Type 'Date' Is Not Valid

Darren

I have 2 datetimepicker that allow user to choose the start date and end date. So after choosing the dates, user must click a button to run the sql query. So the output is filtered based on the chosen date.

However there are error when i declare the date as follows (this code below is put under the button click event) :

Dim startdate As DateTime = Format(DateTimePicker1.Value, "dd/MM/yyyy")
Dim enddate As DateTime = Format(Microsoft.VisualBasic.DateAdd(DateInterval.Day, 1, DateTimePicker2.Value), "dd/MM/yyyy")

query = "Select * from records where recorddate between '" & startdate & "' and '" & enddate & "'"

The error is here :

Dim startdate As DateTime = Format(DateTimePicker1.Value, "dd/MM/yyyy")

The error is :

Conversion from string "13/02/2016" to type 'Date' is not valid.

The sample date of my records are like this:

12/2/2016 7:28:26 PM

I don't know what i missed here. Please help. Thanks

Ňɏssa Pøngjǣrdenlarp

There are several issues in your code.

Dim startdate As DateTime = Format(DateTimePicker1.Value, "dd/MM/yyyy")

If you hold the mouse over Format Intellisense will tell you that it returns a string. Since you cant assign a string result to a variable declared as DateTime, you get the error. If you turn on Option Strict the compiler will tell you about these.

Secondly, the query converts the dates to string also:

"...between '" & startdate & "' and '" & enddate & "'"

Ticks are not all-purpose SQL delimiters, they are a way of designating text/string data. SQL Parameters are safer and prevent accidental data type changes as well as SQL injection attacks. They also make the code easier to read. If you have a name column in your db, try to INSERT a name like Tim O'Brien, D'Angelo Barksdale or Betty's Cupcake Factory. The query will crash. SQL Parameters prevent this.

I have no idea which database, so I guessed Access. That doesn't matter because the DB Providers all work much the same:

' Dim startdate As DateTime = DateTimePicker1.Value
Dim enddate As DateTime = DateTimePicker2.Value.AddDays(1)

Dim SQL = "Select * from records where recorddate between @p1 AND @p2"

Using dbCon As New OleDbConnection(connstr)
    Using cmd As New OleDbCommand(SQL, dbCon)
        dbCon.Open()
        cmd.Parameters.Add("@p1", OleDbType.DBDate).Value = startdate
        cmd.Parameters.Add("@p2", OleDbType.DBDate).Value = DateTimePicker1.Value

        dt = New DataTable
        dt.Load(cmd.ExecuteReader)
    End Using
End Using

Note This assumes the column type in the DB is Date and not string. The BETWEEN clause is unlikely to work with dates as string. If you want them to act as dates, save them as Date. You do not "need to convert to String in order to use it in SQL". Ever.

  • No processing is required for the Date variables. The NET providers know how to pass a DateTime type to the data base.
  • There is no real need to assign the DateTimePicker1.Value to a new variable, because .Value is a DateTime type.
  • The Using blocks declare and create the DB objects, then close and dispose of them to free resources
  • When defining the parameter, the code tells it to expect a date (OleDbType.DBDate), then the Value set is an actual date type.

With Access/OleDB it is important to set the parameter values in the exact same order as they appear in the SQL. OleDB allows named parameters but assigns the values in the order they appear in the SQL.

To emphasize that, MSDN (and others) encourage the use of ? in place of other forms ("@p1", "@firstname", "@DateOfBirth"). Especially in queries with more than a few columns, I like "@p1" because the numeral helps index the related column in the SQL and helps make sure they are in order (visually).

Finally, whether your BETWEEN clause works as you want will depend on several things including that DataType parameter. The dates from the DateTimePicker will include the time. In order to not to exclude some rows because the time portion is earlier than whatever you got from the DateTimePicker, you will need to use the correct one.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Conversion From String To Type 'Date' Is Not Valid

From Dev

Date conversion from string is not valid

From Dev

Conversion from string "‎8/‎5/‎2014" to type 'Date' is not valid

From Dev

Conversion from type 'DBNull' to type 'String' is not valid

From Dev

Conversion from type 'Button' to type string is not valid

From Dev

Conversion from type 'DBNull' to type 'String' is not valid

From Dev

Conversion from type 'Button' to type string is not valid

From Dev

Conversion from string "." to type 'Decimal' is not valid

From Dev

Conversion from string to type Double is not valid

From Dev

Conversion from string "I" to type 'Boolean' is not valid

From Dev

Error: Conversion from string "" to type 'Double' is not valid

From Dev

Conversion from "string" to Type 'Boolean' is not valid

From Dev

Conversion from string to type Double is not valid

From Dev

Conversion from type 'DBNull' to type 'Date' is not valid on tryparse

From Dev

Another "conversion from type DBNull to type Date not valid" issue

From Dev

Conversion from type 'DBNull' to type 'String' is not valid vb.net

From Dev

"Conversion from string to type Double is not valid" error VB.NET

From Dev

"Additional information: Conversion from string "yes" to type 'Boolean' is not valid."

From Dev

Conversion from string "" to type 'Double' is not valid In .NET Fiddle's VB

From Dev

Conversion from string "X.X" to type 'Double' is not valid

From Dev

Conversion from string "" to type 'Integer' is not valid on a for each statement

From Dev

How to get rid of Conversion from string "admin" to type 'Double' is not valid

From Dev

Conversion from string "-1" to type 'Integer' is not valid, but the String value IS a valid int

From Dev

conversion from string to double is not valid

From Dev

Date conversion from string

From Dev

Conversion from type 'DBNull' to type 'Double' is not valid

From Dev

Date-time conversion fails for valid string

From Dev

Conversion from type 'Object(,)' to type 'String' is not valid when deleting cells from vb.net

From Dev

Conversion from type 'DBNull' to type 'String' is not valid when inserting data from datagridview to sql database

Related Related

  1. 1

    Conversion From String To Type 'Date' Is Not Valid

  2. 2

    Date conversion from string is not valid

  3. 3

    Conversion from string "‎8/‎5/‎2014" to type 'Date' is not valid

  4. 4

    Conversion from type 'DBNull' to type 'String' is not valid

  5. 5

    Conversion from type 'Button' to type string is not valid

  6. 6

    Conversion from type 'DBNull' to type 'String' is not valid

  7. 7

    Conversion from type 'Button' to type string is not valid

  8. 8

    Conversion from string "." to type 'Decimal' is not valid

  9. 9

    Conversion from string to type Double is not valid

  10. 10

    Conversion from string "I" to type 'Boolean' is not valid

  11. 11

    Error: Conversion from string "" to type 'Double' is not valid

  12. 12

    Conversion from "string" to Type 'Boolean' is not valid

  13. 13

    Conversion from string to type Double is not valid

  14. 14

    Conversion from type 'DBNull' to type 'Date' is not valid on tryparse

  15. 15

    Another "conversion from type DBNull to type Date not valid" issue

  16. 16

    Conversion from type 'DBNull' to type 'String' is not valid vb.net

  17. 17

    "Conversion from string to type Double is not valid" error VB.NET

  18. 18

    "Additional information: Conversion from string "yes" to type 'Boolean' is not valid."

  19. 19

    Conversion from string "" to type 'Double' is not valid In .NET Fiddle's VB

  20. 20

    Conversion from string "X.X" to type 'Double' is not valid

  21. 21

    Conversion from string "" to type 'Integer' is not valid on a for each statement

  22. 22

    How to get rid of Conversion from string "admin" to type 'Double' is not valid

  23. 23

    Conversion from string "-1" to type 'Integer' is not valid, but the String value IS a valid int

  24. 24

    conversion from string to double is not valid

  25. 25

    Date conversion from string

  26. 26

    Conversion from type 'DBNull' to type 'Double' is not valid

  27. 27

    Date-time conversion fails for valid string

  28. 28

    Conversion from type 'Object(,)' to type 'String' is not valid when deleting cells from vb.net

  29. 29

    Conversion from type 'DBNull' to type 'String' is not valid when inserting data from datagridview to sql database

HotTag

Archive