Hello All,
I am a little new to Visual Studio, but I hold 3 years programming experience in both Java and C. I have created a program for my wife that allows her to organize and save her DVD collection to an access table. The program works just fine except for the save part. I can run my application, insert, delete, and edit the contents of the database, but it will not save. To my knowledge I am only working through the dataset and the data never hits the database file itself. I just need a little help here because I am excited to get this application rolling. It is a little useless if you can't save data. I have an access database named DVD_Library.mdf , data set, table binding source, table adapter, and a binding navigator that is working with this database. The binding navigator is fully functional except for the save button. I am a little confused because every other button works on the navigator except for the one I needed most. Most of my code is listed below. Help is appreciated.
Code:
(Form 1.cs)
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
DVD_Library{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e){
Application.Exit();}
private void button1_Click(object sender, EventArgs e){
if (textBox1.Text.Equals("") || textBox2.Text.Equals("") || comboBox1.Text.Equals("") || comboBox2.Text.Equals("") || comboBox3.Text.Equals("")){
MessageBox.Show("You have reached this message because one or more of your entries is invalid.\nPlease ensure all fields are filled out prior to submitting.");}
if (textBox1 == null || textBox2 == null || comboBox1 == null || comboBox2 == null || comboBox3 == null){
MessageBox.Show("You have reached this message because one or more of your entries is invalid.\nPlease ensure all fields are filled out prior to submitting.");}
else if (!textBox1.Text.Equals("") && !textBox2.Text.Equals("") && !comboBox1.Text.Equals("") && !comboBox2.Text.Equals("") && !comboBox3.Text.Equals("")){
dVD_LibraryDataSet.Table2.AddTable2Row(textBox1.Text, comboBox1.Text, comboBox2.Text, textBox2.Text, comboBox3.Text);
textBox1.Text =
"";textBox2.Text =
"";comboBox1.Text =
"";comboBox2.Text =
"";comboBox3.Text =
"";}
}
private void Form1_Load(object sender, EventArgs e){
// TODO: This line of code loads data into the 'dVD_LibraryDataSet.Table2' table. You can move, or remove it, as needed. this.table2TableAdapter.Fill(this.dVD_LibraryDataSet.Table2);}
private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e){
dVD_LibraryDataSet.Table2.Select();
}
private void movieTitleToolStripMenuItem_Click(object sender, EventArgs e){
if (!movieTitleToolStripMenuItem.Checked){
this.movieTitleDataGridViewTextBoxColumn.Visible = false;}
if (movieTitleToolStripMenuItem.Checked){
this.movieTitleDataGridViewTextBoxColumn.Visible = true;}
}
private void yearToolStripMenuItem_Click(object sender, EventArgs e){
if (!yearToolStripMenuItem.Checked){
this.yearDataGridViewTextBoxColumn.Visible = false;}
if (yearToolStripMenuItem.Checked){
this.yearDataGridViewTextBoxColumn.Visible = true;}
}
private void genreToolStripMenuItem_Click(object sender, EventArgs e){
if (!genreToolStripMenuItem.Checked){
this.genreDataGridViewTextBoxColumn.Visible = false;}
if (genreToolStripMenuItem.Checked){
this.genreDataGridViewTextBoxColumn.Visible = true;}
}
private void actorsToolStripMenuItem_Click(object sender, EventArgs e){
if (!actorsToolStripMenuItem.Checked){
this.actorsDataGridViewTextBoxColumn.Visible = false;}
if (actorsToolStripMenuItem.Checked){
this.actorsDataGridViewTextBoxColumn.Visible = true;}
}
private void mediaTypeToolStripMenuItem_Click(object sender, EventArgs e){
if (!mediaTypeToolStripMenuItem.Checked){
this.mediaTypeDataGridViewTextBoxColumn.Visible = false;}
if (mediaTypeToolStripMenuItem.Checked){
this.mediaTypeDataGridViewTextBoxColumn.Visible = true;}
}
private void aboutDVDLibraryToolStripMenuItem_Click(object sender, EventArgs e){
AboutBox1 about = new AboutBox1();about.Show();
}
private void saveToolStripButton_Click(object sender, EventArgs e){
//I want to be able to let the user save data here MessageBox.Show("File Saved Successfully");}
private void edittoolStripButton_Click(object sender, EventArgs e){
if (edittoolStripButton.Checked.Equals(true)){
edittoolStripButton.ForeColor =
Color.Red;dataGridView1.ReadOnly =
false; MessageBox.Show("The DVD Library can now be edited. Be sure to click the Edit Library button again once you are finished.");}
else if (edittoolStripButton.Checked.Equals(false)){
edittoolStripButton.ForeColor =
Color.DodgerBlue;dataGridView1.ReadOnly =
true;}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e){
//I want to be able to let the user save data here MessageBox.Show("File Saved Successfully");}
}
}

