Click here to Skip to main content
14,689,406 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
In my VB.NET program, I am trying to update a table within my database with two changes. I initially load the database into my program as follows:
Public dsInfo As New DataSet
Public daInfo As New OleDb.OleDbDataAdapter
Dim LoadQuiz As String

Private Sub LoadQuizDatabase()
LoadQuiz = "SELECT * FROM QuizInfo"
daInfo = New OleDb.OleDbDataAdapter(LoadQuiz, PublicModule.con)
daInfo.Fill(dsInfo, "QuizInfo")
End Sub

Then when I get to the point in time where I need to update this table, I use the following code:
Private Sub UpdateQuizInfo()
dsInfo.Tables("QuizInfo").Rows(PublicModule.QuizInfoRow).Item("Questions") = MaxRows
dsInfo.Tables("QuizInfo").Rows(PublicModule.QuizInfoRow).Item("Modified") = Date.Now
daInfo.Update(dsInfo, "QuizInfo")
End Sub

The PublicModule.QuizInfoRow variable is the correct row number. However, every time, it spews out this error:
Quote:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
I have looked for a solution online, and cannot find an answer. I have tried running the LoadQuizDatabase() sub immediately before updating, but without any luck. This is driving me MAD!

Please help!
Posted
Updated 6-Jun-14 23:40pm
v2

After experimenting for several hours, I have finally found the solution. Hope this solves someone else's problem:
Dim cb As New OleDb.OleDbCommandBuilder(TableAdapter)
   
Comments
Mahfoudh Arous 19-Feb-16 1:36am
   
you saved my life!!
Mahfoudh Arous 19-Feb-16 1:36am
   
or better my honor!!
Ignacio Tyther 1-Oct-16 22:47pm
   
it worked out for me, i dont know why, you saved my day thank you
I got the same message after changing a fieldname in the database.
In my visual studio project, I had a dataset bound to this table, so in the Designer I regenerated my Select Statement. (A mistake I think - I should probably have edited both the Select and Update Statements in the Tables property window).

Anyway, looking at that table in the Designer I see that the Update Statement is now set to NONE. Re-entering it (copying the text from a backup) resolved this error.
   
Comments
Sahil 123 7-Nov-17 6:47am
   
I am also facing this problem my whole code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace software
{
public partial class Products : MetroFramework.Forms.MetroForm
{
public Products()
{
InitializeComponent();
}

private void productsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{

}

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

}

private void metroButton1_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.productsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.usersDbDataSet);
productsTableAdapter.Insert(codeTextBox.Text, nameTextBox.Text, rateTextBox.Text);
MetroFramework.MetroMessageBox.Show(this, "The product has been added.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Refresh();
}
catch (Exception ex)
{
MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void metroButton2_Click(object sender, EventArgs e)
{
try
{

productsBindingSource.RemoveCurrent();

}
catch(Exception ex)
{
MetroFramework.MetroMessageBox.Show(this, ex.Message);
}
}

private void metroButton3_Click(object sender, EventArgs e)
{
try {
productsBindingSource.AddNew();
codeTextBox.Select();
}
catch(Exception ex)
{
MetroFramework.MetroMessageBox.Show(this, ex.Message);
}
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900