here is the code i modified it for my own purpose
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'declare local variables and objects...
Dim intPosition As Integer, intMaxID As Integer
Dim strID As String
Dim objCommand As OleDbCommand = New OleDbCommand()
'save the current record position...
intPosition = objCurrencyManager.Position
'initialize a new instance of the dataset object...
objDataSet = New DataSet()
'open the database connection...
objConnection.Open()
'initialize a new instance of oledbadapter object...
objDataAdapter = New OleDbDataAdapter( _
"SELECT MAX(BillNO) as MaxID " & _
"FROM Logs WHERE BillNo LIKE 'DM%'", objConnection)
'fill the dataset object with data...
objDataAdapter.Fill(objDataSet, "Logs")
'if maxid column is null...
If objDataSet.Tables("Logs").Rows(0).Item("MaxID") Is _
System.DBNull.Value Then
'set a default value of 1000...
intMaxID = 1000
Else
'otherwise set the strid variable to the value of MaxID...
strID = CStr(objDataSet.Tables("Logs").Rows(0).Item("MaxID "))
'get the integer part of the string...
intMaxID = CInt(strID.Remove(0, 2))
'increment the value...
intMaxID += 1
End If
'finally set the new id...
strID = intMaxID
'set the oledbcommand object properties.
objCommand.Connection = objConnection
objCommand.CommandText = "INSERT INTO Logs " & _
"(BillNo,StartTime,EndTime,ComputerUsage,Amoun t) " & _
"VALUES(?,?,?,?,?);"
objCommand.CommandType = CommandType.Text
'add parameters for the placeholders in the sql in the
'commandtext property...
'parameter fo rthe BillNo...
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(0).Direction = ParameterDirection.Input
objCommand.Parameters.Item(0).DbType = DbType.Int16
objCommand.Parameters.Item(0).Value = strID
'parameter for the Login...
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(1).Direction = ParameterDirection.Input
objCommand.Parameters.Item(1).DbType = DbType.Time
objCommand.Parameters.Item(1).Value = txtLogin.Text
'parameter for the Logout...
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(2).Direction = ParameterDirection.Input
objCommand.Parameters.Item(2).DbType = DbType.Time
objCommand.Parameters.Item(2).Value = txtLogout.Text
'parameter for the Timeused...
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(3).Direction = ParameterDirection.Input
objCommand.Parameters.Item(3).DbType = DbType.Time
objCommand.Parameters.Item(3).Value = txtTimeUse.Text
'parameter for the Amount
objCommand.Parameters.Add(New OleDbParameter())
objCommand.Parameters.Item(4).Direction = ParameterDirection.Input
objCommand.Parameters.Item(4).DbType = DbType.Currency
objCommand.Parameters.Item(4).Value = txtAmount.Text
'execute the oledbcommand object to insert the new data...
Try
objCommand.ExecuteNonQuery().ToString()
Catch err As OleDbException
MessageBox.Show(err.Message)
End Try
'close the connection...
objConnection.Close()
'fill the dataset and bind the fields...
FillDataSetAndView()
BindFields()
'set the record position to the one that we saved
objCurrencyManager.Position = intPosition
ShowPosition()
'display a message that the record was added...
'clean up...
objDataSet = Nothing
objDataAdapter = Nothing
objCommand = Nothing
End Sub
HEY_HEY
|