Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27184

VS 2010 [RESOLVED] Null Date cannot be added to parameter

$
0
0
I'm hoping I am doing this correctly but what I am currently doing is running through each row of a datatable and using an insert command on each row, inserting the records to a table in a MySQL database.

Below are the parameter statements and on the second one I am getting an error

"Conversion from type 'DBNull' to type 'Date' is not valid."

VB.Net Code:
  1. 'adding to the parameter collection
  2. strSQLCmd.Parameters.Add("@CustomerSince", MySqlDbType.DateTime)
  3.  
  4. 'while running through records in a datatable using an insert command to save to a MySQL table
  5. strSQLCmd.Parameters("@CustomerSince").Value = CDate(row("CustomerSince"))

I checked the original data and of course it is null. So I started looking around to determine what to do to correct the issue. From what I can tell, I need to check if it is null but I am not sure if what I have done is correct. It looks like it still sends Null to my database which is ok, I would just like to check to make sure this code below is the best solution.

VB.Net Code:
  1. If Not IsDBNull(row("CustomerSince")) Then
  2.      strSQLCmd.Parameters("@CustomerSince").Value = CDate(row("CustomerSince"))
  3. End If

Is it possible for someone to have a look and validate if I am doing it correctly? It appears ok and my reports on the data seem to work ok. TIA, rasinc

Viewing all articles
Browse latest Browse all 27184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>