Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15006

VS 2013 How to insert null or empty date from datagridview column to database using vb.net?

$
0
0
Hello fellow coders. I'm asking for your help on how to insert or save empty date value or null value from datagridview column to access database. I research over the web but unfortunately not solved my problem. I'm trying to set dateret as string but it says: "String cannot be converted to date". If I set dateret as Date, then it says: "Not recognized as valid datetime" or "Datatype mismatched". How to address this problem? Following is my simple code. Thank you.

Code:

Dim connstring As String
            connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=retirement.accdb"
            Dim conn As OleDbConnection = New OleDbConnection(connstring)
            Dim ln, fn, mn, retiredate As String
            'Dim dateret as Date
            'Dim dateret as String

            For i As Integer = 0 To Me.DataGridView1.RowCount - 1

                ln = Me.DataGridView1.Item(0, i).Value
                fn = Me.DataGridView1.Item(1, i).Value
                mn = Me.DataGridView1.Item(2, i).Value
             
             
          If Me.DataGridView1.Item(3, i).Value Is Nothing Or Me.DataGridView1.Item(3, i).Value = String.Empty Then
                'nothing to insert or null value inserted
          else
                  dateretire = Me.DataGridView1.Item(3, i).Value
          End If
             
                Dim Sql = "insert into emprecords(lastname, firstname, middlename, dateretire) values('" & ln & "','" & fn & "','" & mn & "','" & dateret & "')"


                Dim comm As New OleDb.OleDbCommand
                comm.CommandText = Sql
                comm.Connection = conn
                conn.Open()
                comm.ExecuteNonQuery()

                comm.Dispose()
                conn.Close()

            Next
            MsgBox("Imported Successfully! Click refresh button to reload data.", MsgBoxStyle.Information, "Import")
            Me.Close()
        End If


Viewing all articles
Browse latest Browse all 15006

Trending Articles



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