How to Increment datarow by value 10

Submitted by velovely on
  1. Public Class frmVisitor
  2.  
  3.     Private dbConnection As New OleDb.OleDbConnection()
  4.     'To add a DataAdapter
  5.     Private daVisitor As OleDb.OleDbDataAdapter
  6.     'To add a CommandBuilder
  7.     Private cmdbVisitor As OleDb.OleDbCommandBuilder Private dtVisitor As New DataTable
  8.     Private rpVisitor As Integer = 0
  9.     Dim intNewVisitorID As Integer
  10.  
  11.     Private Sub frmVisitor_FormClosed(ByVal sender As Object,  ByVal e As System.Windows.Forms.FormClosedEventArgs Handles Me.FormClosed
  12.  
  13.         'Closing a Connection to a Data Source ta the time when Main Form Closed
  14.         'this is accomplished by calling the Close() method of the connection object
  15.         dbConnection.Close()
  16.         dbConnection.Dispose()
  17.     End Sub
  18.  
  19.     Private Sub frmVisitor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20.  
  21.         dbConnection.ConnectionString = _
  22.         "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =SNGPLDbase.mdb"
  23.  
  24.          daVisitor = New OleDb.OleDbDataAdapter("Select * From Visitor", dbConnection)
  25.         'To initialize the CommandBuilder object
  26.         cmdbVisitor = New OleDb.OleDbCommandBuilder(daVisitor)
  27.  
  28.         daVisitor.Fill(dtVisitor) 'Because the DataTable doesn't hold a connection to the data source, you don't need to close it when you're finished.
  29.         'Used to display the current record in the data table
  30.         'Me.ShowVisitorRecord()
  31.     End Sub
  32.  
  33.     Private Sub ShowVisitorRecord()
  34.  
  35.         If dtVisitor.Rows.Count = 0 Then 'When there is no record
  36.  
  37.             Me.cboVisitorCode.Text = ""
  38.             Me.txtVisitorName.Text = ""
  39.             Me.txtVehicleNo.Text = ""
  40.             Exit Sub
  41.  
  42.         End If  'When there is record
  43.  
  44.         cboVisitorCode.Text = dtVisitor.Rows(rpVisitor)("VisitorCode")
  45.         txtVisitorName.Text = dtVisitor.Rows(rpVisitor)("VisitorName")
  46.         txtVehicleNo.Text = dtVisitor.Rows(rpVisitor)("VehicleNo")
  47.  
  48.     End Sub
  49.  
  50.    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  51.  
  52.         If dtVisitor.Rows.Count = 0 Then 'When there is no record
  53.  
  54.             Dim intNewVisitorID = 10 'Initial value of VisitorCode
  55.             Dim drNewRow As DataRow = dtVisitor.NewRow()
  56.             drNewRow.Item("VisitorCode") = intNewVisitorID
  57.  
  58.             Me.cboVisitorCode.Items.Add(drNewRow.Item("VisitorCode"))
  59.             Me.cboVisitorCode.Text = drNewRow.Item("VisitorCode")
  60.  
  61.             '  cboVisitorCode.Text = dtVisitor.Rows(rpVisitor)("VisitorCode")
  62.             dtVisitor.Rows.Add(drNewRow)
  63.  
  64.             Exit Sub
  65.         Else
  66.  
  67.             ' If there are any rows in the data table,
  68.             ' move to the last and show the record.
  69.  
  70.             If dtVisitor.Rows.Count > 0 Then
  71.                 rpVisitor = dtVisitor.Rows.Count - 1
  72.                 ' Me.ShowVisitorRecord()
  73.             End If
  74.  
  75.             Dim drNewRow As DataRow = dtVisitor.NewRow()
  76.             intNewVisitorID = drNewRow.Item("VisitorCode") + 10 'VisitorCode increase by 10
  77.             drNewRow.Item("VisitorCode") = intNewVisitorID
  78.  
  79.             'To Insert new VisitorCode to Combobox In case of New Entry.
  80.             Me.cboVisitorCode.Items.Add(drNewRow.Item("VisitorCode"))
  81.             Me.cboVisitorCode.Text = drNewRow.Item("VisitorCode")
  82.             dtVisitor.Rows.Add(drNewRow)
  83.  
  84.         End If
  85.     End Sub
  86. End Class
I have three fields on the front end. Of which two are type text and one combobox. VisitorCode typefield comboBox VisitorName typefield Textbox VehicleNo typefield Textbox When I click the combobox , it will display all VisitorCode record present. Then of which I take a selection of VisitorCode as a result it show its specific two record i.e. VisitorName & VehicleNo. Further when I click the btnAdd button it generate a new VisitorCode on comboBox field and the data for my other two field of nature textfield are manually enterd. For example. VisitorCode 50 VisitorName abc VehicleNo xyz250 Help..Visitor Code=10,20,30,40 are present on combobox suppose. Means when user click the Add button then it generate the value 50, and for next time it generate 60. etc. When I click the save button all the three field are stored in the database upto now I had not write a code for that bc when my first problem of Add button will handle then I move forward. intNewVisitorID = drNewRow.Item("VisitorCode") + 10 when i run the program the process will stop at the above line and display the following error Operator '+' is not defined for type 'DBNull' and type 'Integer'. but already there is a data in the database