|
-
Nov 24th, 2009, 08:53 PM
#1
Thread Starter
New Member
I am having trouble with connecting to this database and getting updated.
Ive never done a database before and i found this in one of my books... I think it should work. The part of the program that is in bold is the issue i get an error that says "Update unable to find TableMapping['Table'] or DataTable 'Table'." From the da.Update(ds)... Please help ive been trying to figure this out for hours!!!!!!!!!!! Thank you
Public Class retailForm
Dim tempPrice As Double
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Dim inc As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addItemButton.Click
Dim smallDishPrice As Double
Dim smallConePrice As Double
Const tax As Double = 0.07975
Dim totalTax As Double
Dim total As Double
If (itemsTextBox.Text.ToUpper = "SD") Then
smallDishPrice = 1.1
tempPrice = tempPrice + smallDishPrice
itemsOrderedListBox.Items.Add("Small Dish @ " & smallDishPrice.ToString("C2"))
ElseIf (itemsTextBox.Text.ToUpper = "SC") Then
smallConePrice = 2
tempPrice = tempPrice + smallConePrice
itemsOrderedListBox.Items.Add("Small Cone @ " & smallConePrice.ToString("C2"))
End If
totalTax = tax * tempPrice
subtotalLabel.Text = tempPrice.ToString("C2")
taxLabel.Text = totalTax.ToString("C2")
total = tempPrice + totalTax
totalLabel.Text = total.ToString("C2")
inc = 0
If inc <> -1 Then
MessageBox.Show("Yes")
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("itemlog").NewRow()
dsNewRow.Item("smallcone") = itemsTextBox.Text
' dsNewRow.Item("Surname") = txtSurname.Text
ds.Tables("itemlog").Rows.Add(dsNewRow)
da.Update(ds)
MsgBox("New Record added to the Database")
End If
itemsTextBox.Focus()
itemsTextBox.Text = ""
End Sub
Private Sub removeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeButton.Click
Dim CurrPos As Integer = itemsOrderedListBox.SelectedIndex
itemsOrderedListBox.Items.RemoveAt(CurrPos)
End Sub
Private Sub retailForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con As New OleDb.OleDbConnection
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\KBCustard1.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM log"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "itemlog")
con.Close()
inc = -1
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Printing
'If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'PrintDocument1.Print()
'End If
End Sub
End Class
-
Nov 25th, 2009, 09:35 AM
#2
Re: I am having trouble with connecting to this database and getting updated.
How about a different approach? The following uses simple SQL to retrieve and add a record if the find operation fails.
Create a MDB under Bin\Debug\Data named demo.mdb with a table named People with two string fields PersonName and ContactPosition.
Add the following code to your project, build and run.
Code:
Private Sub AddRecordIfPersonNotInTableDemo()
Using cn As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data\demo.mdb;")
Dim da As New OleDb.OleDbDataAdapter( _
"SELECT PersonName, ContactPosition FROM People;", cn)
Dim ds As New DataSet
da.Fill(ds)
Dim dv As DataView
dv = ds.Tables(0).DefaultView
dv.Sort = "PersonName"
Dim result As Integer = dv.Find("Mary Jane Smith")
If result = -1 Then
cn.Open()
Console.WriteLine("Adding")
Dim cmd As New OleDb.OleDbCommand With _
{.CommandText = _
<SQL>
INSERT INTO People
(PersonName, ContactPosition)
VALUES ('Mary Jane Smith','Manager')
</SQL>.Value, _
.Connection = cn}
cmd.ExecuteNonQuery()
Else
Console.WriteLine("Found")
End If
End Using
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|