Adapter.Update does not change database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to enter a new row into a test database and have been
unsuccessful. The code compiles fine and runs with out runtime errors,
however after closing the form and checking the database it is the same.
Since this is my first time using a database any additional suggestions that
I should check on that are not obvious in my code snippet will be useful!

Sincere thanks,
Rock


private void button1_Click(object sender, EventArgs e)
{
// Create new entry
myDatabaseDataSet.UserNamesRow user =
myDatabaseDataSet.UserNames.NewUserNamesRow();
user.First = firstTextBox.Text;
user.Last = lastTextBox.Text;
myDatabaseDataSet.UserNames.Rows.Add(user);

// Save new entry
this.Validate();
this.userNamesBindingSource.EndEdit();
if(user.RowState != DataRowState.Added)
MessageBox.Show("No Changes Seen in DataSEt");
int result =
this.userNamesTableAdapter.Update(this.myDatabaseDataSet.UserNames);

//Display # of updated rows
MessageBox.Show(result.ToString());
}
 
Rock,

Does your database table not have a primary key, mostly it does not work if
that is not supported.

Cor
 
Hi,

Rock said:
I am trying to enter a new row into a test database and have been
unsuccessful. The code compiles fine and runs with out runtime errors,
however after closing the form and checking the database it is the same.
Since this is my first time using a database any additional suggestions
that
I should check on that are not obvious in my code snippet will be useful!

The code looks ok.

What's the value of "result" after update ? What DB are you using ?

HTH,
Greetings
 
Thanks for responding to my question. Yes I have a primary key assigned and
the value of "result" is, 1. And as for the type of database, I am not
entirely sure how much info I can give but I will try: it is a SQL Database
located on my computer (.mdf) there is only this one table that I am trying
to add to. Lastly, the VS project was just started from scratch so all
variable settings are at there defaults as to try and eliminate any variables.

I have been following examples to the T and have had no luck, I hope you can
find or recall things to try so I can get this thing going!

Sincere thanks,
Rock
 
Thanks for responding to my question. Yes I have a primary key assigned and
the value of "result" is, 1. And as for the type of database, I am not
entirely sure how much info I can give but I will try: it is a SQL Database
located on my computer (.mdf) there is only this one table that I am trying
to add to. Lastly, the VS project was just started from scratch so all
variable settings are at there defaults as to try and eliminate any variables.

I have been following examples to the T and have had no luck, I hope you can
find or recall things to try so I can get this thing going!

Sorry for the redundancy.

Sincere thanks,
Rock


PS. Is there an read-only setting that would allow this to happen? Where
everything looks like it is updating and going well but then the database
just doesn't take the changes.
 
Thanks for responding to my question. Yes I have a primary key assigned and
the value of "result" is, 1. And as for the type of database, I am not
entirely sure how much info I can give but I will try: it is a SQL Database
located on my computer (.mdf) there is only this one table that I am trying
to add to. Lastly, the VS project was just started from scratch so all
variable settings are at there defaults as to try and eliminate any variables.

I have been following examples to the T and have had no luck, I hope you can
find or recall things to try so I can get this thing going!

Sincere thanks,
Rock

PS. Is there any things that would cause the database to be read-only? Where
everything seems to go fine with the update but then the database just
doesn't take the changes.
 
Hi,

Rock said:
Thanks for responding to my question. Yes I have a primary key assigned
and
the value of "result" is, 1. And as for the type of database, I am not
entirely sure how much info I can give but I will try: it is a SQL
Database
located on my computer (.mdf) there is only this one table that I am
trying
to add to. Lastly, the VS project was just started from scratch so all
variable settings are at there defaults as to try and eliminate any
variables.

I have been following examples to the T and have had no luck, I hope you
can
find or recall things to try so I can get this thing going!

If result == 1 then i think there's little chance the update failed.

Is the .mdf file part of the VS project (visible in solution explorer) ?

If so, then most likely it's copied to bin/debug each time your app starts
and that's the one used by your app. But the next time your app starts it
gets overwritten and all previous changes made are lost.

An option is to change "|DataDirectory|\yourdb.mdf" into "..\...\yourdb.mdf"
within connectionString inside app.config file. This way it will always use
the project database (instead of a new copy)

HTH,
Greetings
 
Rock,

Let I ask my question in another way, what is the primary key. I don't see
you setting it?

Cor
 
You have found what I have overlooked being new to SQL databases in VStudio.
And I can't thank you enough for getting rid of this headache. Changing the
directory is still a little fuzzy; could you give detailed steps describing
where I can go about changing this?

Sincere thanks,
Rock
 
Hi,

Rock said:
You have found what I have overlooked being new to SQL databases in
VStudio.
And I can't thank you enough for getting rid of this headache. Changing
the
directory is still a little fuzzy; could you give detailed steps
describing
where I can go about changing this?

I'm new to this too, i'm not sure if i can tell you anything more then i
already did.

Check if you have a file called app.config (solution explorer).
Open it and look if you have a tag like <connectionStrings>.
Within that tag multiple connection strings can be defined, there should be
one.
Within that connectionString there should be something like
"...AttachDbFileName=|DataDirectory|\yourdb.mdf...".
Change it into "...AttachDbFileName=..\..\yourdb.mdf...".

If this doesn't work explain what's different and what version of vs you're
using.

Greetings
 
This almost works for me the connection is made but then an exceptions is
thrown when trying to fill the dataset from the database. Have you actually
tried this?

What I was able to do to temp. solve this was to setup my test database on
an extenal server and all is well!

Again sincere thanks,
Rock
 
Hi,

Rock said:
This almost works for me the connection is made but then an exceptions is
thrown when trying to fill the dataset from the database. Have you
actually
tried this?

Yes, i try (or have tried) most of the things i post :-). But i did say i'm
new to this too, so i haven't extensively tested this.

If you get an exception during fill i assume it can't find the DB. If you
found this "AttachDBFileName" thing, you could try an absolute path (the
project path + mdf file or elsewhere), but not the one in \bin\debug.

hth,
Greetings
 
Back
Top