G
gordonfmoore
Here's a variation on the old chestnut, but I can't get a handle on it.
I have a database that has a many to many relation. Table A has two
columns AID and AName, table B has two columns BID and BName and table
AB has three columns ABID, AID and BID for relating tables A and B.
(I am using Access, but don't use the autogenerated ID, although I let
it operate.)
I create a relation which contains ABID, AID, BID and BName.
If I edit BName in this relation I get the above error when I try to
update the databases, but I am only updating AB with the IDs, not the
name info and yet I am getting the error:
System.Data.OleDb.OleDbException: No value given for one or more
required parameters.
at
System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at
System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable
dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String
srcTable)
at ConsoleApplication1.Program.Main(String[] args) in C:\Documents
and Settings\Gordon\My Documents\Visual Studio
2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line
111
0 A and 0 B and 0 AB records updated - from my line of code
Line 111 contains the: i = daAB.Update(ds, "AB"); line
I have noted by cycling through the datarows of the AB table in the
dataset that the one that is changed is getting its status changed to
modified, which is fair enough. I suspect that since the
dataadapter.update is seeing that change then it wants to update the AB
table in the access table, even though it doesn't need to.
Any pointers would be gratefully appreciated, as well as any thoughts
on the update code, as I found this pretty complex to get my head
around - possibly where there are errors?
I have stripped my code as much as possible, but this is what generates
the error:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string myDB = "AorB.mdb";
OleDbConnection conn = null;
DataSet ds;
OleDbDataAdapter daA;
OleDbDataAdapter daB;
OleDbDataAdapter daAB;
OleDbDataAdapter daBA;
DataRelation drAB;
DataRelation drBA;
//create a connection string to the access database
conn = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;
User Id=;Password=;
Data Source=" + myDB);
//clear the dataset
ds = null;
ds = new DataSet();
//set up the link
daA = new OleDbDataAdapter("SELECT * FROM A ORDER BY
AName", conn);
daB = new OleDbDataAdapter("SELECT * FROM B ORDER BY
BName", conn);
//fill the dataset with data
daA.Fill(ds, "A");
daB.Fill(ds, "B");
//Create Data Relation from A to B,
daAB = new OleDbDataAdapter("SELECT AB.ABID, AB.AID,
AB.BID, B.BName FROM B INNER JOIN AB ON B.BID = AB.BID ORDER BY
B.BName", conn);
daAB.Fill(ds, "AB");
drAB = new DataRelation("AToB",
ds.Tables["A"].Columns["AID"],
ds.Tables["AB"].Columns["AID"], true);
ds.Relations.Add(drAB);
//daBA = new OleDbDataAdapter("SELECT AB.ABID, AB.BID,
AB.AID, A.AName FROM A INNER JOIN AB ON A.AID = AB.AID ORDER BY
A.AName", conn);
//daBA.Fill(ds, "BA");
//drBA = new DataRelation("BToA",
// ds.Tables["B"].Columns["BID"],
// ds.Tables["BA"].Columns["BID"], true);
//ds.Relations.Add(drBA);
ds.EnforceConstraints = true;
//Change the BName in the first row of the AB table
DataRow[] dr = ds.Tables["AB"].Select();
DataRow r = dr[0];
r["BName"] = "Help";
//update database
string updateSQLA = "Update A set [AID]=?, [AName]=? where
[AID]=?";
//
//the @ is to make sure that it is treated as is, no
confusion with C# keywords
daA.UpdateCommand = new OleDbCommand(updateSQLA, conn);
daA.UpdateCommand.Parameters.Add("@AID", OleDbType.Integer,
10, "AID");
daA.UpdateCommand.Parameters.Add("@AName",
OleDbType.VarWChar, 50, "AName");
OleDbParameter AID1 =
daA.UpdateCommand.Parameters.Add("@AID", OleDbType.Integer, 10, "AID");
AID1.SourceVersion = DataRowVersion.Original;
string updateSQLB = "Update B set [BID]=?, [BName]=? where
[BID]=?";
//
daB.UpdateCommand = new OleDbCommand(updateSQLB, conn);
daB.UpdateCommand.Parameters.Add("@BID", OleDbType.Integer,
10, "BID");
daB.UpdateCommand.Parameters.Add("@BName",
OleDbType.VarWChar, 50, "BName");
OleDbParameter BID1 =
daB.UpdateCommand.Parameters.Add("@BID", OleDbType.Integer, 10, "BID");
BID1.SourceVersion = DataRowVersion.Original;
//now need to update AB table in real database
string updateSQLAB = "Update AB set [ABID]=?, [AID]=?,
[BID]=? where [AID]=? and [BID]=?";
//
daAB.UpdateCommand = new OleDbCommand(updateSQLAB, conn);
daAB.UpdateCommand.Parameters.Add("@ABID",
OleDbType.Integer, 10, "ABID");
daAB.UpdateCommand.Parameters.Add("@AID",
OleDbType.Integer, 10, "AID");
daAB.UpdateCommand.Parameters.Add("@BID",
OleDbType.Integer, 10, "BID");
//OleDbParameter ABID1 =
daA.UpdateCommand.Parameters.Add("@ABID", OleDbType.Integer, 10,
"ABID");
//ABID1.SourceVersion = DataRowVersion.Original;
OleDbParameter ABID2 =
daA.UpdateCommand.Parameters.Add("@AID", OleDbType.Integer, 10, "AID");
ABID2.SourceVersion = DataRowVersion.Original;
OleDbParameter ABID3 =
daA.UpdateCommand.Parameters.Add("@BID", OleDbType.Integer, 10, "BID");
ABID3.SourceVersion = DataRowVersion.Original;
int g = 0, h = 0, i = 0;
try //AB first (important)
{
i = daAB.Update(ds, "AB");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
g = daA.Update(ds, "A");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
h = daB.Update(ds, "B");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
//for testing
MessageBox.Show(g.ToString() + " A and " + h.ToString() + "
B and " + i.ToString() + " AB records updated");
}
}
}
I have a database that has a many to many relation. Table A has two
columns AID and AName, table B has two columns BID and BName and table
AB has three columns ABID, AID and BID for relating tables A and B.
(I am using Access, but don't use the autogenerated ID, although I let
it operate.)
I create a relation which contains ABID, AID, BID and BName.
If I edit BName in this relation I get the above error when I try to
update the databases, but I am only updating AB with the IDs, not the
name info and yet I am getting the error:
System.Data.OleDb.OleDbException: No value given for one or more
required parameters.
at
System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at
System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable
dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String
srcTable)
at ConsoleApplication1.Program.Main(String[] args) in C:\Documents
and Settings\Gordon\My Documents\Visual Studio
2005\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line
111
0 A and 0 B and 0 AB records updated - from my line of code
Line 111 contains the: i = daAB.Update(ds, "AB"); line
I have noted by cycling through the datarows of the AB table in the
dataset that the one that is changed is getting its status changed to
modified, which is fair enough. I suspect that since the
dataadapter.update is seeing that change then it wants to update the AB
table in the access table, even though it doesn't need to.
Any pointers would be gratefully appreciated, as well as any thoughts
on the update code, as I found this pretty complex to get my head
around - possibly where there are errors?
I have stripped my code as much as possible, but this is what generates
the error:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string myDB = "AorB.mdb";
OleDbConnection conn = null;
DataSet ds;
OleDbDataAdapter daA;
OleDbDataAdapter daB;
OleDbDataAdapter daAB;
OleDbDataAdapter daBA;
DataRelation drAB;
DataRelation drBA;
//create a connection string to the access database
conn = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;
User Id=;Password=;
Data Source=" + myDB);
//clear the dataset
ds = null;
ds = new DataSet();
//set up the link
daA = new OleDbDataAdapter("SELECT * FROM A ORDER BY
AName", conn);
daB = new OleDbDataAdapter("SELECT * FROM B ORDER BY
BName", conn);
//fill the dataset with data
daA.Fill(ds, "A");
daB.Fill(ds, "B");
//Create Data Relation from A to B,
daAB = new OleDbDataAdapter("SELECT AB.ABID, AB.AID,
AB.BID, B.BName FROM B INNER JOIN AB ON B.BID = AB.BID ORDER BY
B.BName", conn);
daAB.Fill(ds, "AB");
drAB = new DataRelation("AToB",
ds.Tables["A"].Columns["AID"],
ds.Tables["AB"].Columns["AID"], true);
ds.Relations.Add(drAB);
//daBA = new OleDbDataAdapter("SELECT AB.ABID, AB.BID,
AB.AID, A.AName FROM A INNER JOIN AB ON A.AID = AB.AID ORDER BY
A.AName", conn);
//daBA.Fill(ds, "BA");
//drBA = new DataRelation("BToA",
// ds.Tables["B"].Columns["BID"],
// ds.Tables["BA"].Columns["BID"], true);
//ds.Relations.Add(drBA);
ds.EnforceConstraints = true;
//Change the BName in the first row of the AB table
DataRow[] dr = ds.Tables["AB"].Select();
DataRow r = dr[0];
r["BName"] = "Help";
//update database
string updateSQLA = "Update A set [AID]=?, [AName]=? where
[AID]=?";
//
//the @ is to make sure that it is treated as is, no
confusion with C# keywords
daA.UpdateCommand = new OleDbCommand(updateSQLA, conn);
daA.UpdateCommand.Parameters.Add("@AID", OleDbType.Integer,
10, "AID");
daA.UpdateCommand.Parameters.Add("@AName",
OleDbType.VarWChar, 50, "AName");
OleDbParameter AID1 =
daA.UpdateCommand.Parameters.Add("@AID", OleDbType.Integer, 10, "AID");
AID1.SourceVersion = DataRowVersion.Original;
string updateSQLB = "Update B set [BID]=?, [BName]=? where
[BID]=?";
//
daB.UpdateCommand = new OleDbCommand(updateSQLB, conn);
daB.UpdateCommand.Parameters.Add("@BID", OleDbType.Integer,
10, "BID");
daB.UpdateCommand.Parameters.Add("@BName",
OleDbType.VarWChar, 50, "BName");
OleDbParameter BID1 =
daB.UpdateCommand.Parameters.Add("@BID", OleDbType.Integer, 10, "BID");
BID1.SourceVersion = DataRowVersion.Original;
//now need to update AB table in real database
string updateSQLAB = "Update AB set [ABID]=?, [AID]=?,
[BID]=? where [AID]=? and [BID]=?";
//
daAB.UpdateCommand = new OleDbCommand(updateSQLAB, conn);
daAB.UpdateCommand.Parameters.Add("@ABID",
OleDbType.Integer, 10, "ABID");
daAB.UpdateCommand.Parameters.Add("@AID",
OleDbType.Integer, 10, "AID");
daAB.UpdateCommand.Parameters.Add("@BID",
OleDbType.Integer, 10, "BID");
//OleDbParameter ABID1 =
daA.UpdateCommand.Parameters.Add("@ABID", OleDbType.Integer, 10,
"ABID");
//ABID1.SourceVersion = DataRowVersion.Original;
OleDbParameter ABID2 =
daA.UpdateCommand.Parameters.Add("@AID", OleDbType.Integer, 10, "AID");
ABID2.SourceVersion = DataRowVersion.Original;
OleDbParameter ABID3 =
daA.UpdateCommand.Parameters.Add("@BID", OleDbType.Integer, 10, "BID");
ABID3.SourceVersion = DataRowVersion.Original;
int g = 0, h = 0, i = 0;
try //AB first (important)
{
i = daAB.Update(ds, "AB");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
g = daA.Update(ds, "A");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
h = daB.Update(ds, "B");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
//for testing
MessageBox.Show(g.ToString() + " A and " + h.ToString() + "
B and " + i.ToString() + " AB records updated");
}
}
}