Christian Forums

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • Focus on the Family

    Strengthening families through biblical principles.

    Focus on the Family addresses the use of biblical principles in parenting and marriage to strengthen the family.

  • Guest, Join Papa Zoom today for some uplifting biblical encouragement! --> Daily Verses
  • The Gospel of Jesus Christ

    Heard of "The Gospel"? Want to know more?

    There is salvation in no other, for there is not another name under heaven having been given among men, by which it behooves us to be saved."

C# dataadapter tries to add Datacolumn from wrong table

2024 Website Hosting Fees

Total amount
$1,048.00
Goal
$1,038.00

wwjd_kilden

Member
Hi.
I'm not sure if my problem lies in this bit of code or somewhere else, but the exception is thrown from my dataadapter when call the update command on the first dataadapter. .

Error message says Additional information: Missing the DataColumn 'company_ID' in the DataTable 'machine' for the SourceColumn 'company_ID

I have two tables (machine and ownership), and I want to update them both. However, it seems that my dataadapter wants to add the first column of one table (ownership) to the other table (machine), even though I have two separate data adapters.

Knowing myself, it is probably some tiny, silly error :tongue
(It saved to the database without problems before I tried adding the code to save to the ownership table)

Ideas? Am I missing something obvious? (I'm very good at that....) I can post more of my code if needed.

mysql> explain machine;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| SN | varchar(10) | NO | PRI | NULL | |
| model | varchar(4) | YES | | NULL | |
| machinetype | varchar(30) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+

mysql> explain ownership;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| company_ID | int(3) | NO | PRI | NULL | |
| machine_SN | varchar(10) | NO | PRI | NULL | |
| contract | varchar(4) | YES | | NULL | |
| contact | int(3) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+

C#

if (tablename == "machine")
{
foreach (DataColumn c in ds.Tables[tablename].Columns)
{
// Add values from list into the datarow in corresponding columns
string colname = c.ColumnName;
row[colname] = fieldList;
i++;
}
//-----------------------------------------------------
ds2 = new DataSet("ownership");
string q = @"SELECT * from ownership;";
MySqlCommand cmd2 = new MySqlCommand(q, connection);
da2 = new MySqlDataAdapter(cmd2);
da2.Fill(ds2, "ownership");
ownershiprow = ds2.Tables["ownership"].NewRow(); ;

foreach (DataColumn col in ds2.Tables["ownership"].Columns)
{
// Add values from list into the datarow in corresponding columns
string colname = col.ColumnName;
if (colname == "company_ID" || colname == "contact") { ownershiprow[colname] = Convert.ToInt32(fieldList); } //TEST fordi company_ID og contact er int... endre i DB?
else { ownershiprow[colname] = fieldList; }

i++;
}

// some code inbetween

if (tablename == "machine")
{
ds.Tables[tablename].Rows.Add(row);
MySqlCommandBuilder builder = new MySqlCommandBuilder(da);
da.Update(ds, "machine");

Error msg from dataadapter line: Additional information: Missing the DataColumn 'company_ID' in the DataTable 'machine' for the SourceColumn 'company_ID

controller.showMessage("Data saved to mc");
// ---------------------------------------------------
ds2.Tables["ownership"].Rows.Add(ownershiprow);
MySqlCommandBuilder build = new MySqlCommandBuilder(da2);
da2.Update(ds2, "ownership");
controller.showMessage("Data saved to ownership");
}
 
Why did you create th e string colname twice?
Is it possible colname has th e value from ownership th e first time you iterate through machine?
 
Back
Top