adigilani
New member
- Joined
- Oct 15, 2009
- Messages
- 3
- Programming Experience
- Beginner
Hi
Need some help here. I am learning to code in VB 2008 and my first assignment at school is to build a vending machine. I have a database and i am able to read the values of it in a variable using
Dim sConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=vendingMachine.mdb" ' Holding the connection string
Dim conn As New OleDbConnection ' Initilize a OLE connection object variable
Dim oleDBAdapter As New OleDbDataAdapter ' Initilize a OLE DB Adapter object variable
Dim ds As New DataSet ' Initilize a dataset object variable
conn = New OleDbConnection(sConnString) ' make instance of the connection
Try ' trying to open the db connection
conn.Open() ' open the connection
Catch ex As Exception ' catching error
MsgBox("Database connection Error...!")
mainFrm.Close()
End Try
end sub
sqlString = "SELECT * FROM myProducts"
oleDBAdapter = New OleDbDataAdapter(sqlString, conn)
oleDBAdapter.Fill(ds, "myProducts Table") ' retriving the products
sqlString = "SELECT * FROM MachineAdmin"
oleDBAdapter.SelectCommand.CommandText = sqlString
oleDBAdapter.Fill(ds, "MachineAdmin Table") ' retriving the coins
then i am storing the coins in an array
tempVar = 0
If ds.Tables(1).Rows.Count > 0 Then
Do While ds.Tables(1).Rows.Count > tempVar
For i = 0 To ds.Tables(1).Columns.Count - 2
machineCoinBank(i) = ds.Tables(1).Rows(tempVar).Item(i + 1)
MsgBox(machineCoinBank(i))
Next
tempVar = tempVar + 1
Loop
Else
MsgBox("Error loading values stored in coins in the variable...!")
Application.Exit()
End If
and then after receiving a coin from the user i want to add the coin to the appropriate type of coin list and update the database
i've tried using the follow
For tempVal = 0 To 7
ds.Tables(1).Rows(0).BeginEdit()
ds.Tables(1).Rows(0).Item(tempVal + 1) = CInt(machineCoinBank(tempVal)) + CInt(recievedCoins(tempVal)) ' Reset the recieved coins counter array
oleDBAdapter.Update(ds.Tables(1))
Next
The above all doesn't give me any exception or any error but the database is not getting updated.
The database is in the same folder as the project. I have checked it and nothing has happened to the values and i have checked the bin as well and nothing has happened.
Can anyone please help. I have tried looking at the internet but cannot find any thing which can point me to a right direction.
also i want to know
1) if i have the database in a different folder what will happen to it when i use the "add new data source". Does it get copied into the project folder. Well, i saw it did but what i actually mean to ask is that which database is in use then. the one in the project folder or the one in the original folder
2) if the same dataset defined on this form is accessible on the next form or i have to create dataset again. If yes then after manipulating with the data there how i can refresh the dataset when i come back to the main form.
Please.
Need some help here. I am learning to code in VB 2008 and my first assignment at school is to build a vending machine. I have a database and i am able to read the values of it in a variable using
Dim sConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=vendingMachine.mdb" ' Holding the connection string
Dim conn As New OleDbConnection ' Initilize a OLE connection object variable
Dim oleDBAdapter As New OleDbDataAdapter ' Initilize a OLE DB Adapter object variable
Dim ds As New DataSet ' Initilize a dataset object variable
conn = New OleDbConnection(sConnString) ' make instance of the connection
Try ' trying to open the db connection
conn.Open() ' open the connection
Catch ex As Exception ' catching error
MsgBox("Database connection Error...!")
mainFrm.Close()
End Try
end sub
sqlString = "SELECT * FROM myProducts"
oleDBAdapter = New OleDbDataAdapter(sqlString, conn)
oleDBAdapter.Fill(ds, "myProducts Table") ' retriving the products
sqlString = "SELECT * FROM MachineAdmin"
oleDBAdapter.SelectCommand.CommandText = sqlString
oleDBAdapter.Fill(ds, "MachineAdmin Table") ' retriving the coins
then i am storing the coins in an array
tempVar = 0
If ds.Tables(1).Rows.Count > 0 Then
Do While ds.Tables(1).Rows.Count > tempVar
For i = 0 To ds.Tables(1).Columns.Count - 2
machineCoinBank(i) = ds.Tables(1).Rows(tempVar).Item(i + 1)
MsgBox(machineCoinBank(i))
Next
tempVar = tempVar + 1
Loop
Else
MsgBox("Error loading values stored in coins in the variable...!")
Application.Exit()
End If
and then after receiving a coin from the user i want to add the coin to the appropriate type of coin list and update the database
i've tried using the follow
For tempVal = 0 To 7
ds.Tables(1).Rows(0).BeginEdit()
ds.Tables(1).Rows(0).Item(tempVal + 1) = CInt(machineCoinBank(tempVal)) + CInt(recievedCoins(tempVal)) ' Reset the recieved coins counter array
oleDBAdapter.Update(ds.Tables(1))
Next
The above all doesn't give me any exception or any error but the database is not getting updated.
The database is in the same folder as the project. I have checked it and nothing has happened to the values and i have checked the bin as well and nothing has happened.
Can anyone please help. I have tried looking at the internet but cannot find any thing which can point me to a right direction.
also i want to know
1) if i have the database in a different folder what will happen to it when i use the "add new data source". Does it get copied into the project folder. Well, i saw it did but what i actually mean to ask is that which database is in use then. the one in the project folder or the one in the original folder
2) if the same dataset defined on this form is accessible on the next form or i have to create dataset again. If yes then after manipulating with the data there how i can refresh the dataset when i come back to the main form.
Please.