seano
Active member
- Joined
- Jul 4, 2012
- Messages
- 32
- Programming Experience
- Beginner
HI all my Insert query isnt working, i have all my database code in one class and i have called the insert query before and it worked fine, here is the insert function
clsDatamanagement this function is setup so all i have to pass it is the all the values to insert except the ID of the table im inserting into:
this getNextID function finds the next available ID in the table:
And Finally the Form code:
I had a similar problem with my update query and it was an simple fix, this should be a simple mistake somewhere but i cant get my finger on it, any help would be much appreciated.
I hope i have provided you with enough information for a quick solution, thanks for your time.
clsDatamanagement this function is setup so all i have to pass it is the all the values to insert except the ID of the table im inserting into:
VB.NET:
Public Sub Insert(ByVal tableName As String, ByVal insertClause As String, ByVal ID As String)
Try
Dim myOleDbConnection As New OleDbConnection(connectionString)
' Get the result as a Table - one record per object
myOleDbConnection.Open()
Dim myCommand As New OleDbCommand("INSERT INTO " & tableName & " VALUES (" & getNextID(tableName, ID) & ", " & insertClause, myOleDbConnection)
myCommand.ExecuteNonQuery()
myOleDbConnection.Close()
MsgBox("Saved Successfully!", MsgBoxStyle.Information, "POS System")
Catch ex As Exception
MsgBox("Saved UnSuccessfully!", MsgBoxStyle.Critical, "POS System")
End Try
End Sub
this getNextID function finds the next available ID in the table:
VB.NET:
Public Function getNextID(ByVal tableName As String, ByVal ID As String) As Integer
'get nextid
Dim myOleDbConnection As New OleDbConnection(connectionString)
Dim myOleDbDataAdapter As New OleDbDataAdapter("SELECT " & ID & " FROM " & tableName, myOleDbConnection)
Dim DSObject As New DataSet
'create and open the data connection
myOleDbConnection.Open()
'Put everything from Data Adapter into DATA SET
myOleDbDataAdapter.Fill(DSObject, "Employees")
Dim NextID As Integer = DSObject.Tables(0).Rows(DSObject.Tables(0).Rows.Count - 1).Item(0)
Return NextID + 1
End Function
And Finally the Form code:
VB.NET:
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
Dim DSUser As New DataSet
DSUser = DataManagement.SelectObjects("Employees", "Where FirstName= " & "'" & txtname.Text & "'")
Dim ID As Integer = -1
If DSUser.Tables(0).Rows.Count = 1 Then
ID = DSUser.Tables(0).Rows(0).Item("EmployeeID")
End If
If ID = -1 Then
MsgBox("Cant Find Employee", MsgBoxStyle.Critical, "POS System")
Else
DataManagement.Insert("TimeSheet", ID & ", '" & datatimepicker1.Value & "', '" & txthours.Text & "'", "TimeSheetID")
End If
End Sub
I had a similar problem with my update query and it was an simple fix, this should be a simple mistake somewhere but i cant get my finger on it, any help would be much appreciated.
I hope i have provided you with enough information for a quick solution, thanks for your time.