Public Class frmVisitor Private dbConnection As New OleDb.OleDbConnection() 'To add a DataAdapter Private daVisitor As OleDb.OleDbDataAdapter 'To add a CommandBuilder Private cmdbVisitor As OleDb.OleDbCommandBuilder Private dtVisitor As New DataTable Private rpVisitor As Integer = 0 Dim intNewVisitorID As Integer Private Sub frmVisitor_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs Handles Me.FormClosed 'Closing a Connection to a Data Source ta the time when Main Form Closed 'this is accomplished by calling the Close() method of the connection object dbConnection.Close() dbConnection.Dispose() End Sub Private Sub frmVisitor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dbConnection.ConnectionString = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =SNGPLDbase.mdb" daVisitor = New OleDb.OleDbDataAdapter("Select * From Visitor", dbConnection) 'To initialize the CommandBuilder object cmdbVisitor = New OleDb.OleDbCommandBuilder(daVisitor) 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. 'Used to display the current record in the data table 'Me.ShowVisitorRecord() End Sub Private Sub ShowVisitorRecord() If dtVisitor.Rows.Count = 0 Then 'When there is no record Me.cboVisitorCode.Text = "" Me.txtVisitorName.Text = "" Me.txtVehicleNo.Text = "" Exit Sub End If 'When there is record cboVisitorCode.Text = dtVisitor.Rows(rpVisitor)("VisitorCode") txtVisitorName.Text = dtVisitor.Rows(rpVisitor)("VisitorName") txtVehicleNo.Text = dtVisitor.Rows(rpVisitor)("VehicleNo") End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click If dtVisitor.Rows.Count = 0 Then 'When there is no record Dim intNewVisitorID = 10 'Initial value of VisitorCode Dim drNewRow As DataRow = dtVisitor.NewRow() drNewRow.Item("VisitorCode") = intNewVisitorID Me.cboVisitorCode.Items.Add(drNewRow.Item("VisitorCode")) Me.cboVisitorCode.Text = drNewRow.Item("VisitorCode") ' cboVisitorCode.Text = dtVisitor.Rows(rpVisitor)("VisitorCode") dtVisitor.Rows.Add(drNewRow) Exit Sub Else ' If there are any rows in the data table, ' move to the last and show the record. If dtVisitor.Rows.Count > 0 Then rpVisitor = dtVisitor.Rows.Count - 1 ' Me.ShowVisitorRecord() End If Dim drNewRow As DataRow = dtVisitor.NewRow() intNewVisitorID = drNewRow.Item("VisitorCode") + 10 'VisitorCode increase by 10 drNewRow.Item("VisitorCode") = intNewVisitorID 'To Insert new VisitorCode to Combobox In case of New Entry. Me.cboVisitorCode.Items.Add(drNewRow.Item("VisitorCode")) Me.cboVisitorCode.Text = drNewRow.Item("VisitorCode") dtVisitor.Rows.Add(drNewRow) End If End Sub 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
Try converting the VisitorCode like:
Plz u repost ur answer b/c nothing is seen in ur answer area
Really?
It's very clear in my comp.
ok that f9 when i tested on another brother
Pages
Add new comment