Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm having a little trouble getting with DataRows and DataRowCollections. The primary question is at the bottom of this post.

 

Here is a code snippet that is working well:

 

// code is truncated for brevity;

DataRowCollection drcComputer = ds1.Tables["scsdComputer"].Rows;
foreach (DataRow row in drcComputer)
{
  // search for assetID from computer table in asset table
  string strrowSelect = "assetID = '" + strassetID + "'";
  DataRow[] foundRows = ds1.Tables["asset"].Select(strrowSelect);

  // if assetID does not exist in asset table, then add it
  if (foundRows.Length < 1)
  {
     // add new row
     DataRow newRow = ds1.Tables["asset"].NewRow();
     newRow["assetID"] = strassetID;
     ds1.Tables["asset"].Rows.Add(newRow);
  }
  else
  {
  // NOTE: this is where I am having problems
  }
}

 

The above code pulls assetID from the scsdComputers table and searches for it in the asset table. If the assetID is not found in the asset table, the assetID is added to the asset table in the if-block.

 

The trouble comes in the else-block. If the assetID does exist in the asset table, then I want to edit a value in the row returned by the Select method used one line above the if-block. However, I noticed there seems to be some difference between DataRow[] and DataRow, and that seems to be what I need to know.

 

I cannot add 'foundRows.BeginEdit()' to my else-block because the BeginEdit method does not exist for 'DataRow[] foundRows'. Do I have to create a new DataRowCollection and re-run my Select method in the else-block using DataRow?

Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
  • *Experts*
Posted (edited)

DataRow[] would be an array of DataRow objects; you need to

specify an element in the array. So, if you want to edit the first row

that was found, you would call foundRows[0].BeginEdit();

 

If there were multiple rows returned by your query, and you want

to edit each one, then a foreach loop would do the trick.

 

foreach (DataRow dr in foundRows) {
 dr.BeginEdit();
 // code
 dr.EndEdit();
}

Edited by Volte
Posted

Thanks... One More...

 

Thanks, that worked!

 

Now I have another problem.

 

I am using OleDbCommandBuilder to automatically generate my update string when I do use the OleDbDataAdapter.Update method.

 

During my pass, I successfully add 7872 records to the asset table using the Adapter.Update with OleDbCommandBuilder. During a second pass, I load the existing 7872 records in DataSet ds1, add another 1846 new records to ds1, and attempt to update the source.

 

When I call the OleDbDataAdapter.Update method (still using OleDbCommandBuilder), I get a "string too long" error telling me I am trying to add too many records. I've included a very basic code snippet below:

 

// create dataset and populate with tables
DataSet ds1 = new DataSet();
ds1.Tables.Add("scsdComputer");
ds1.Tables.Add("asset");

// setup connection using my dbConnection class
dbConnection masterDb = new dbConnection();
OleDbConnection mdb = masterDb.m_mdb;

// create the adapter and command objects
OleDbDataAdapter scsdComputerAdapter = new OleDbDataAdapter(strscsdComputerSelect, mdb);
OleDbCommandBuilder scsdComputerCommand = new OleDbCommandBuilder(scsdComputerAdapter);
OleDbDataAdapter assetAdapter = new OleDbDataAdapter(strassetSelect, mdb);
OleDbCommandBuilder assetCommand = new OleDbCommandBuilder(assetAdapter);

// populate the tables in the dataset
scsdComputerAdapter.Fill(ds1, "scsdComputer");
assetAdapter.Fill(ds1, "asset");

try
{
  // do a bunch of stuff then update source
  scsdComputerAdapter.Update(ds1, "scsdComputer");
  assetAdapter.Update(ds1.GetChanges(), "asset");
}

 

The error is generated by the scsdComputerAdapter.Update line. Any ideas would be greatly appreciated.

Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
Posted

I got it...

 

Ok, this was a case of the absent-minded professor.

 

The assetID field had a 10 character constraint on it, which was fine before I changed the rules about what I would accept as an assetID. Everything should work fine now.

Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...