Question Writing to an Access Database

hacket

New member
Joined
Nov 5, 2008
Messages
4
Programming Experience
1-3
Hello,

I'm attempting to write data to an access database from a couple of text boxes on the form I'm working with. I get no errors, but the data doesn't appear in the database when I open it up. Anyone know what I'm missing here, because I'm using multiple examples of code that I've found.

VB.NET:
 Dim command As New OleDbCommand
        Dim connection As New OleDbConnection

        connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\RecipesDatabase.mdb"

        command.CommandText = "INSERT INTO [Recipes] (Name, Ingredients) VALUES(@p1, @p2)"
        command.Connection = connection

        command.Parameters.Add("@p1", OleDbType.VarChar).Value = txtName.Text
        command.Parameters.Add("@p2", OleDbType.VarChar).Value = txtIngredients.Text

        connection.Open()
        command.ExecuteNonQuery()
        connection.Close()

Thank You.
 
If there are no errors then it's working. You're just looking in the wrong place. To confirm that the data is being saved change this:
VB.NET:
command.ExecuteNonQuery()
to this:
VB.NET:
MessageBox.Show(command.ExecuteNonQuery() & "records were saved.")
If that says "0 records were saved." then you're correct that the data is not being saved, but it won't say that. It will say "1 records were saved.".

I'm guessing that you're looking in the MDB file that you added to your project. That's not the database that the data is being saved to. Each time you build your project a copy of that database is created in the output folder, i.e. where the EXE is created. THAT is the database you'd have to look in to find the data. Also, if you exit the app and then run it again in the debugger, a new copy of the database will be created, overwriting your changes.

Read this.
 
Yea it did exactly what you said it would do. And it overwrites the database. How would I get around this? I'm kinda new to this, and appreciate your help a lot.
 
Yea it did exactly what you said it would do. And it overwrites the database. How would I get around this? I'm kinda new to this, and appreciate your help a lot.
Argh!!! I specifically posted a link that told you how to get around it. It explained what Copy Always and Copy If Newer do.
 
Argh!!! I specifically posted a link that told you how to get around it. It explained what Copy Always and Copy If Newer do.

The link in your post seems broken? I see it as h t t p : / / e x e c u t e n o n q u e r y ( ) (without spaces)

Hacket, jmc may have intended to post the link from my signature, DNU. You can take a read of it if you like; it may contain more info you find helpful
 
The link in your post seems broken? I see it as h t t p : / / e x e c u t e n o n q u e r y ( ) (without spaces)

Hacket, jmc may have intended to post the link from my signature, DNU. You can take a read of it if you like; it may contain more info you find helpful
Oopsy! Noone mentioned it. It was supposed to be:

How to: Manage Local Data Files in Your Project
 
Oopsy! Noone mentioned it.

Users are weird, aren't they? We rebooted a machine here and noone complained that half the website was down for 4 days (Service was set to startup-type Manual, and the tech who rebooted the box didnt check it was actually doing all of its jobs)
 
Argh!!! I specifically posted a link that told you how to get around it. It explained what Copy Always and Copy If Newer do.

I suppose I should have mentioned that it didn't work, but I've had so much experience with people jumping down my throat about...well what you just did...that I didn't feel like wasting my keystrokes and being accused of not clicking the link right. (I've had that)

Thank you to everyone though. I got that and many issues wrapped up.:cool:
 

Latest posts

Back
Top