0
Public Sub uploadVehicle_Transaction()
        Try
            Dim VT As New DataTable
            VT = New Statn_Sync.DataSetTableAdapters.Vehicle_TransactionsTableAdapter().GetData()
                  For Each dr As DataRow In VT.Rows
                     Dim MOV As String = comT.insertVehicle_Transaction(Convert.ToUInt64(dr("TransactionID")), _
                                                                   Convert.ToDateTime(dr("Transaction_date")), _
                                                                   Convert.ToUInt32(dr("Bank")), _
                                                                   Convert.ToString(dr("Teller_number")), _
                                                                   Convert.ToUInt32(dr("Amount")), _
                                                                   Convert.ToString(dr("Generated_by")), _
                                                                   Convert.ToString(dr("Station")), _
                                                                   Convert.ToString(dr("Customer_name")), _
                                                                   Convert.ToUInt32(dr("Transaction_category")), _
                                                                   Convert.ToString(dr("Deposit_slip")), _
                                                                   Convert.ToUInt32(dr("Sync")), _
                                                                   Convert.ToDecimal(dr("Penalty")), _
                                                                   Convert.ToDecimal(dr("OGSG")), _
                                                                   Convert.ToDecimal(dr("CMR")), _
                                                                   Convert.ToDecimal(dr("Goshen")), _
                                                                   Convert.ToDecimal(dr("Insurance")), _
                                                                   Convert.ToDecimal(dr("OCost")), _
                                                                   Convert.ToDecimal(dr("OGSG_Renewal")), _
                                                                   Convert.ToDecimal(dr("De_pulse")))
                       AddToLog(Convert.ToString(dr("paytAdEntryID").ToString))
                 Next
        Catch ex As Exception
            AddToLog(ex.Message)
        End Try

End Sub
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
olaseun28
  • 5
  • 1
  • 1
  • 3

1 Answers1

0

In this code you're taking data from your DataTable and casting it into various types. Well, one of those values is null in the database, so .Net interprets that as as System.DbNull... And you can't cast that this way.

You could check for DbNull, handle that if it's found, and convert otherwise, when it's safe.

Here's a C# version of the same question: Unable to cast object of type 'System.DBNull' to type 'System.String`

Community
  • 1
  • 1
Brian MacKay
  • 31,133
  • 17
  • 86
  • 125
  • transactionID bigint Unchecked, transaction_date datetime Unchecked, bank int Checked, teller_number varchar(20)Unchecked, amount int Unchecked,generated_by nvarchar(50) Unchecked station varchar(20)Checked,customer_name varchar(50) Checked transaction_category int Unchecked,deposit_slip varchar(20) Checked sync int Unchecked, Penalty decimal(18, 2) Checked OGSG decimal(18, 2) Checked CMR decimal(18, 2) Checked Goshen decimal(18, 2) Checked Insurance decimal(18, 2)Checked OCost decimal(18, 2) Checked OGSG_Renewal decimal(18, 2)Checked De_pulse decimal(18, 2) Checked – olaseun28 Jun 16 '14 at 00:44
  • @Brain Mackay that's my MSSQLtable structure respectively what do you thinks I should do? – olaseun28 Jun 16 '14 at 00:45
  • @olaseun28 Table structure is really a whole other conversation, with other considerations. The problem here is that VT.Rows has some rows with null values. Null values are fine, but you need to check for System.DbNull and handle the nulls before you convert. If you just convert as you're doing here, eventually you will run into a null, and it will throw an exception. – Brian MacKay Jun 16 '14 at 12:28
  • @olaseun28 You should really take a long look at the link I provided, it has a lot of good detail on this problem. – Brian MacKay Jun 16 '14 at 12:30