Solved

TableAdapter.Update(tableName); does not update table

Posted on 2008-10-04
11
1,358 Views
Last Modified: 2013-12-17
I am new to connecting to an Access database via the multitude of automated tools provided by Visual Studio 2005, but find that my call to Update() does not work.  I cal the Update() method inside the updateBtn_Click() handler, on the last line.

I will post the entire partial class here:

namespace WindowsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.dMS_HaughtonDataSet = new WindowsApplication1.DMS_HaughtonDataSet();
            this.fillByToolStrip = new System.Windows.Forms.ToolStrip();
            this.fillByToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.badgeIDsBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.badgeIDsTableAdapter = new WindowsApplication1.DMS_HaughtonDataSetTableAdapters.BadgeIDsTableAdapter();
            this.updateBtn = new System.Windows.Forms.Button();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.iDDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.wirelessIDDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.badgeIDDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.restaurantIDDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.physicalDescriptionDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dMS_HaughtonDataSet)).BeginInit();
            this.fillByToolStrip.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.badgeIDsBindingSource)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            //
            //
            // dMS_HaughtonDataSet
            //
            this.dMS_HaughtonDataSet.DataSetName = "DMS_HaughtonDataSet";
            this.dMS_HaughtonDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            //
            // fillByToolStrip
            //
            this.fillByToolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fillByToolStripButton});
            this.fillByToolStrip.Location = new System.Drawing.Point(0, 0);
            this.fillByToolStrip.Name = "fillByToolStrip";
            this.fillByToolStrip.Size = new System.Drawing.Size(292, 25);
            this.fillByToolStrip.TabIndex = 1;
            this.fillByToolStrip.Text = "fillByToolStrip";
            //
            // fillByToolStripButton
            //
            this.fillByToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.fillByToolStripButton.Name = "fillByToolStripButton";
            this.fillByToolStripButton.Size = new System.Drawing.Size(35, 22);
            this.fillByToolStripButton.Text = "FillBy";
            this.fillByToolStripButton.Click += new System.EventHandler(this.fillByToolStripButton_Click);
            //
            // badgeIDsBindingSource
            //
            this.badgeIDsBindingSource.DataMember = "BadgeIDs";
            this.badgeIDsBindingSource.DataSource = this.dMS_HaughtonDataSet;
            //
            // badgeIDsTableAdapter
            //
            this.badgeIDsTableAdapter.ClearBeforeFill = true;
            //
            // updateBtn
            //
            this.updateBtn.Location = new System.Drawing.Point(94, 221);
            this.updateBtn.Name = "updateBtn";
            this.updateBtn.Size = new System.Drawing.Size(75, 23);
            this.updateBtn.TabIndex = 2;
            this.updateBtn.Text = "Update";
            this.updateBtn.UseVisualStyleBackColor = true;
            this.updateBtn.Click += new System.EventHandler(this.updateBtn_Click);
            //
            // dataGridView1
            //
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.iDDataGridViewTextBoxColumn,
            this.wirelessIDDataGridViewTextBoxColumn,
            this.badgeIDDataGridViewTextBoxColumn,
            this.restaurantIDDataGridViewTextBoxColumn,
            this.physicalDescriptionDataGridViewTextBoxColumn});
            this.dataGridView1.DataSource = this.badgeIDsBindingSource;
            this.dataGridView1.Location = new System.Drawing.Point(34, 44);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(240, 150);
            this.dataGridView1.TabIndex = 3;
            //
            // iDDataGridViewTextBoxColumn
            //
            this.iDDataGridViewTextBoxColumn.DataPropertyName = "ID";
            this.iDDataGridViewTextBoxColumn.HeaderText = "ID";
            this.iDDataGridViewTextBoxColumn.Name = "iDDataGridViewTextBoxColumn";
            //
            // wirelessIDDataGridViewTextBoxColumn
            //
            this.wirelessIDDataGridViewTextBoxColumn.DataPropertyName = "WirelessID";
            this.wirelessIDDataGridViewTextBoxColumn.HeaderText = "WirelessID";
            this.wirelessIDDataGridViewTextBoxColumn.Name = "wirelessIDDataGridViewTextBoxColumn";
            //
            // badgeIDDataGridViewTextBoxColumn
            //
            this.badgeIDDataGridViewTextBoxColumn.DataPropertyName = "BadgeID";
            this.badgeIDDataGridViewTextBoxColumn.HeaderText = "BadgeID";
            this.badgeIDDataGridViewTextBoxColumn.Name = "badgeIDDataGridViewTextBoxColumn";
            //
            // restaurantIDDataGridViewTextBoxColumn
            //
            this.restaurantIDDataGridViewTextBoxColumn.DataPropertyName = "RestaurantID";
            this.restaurantIDDataGridViewTextBoxColumn.HeaderText = "RestaurantID";
            this.restaurantIDDataGridViewTextBoxColumn.Name = "restaurantIDDataGridViewTextBoxColumn";
            //
            // physicalDescriptionDataGridViewTextBoxColumn
            //
            this.physicalDescriptionDataGridViewTextBoxColumn.DataPropertyName = "PhysicalDescription";
            this.physicalDescriptionDataGridViewTextBoxColumn.HeaderText = "PhysicalDescription";
            this.physicalDescriptionDataGridViewTextBoxColumn.Name = "physicalDescriptionDataGridViewTextBoxColumn";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.updateBtn);
            this.Controls.Add(this.fillByToolStrip);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dMS_HaughtonDataSet)).EndInit();
            this.fillByToolStrip.ResumeLayout(false);
            this.fillByToolStrip.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.badgeIDsBindingSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private DMS_HaughtonDataSet dMS_HaughtonDataSet;
        private System.Windows.Forms.BindingSource badgeIDsBindingSource;
        private WindowsApplication1.DMS_HaughtonDataSetTableAdapters.BadgeIDsTableAdapter badgeIDsTableAdapter;
        private System.Windows.Forms.ToolStrip fillByToolStrip;
        private System.Windows.Forms.ToolStripButton fillByToolStripButton;
        private System.Windows.Forms.Button updateBtn;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn iDDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn wirelessIDDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn badgeIDDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn restaurantIDDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn physicalDescriptionDataGridViewTextBoxColumn;
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dMS_HaughtonDataSet.BadgeIDs' table. You can move, or remove it, as needed.
            this.badgeIDsTableAdapter.Fill(this.dMS_HaughtonDataSet.BadgeIDs);

        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                //this.batchProcessesTableAdapter.FillBy(this.dMS_HaughtonDataSet.BadgeIDs);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void updateBtn_Click(object sender, EventArgs e)
        {
            badgeIDsTableAdapter.Update(this.dMS_HaughtonDataSet.BadgeIDs);
        }
    }
}
0
Comment
Question by:newbieweb
  • 5
  • 5
