Click here to Skip to main content
15,913,944 members

Comments by wsm.akh@gmail.com (Top 1 by date)

wsm.akh@gmail.com 6-Jan-18 13:23pm View    
I am getting error message "Conversion from type 'DBNull' to type 'String' is not valid." each time while navigating the records whenever any any field comes.

My code is;

Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class Form1
Dim CurrentRow As Integer
Dim DST As New DataSet
Dim ds As New DataSet
Dim dv As DataView
Dim cm As CurrencyManager
Dim dataadapter As OleDb.OleDbDataAdapter
Public CON As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Dot Net Projects\vb dot project complete example\DataMovement\DataMovement.accdb")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dataadapter = New OleDb.OleDbDataAdapter("SELECT * FROM Table1", CON)
filldatasetandview()
FormLoadKaro()
End Sub

Private Sub FormLoadKaro()
Try
CurrentRow = 0
CON.Open()
dataadapter = New OleDb.OleDbDataAdapter("SELECT * FROM Table1", CON)
dataadapter.Fill(DST, "Table1")
ShowData(CurrentRow)
CON.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

' Binding all fields
Public Sub ShowData(ByVal CurrentRow)
Try
TextBox1.Text = DST.Tables("Table1").Rows(CurrentRow)("ID")
DateTimePicker1.Text = DST.Tables("Table1").Rows(CurrentRow)("dob")
TextBox3.Text = DST.Tables("Table1").Rows(CurrentRow)("naam")
TextBox4.Text = DST.Tables("Table1").Rows(CurrentRow)("address")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

' Fill Data
Public Sub filldatasetandview()
ds = New DataSet
dataadapter.Fill(ds, "Table1")
dv = New DataView(ds.Tables("Table1"))
cm = CType(Me.BindingContext(dv), CurrencyManager)
End Sub

' Binding all fields
Public Sub bindfields()
TextBox1.DataBindings.Clear()
DateTimePicker1.DataBindings.Clear()
TextBox3.DataBindings.Clear()
TextBox4.DataBindings.Clear()

TextBox1.DataBindings.Add("text", dv, "ID")
DateTimePicker1.DataBindings.Add("text", dv, "dob")
TextBox3.DataBindings.Add("text", dv, "naam")
TextBox4.DataBindings.Add("text", dv, "address")
End Sub

Private Sub cmdPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPre.Click
Try
If CurrentRow <> 0 Then
CurrentRow -= 1
ShowData(CurrentRow)
Else
MsgBox("This is the First Record...")
End If
Catch ex As Exception
MsgBox("No record to show")
End Try
End Sub

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
If CurrentRow = DST.Tables("Table1").Rows.Count - 1 Then
MsgBox("This is the Last Record...")
Else
CurrentRow += 1
ShowData(CurrentRow)
End If
End Sub

Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
Try
CurrentRow = 0
ShowData(CurrentRow)
Catch ex As Exception
MsgBox("There is no record to show")
End Try
End Sub

Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
CurrentRow = DST.Tables("Table1").Rows.Count - 1
ShowData(CurrentRow)
End Sub
End Class