473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problems adding a new row to an ms access database via VB.net

11
Im getting the "Syntax error in INSERT INTO statement." at my
da.Update(ds, "Expenses") line in code... heres my full code to follow.

------------------------------------------------------------------------------------------------------------
Private Sub ConnectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectToolStripMenuItem.Click

con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\eneal\Desktop\BudgetingDatabase.mdb"

con.Open()

MsgBox("A Connection to the Database is now open")




sqlExp = "Select * FROM Expenses"
da = New OleDb.OleDbDataAdapter(sqlExp, con)
da.Fill(ds, "Expenses")

End sub
----------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------

Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click



Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow

dsNewRow = ds.Tables("Expenses").NewRow()
dsNewRow.Item("Expense Description") = Expense_DescriptionTextBox.Text
ds.Tables("Expenses").Rows.Add(dsNewRow)
da.Update(ds, "Expenses") ****This is the line my problems at*

MsgBox("New Record added to the Database")


End Sub
------------------------------------------------------------------------------------------------------



I've searched high an low to an answer for this problem but nothing has seemed remotly close to what I'm experiencing.
Sep 20 '07 #1
8 2439
if you want to update the data from the dataadapter you should keep the priamry key in your table
Sep 21 '07 #2
kenobewan
4,871 Expert 4TB
What is your insert statement?
Sep 21 '07 #3
Nimion
11
Well I tried it with and without a primary key. And I was under the impression the Command Builder built the insert into statment automatically based upon the select?
Sep 21 '07 #4
kenobewan
4,871 Expert 4TB
I'm not good at impressions, so I'll just my opinion. If you dont have an insert statement, usually in the dataadapter, there will be no insertion. What software are you using? I'd be reluctant to use a 'command builder' that based its insert statements on my select statements.
Sep 22 '07 #5
Use only one adapter and only one select statement of the updatable table. Easily you can the error .

Otherwise
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow

' add the following line
da.insertcommand=cb.getinsertcommand()

dsNewRow = ds.Tables("Expenses").NewRow()
dsNewRow.Item("Expense Description") = Expense_DescriptionTextBox.Text
ds.Tables("Expenses").Rows.Add(dsNewRow)
da.Update(ds, "Expenses") ****This is the line my problems at*

MsgBox("New Record added to the Database")


Use the select statement and fill the adapter after that you perform the row addition.
Sep 24 '07 #6
Nimion
11
Ok I made sure I had one adpater, and it was being filled. I went into debug mode and took a look at the insert command that was built and it was correct. But I get that same error at the same spot... Insert Into was wrong.. I tried playing with it for a few hours but it seems something isnt matching up right behind the scenes and I cant seem to pinpoint it.
Sep 24 '07 #7
Nimion
11
Ok I've re-written the entire code from scratch... Now I get an entirly different error. The dreaded "No value given for one or more required parameters." at the da.update line... Now I thought the ?'s meant those were parameters? I replaced all the ?'s with 0's for a test, and it added a new row....with all 0's. So I know its something with the ?'s and it not seeing the parameters in the add.row..

New Code Below:

Dim CN As New OleDb.OleDbConnection

CN.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\eneal\Desktop\BudgetingDatabase.mdb"

'Open connection
cn.Open()

'Create Select Statement
Dim sql As String = "SELECT [Ref Number], [Expense Amount], [Expense Description], [Expense Date], Vendor, [PO Number], [Req Number] FROM Expenses"

'Create update/insert statement
Dim updcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(" Insert Into Expenses ([Ref Number], [Expense Amount], [Expense Description], [Expense Date], Vendor, [PO Number], [Req Number]) Values (?,?,?,?,?,?,?)")

Dim da As New OleDb.OleDbDataAdapter(sql, CN)

Dim ds As New DataSet()
da.FillSchema(ds, SchemaType.Source, "Expenses")
'Fill the DataTable.
da.Fill(ds, "Expenses")

' Create an auxiliary CommandBuilder object for this DataAdapter.
Dim cmdBuilder As New OleDb.OleDbCommandBuilder(da)
da.InsertCommand = updcmd
da.DeleteCommand = cmdBuilder.GetDeleteCommand
da.UpdateCommand = cmdBuilder.GetUpdateCommand

With ds.Tables("Expenses")
Dim dr As DataRow = .NewRow
dr("Expense Description") = "QWERTY Test"
dr("Ref Number") = 5
dr("Expense Amount") = 5
dr("Expense Date") = "5/5/05"
dr("Vendor") = "Fiftys"
dr("PO Number") = 555
dr("Req Number") = 5555
.Rows.Add(dr)
End With

'Send changes to the database, and disconnect.
da.Update(ds, "Expenses")

cn.Close()
Sep 24 '07 #8
Nimion
11
Well I figured it out...I did something revolutionary. I went into the database and made all the fieldnames without a space between them -.-'...

It seems that the commandbuilder wasnt putting in the [ ]'s and I didnt know how to get it to use the bracekts... so I just made that small modification (doesnt really affect anything). And now it works like a charm!... Lol. Thanks for everyone who attempted to help me :)
Sep 24 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sérgio Almeida | last post by:
Greetings I'm having problems adding records to an access database. What I want to do is very simple. Here is my code. __CODE_START__ dim sqlStatement sqlStatement="insert into...
6
by: Frank Wilson | last post by:
Tom, It sounds to me like ASP, not ASP.NET is handling the request for WebForm1.aspx. This is most likely an IIS config issue that may have been caused by order of installation or...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
15
by: sflynn | last post by:
I've just created a new program in Visual Studio 2005, Visual Basic. I can publish, install, and run the program just fine as long as the user is a member of the "Domain Admins" group. My problem...
18
JamesDC
by: JamesDC | last post by:
Hi, So I'm working with an Access 2002 database for waste managemnt. The person in my role before my put together the program before he left and now I'm in charge of it. After updating a few...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.