11 Comments
 
LVL 13

Assisted Solution

by:SameerJagdale
SameerJagdale earned 200 total points
ID: 22644095
are you getting any error? if yes please post it.. before update ..can you check the tables count..something like:
private void updateBtn_Click(object sender, EventArgs e)
        {
            int count = this.dMS_HaughtonDataSet.Tables.Count;
            badgeIDsTableAdapter.Update(this.dMS_HaughtonDataSet.BadgeIDs);
        }
 
0
 
LVL 18

Accepted Solution

by:
Priest04 earned 300 total points
ID: 22644115
In theupdateBtn_Click event, add this code

private void updateBtn_Click(object sender, EventArgs e)
{
    DataTable dtTemp = this.dMS_HaughtonDataSet.BadgeIDs.GetChanges();
    if (dtTemp == null) MessageBox.Show("No data changed!");
    badgeIDsTableAdapter.Update(this.dMS_HaughtonDataSet.BadgeIDs);
}

Goran
0
 

Author Comment

by:newbieweb
ID: 22644645
OK.

I added the code each of you had suggested, and the # of  tables is 44.  And the message box only opens when I have made changes.  

I am using an Access database.  But when I had Access open and made changes I got  this exception:

{"Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."}

Then I closed Access and no  longer got an error when I called Update(), but the update did not happen.  I opened Access and found the old value in the database.  Any ideas?

newbieweb

I am using an Access database.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22644769
lets see if anything is wrong with the InsertCommand, UpdateCommand or DeleteCommand. Try this

private void updateBtn_Click(object sender, EventArgs e)
{
    OleDbCommandBuilder cmb = new OleDbCommandBuilder(badgeIDsTableAdapter); \\ this will recreate all command objects
    badgeIDsTableAdapter.Update(this.dMS_HaughtonDataSet.BadgeIDs);
}
0
 

Author Comment

by:newbieweb
ID: 22644814
I'm getting a compilation error here:

OleDbCommandBuilder cmb = new OleDbCommandBuilder(badgeIDsTableAdapter);

I wants an OleDataAdapter not a:

WindowsApplication1.DMS_HaughtonDataSetTableAdapters.BadgeIDsTableAdapter
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 18

Expert Comment

by:Priest04
ID: 22644985
Ah, you are using a tableAdapter, sorry. Two things:

1) have you changed tables structure in a database after you have created dataset through designer?
2) do you have a primary key set for table?
0
 

Author Comment

by:newbieweb
ID: 22645042
1)  No I have not changed the database structure.
2) In Access, I had definned the ID column as AutoIncrement and selected it as the Key, so there is an icon of a Key next  to it.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22645068
I dont see what else could be the cause here. Can you rebuild table adapter just to make sure its compatible with database structure?
0
 

Author Comment

by:newbieweb
ID: 22645089
How do I rebuild it?  I used the drag and drop features of Visual Studio 2005.  

I think I will start  with a new application and see if  that  works .

thanks,
newbieweb
0
 

Author Comment

by:newbieweb
ID: 22645120
It's working  now.

I rebuilt the tiny project and the update worked.  This time I chose not to add the local database to the project.

Thanks,
newbieweb
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22645160
>> This time I chose not to add the local database to the project.

If you choosed to add database to the project directory, then it copies your database (ex. c:\Test.mdb) to your projects directory (ex. c:\MyProject\test.mdb).

In this case, c:\test.mdb will always contain old data, and new data will be in c:\MyProject\test.mdb database.

That is all that has to do with adding database to the project.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now