How to update an Access Database (C#)
rafeek
Thank you Depp,
You solved my problem with only two lines of code. I thought that it would be much more difficult than that. Just so everyone knows how to update an ms access database I am posting the fixed code below thanks again Depp.
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
DVD_Library{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e){
Application.Exit();}
private void button1_Click(object sender, EventArgs e){
if (textBox1.Text.Equals("") || textBox2.Text.Equals("") || comboBox1.Text.Equals("") || comboBox2.Text.Equals("") || comboBox3.Text.Equals("")){
MessageBox.Show("You have reached this message because one or more of your entries is invalid.\nPlease ensure all fields are filled out prior to submitting.");}
if (textBox1 == null || textBox2 == null || comboBox1 == null || comboBox2 == null || comboBox3 == null){
MessageBox.Show("You have reached this message because one or more of your entries is invalid.\nPlease ensure all fields are filled out prior to submitting.");}
else if (!textBox1.Text.Equals("") && !textBox2.Text.Equals("") && !comboBox1.Text.Equals("") && !comboBox2.Text.Equals("") && !comboBox3.Text.Equals("")){
dVD_LibraryDataSet.Table2.AddTable2Row(textBox1.Text, comboBox1.Text, comboBox2.Text, textBox2.Text, comboBox3.Text);
textBox1.Text =
"";textBox2.Text =
"";comboBox1.Text =
"";comboBox2.Text =
"";comboBox3.Text =
"";}
}
private void Form1_Load(object sender, EventArgs e){
// TODO: This line of code loads data into the 'dVD_LibraryDataSet.Table2' table. You can move, or remove it, as needed. this.table2TableAdapter.Fill(this.dVD_LibraryDataSet.Table2);}
private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e){
dVD_LibraryDataSet.Table2.Select();
}
private void movieTitleToolStripMenuItem_Click(object sender, EventArgs e){
if (!movieTitleToolStripMenuItem.Checked){
this.movieTitleDataGridViewTextBoxColumn.Visible = false;}
if (movieTitleToolStripMenuItem.Checked){
this.movieTitleDataGridViewTextBoxColumn.Visible = true;}
}
private void yearToolStripMenuItem_Click(object sender, EventArgs e){
if (!yearToolStripMenuItem.Checked){
this.yearDataGridViewTextBoxColumn.Visible = false;}
if (yearToolStripMenuItem.Checked){
this.yearDataGridViewTextBoxColumn.Visible = true;}
}
private void genreToolStripMenuItem_Click(object sender, EventArgs e){
if (!genreToolStripMenuItem.Checked){
this.genreDataGridViewTextBoxColumn.Visible = false;}
if (genreToolStripMenuItem.Checked){
this.genreDataGridViewTextBoxColumn.Visible = true;}
}
private void actorsToolStripMenuItem_Click(object sender, EventArgs e){
if (!actorsToolStripMenuItem.Checked){
this.actorsDataGridViewTextBoxColumn.Visible = false;}
if (actorsToolStripMenuItem.Checked){
this.actorsDataGridViewTextBoxColumn.Visible = true;}
}
private void mediaTypeToolStripMenuItem_Click(object sender, EventArgs e){
if (!mediaTypeToolStripMenuItem.Checked){
this.mediaTypeDataGridViewTextBoxColumn.Visible = false;}
if (mediaTypeToolStripMenuItem.Checked){
this.mediaTypeDataGridViewTextBoxColumn.Visible = true;}
}
private void aboutDVDLibraryToolStripMenuItem_Click(object sender, EventArgs e){
AboutBox1 about = new AboutBox1();about.Show();
}
private void saveToolStripButton_Click(object sender, EventArgs e){
this.table2TableAdapter.Update(this.dVD_LibraryDataSet); this.table2TableAdapter.Fill(this.dVD_LibraryDataSet.Table2); MessageBox.Show("File Saved Successfully");}
private void edittoolStripButton_Click(object sender, EventArgs e){
if (edittoolStripButton.Checked.Equals(true)){
edittoolStripButton.ForeColor =
Color.Red;dataGridView1.ReadOnly =
false; MessageBox.Show("The DVD Library can now be edited. Be sure to click the Edit Library button again once you are finished.");}
else if (edittoolStripButton.Checked.Equals(false)){
edittoolStripButton.ForeColor =
Color.DodgerBlue;dataGridView1.ReadOnly =
true;}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e){
this.table2TableAdapter.Update(this.dVD_LibraryDataSet); this.table2TableAdapter.Fill(this.dVD_LibraryDataSet.Table2); MessageBox.Show("File Saved Successfully");}
}
}
hedish
I was reading this topic because I've got one problem with update a MSAccess database with table adapters.
I've readed the C# help but I don't understand the way of working of table adapters, especially about how they handle the data in a insert operation.
My code is very simple:
clienteTA.Insert(txtNome.Text, txtNIF.Text, txtMorada.Text);
clienteTA.Update(localDS.Cliente);
where:
private FOGest.localDataSetTableAdapters.ClienteTableAdapter clienteTA;
private localDataSet localDS;
declared in the designer.cs file.
In the solution explorer I've also the mdb file with the option "Copy to output directory: Copy Always". I've tried the other options, but no one have worked.
All that I need is update the mdb file with the inputed data in the text boxes.
Can anyone help me and explain the way of working of table adapters in this cases
By the way, if you can explain the difference between the Fill and the GetData methods I would be very thankfull.
Thanks in advance.
Nelson Neves
Tommosimmo
I've not read all of your code, but i pressed ctrl+f and searched for the keyword "Update" and didn't find it ;-)
To update a database, you've got to use the Update method throught tableadapter's instances.
so, if you filled the dataset with this line:
this.table2TableAdapter.Fill(this.dVD_LibraryDataSet.Table2);
you've got to update the DB (throught tableadapter) with something like this:
this.table2TableAdapter.Update(this.dVD_LibraryDataSet);