tags:

views:

80

answers:

5

Hi,

I am not sure why VB makes everything such a pain. I have a fields which stores the date and time in the format required to store in MySQL database

 Dim AppDate As String = String.Empty
      If Not String.IsNullOrEmpty(Me.AppDate.Text.Trim) Then
         AppDate = Format(CDate(Me.AppDate.Text), "yyyy-MM-dd h:mm:ss")
      Else
        //Need to assign a null value to AppDate 
      End If

Now I need to assign the AppDate to NUll like DBNull, but I am not able to do it directly. If I change AppDate to Date then I am not getting the required format.

Any help is appreciated .

Thanks in Advance.

+5  A: 

AppDate = Nothing

IniTech
Not working this will insert "0000-00-00 00:00:00" into database. I need to enter Null.
fireBand
Update business logic like Dan mentions. This is not a VB.NET issue.
AMissico
+3  A: 

AppDate = Nothing should work, you could also use DateTime.MinValue and update your business logic to treat it appropriately.

Dan
Thanks to every one for all the advices. I had to use IIF function in my INSERT statement apart from AppDate = Nothing.
fireBand
A: 
AppDate = DbNull.Value 'does this work?
Robert
A: 

The string value returned by the DbNull.ToString() method is the empty string (""), which may imply that the string representation of DbNull is the empty string.

M.A. Hanin
+1  A: 

the date and time in the format required to store in MySQL database

If you're building a datetime string to save to a database, you're doing it all wrong.

Using the MySql Connector/Net, you should be building your query like this:

Dim sql As String = "UPDATE `MyTable` SET `MyField` = @MyField WHERE `ID` = @MyID"
Using cn As New MySqlconnection("..your connection string here.."), _
      cmd As New SqlCommand(sql, cn)

    cmd.Parameters.Add("@MyField", MySqlDbType.DateTime).Value = MyDateTimeVar ''# NO FORMATTING NEEDED!
    cmd.Parameters.Add("@MyID", MySqlDbType.Int).Value = MyIDIntegerVar

    cn.Open()
    cmd.ExecuteNonQuery()
End Using
Joel Coehoorn