Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OleDB DataAdapter Multiple Row Update Problem

Status
Not open for further replies.

ccshadow

Programmer
Jan 29, 2004
33
US
I have a strange problem that occurs when I attempt to perform an update w/ a DataAdapter. I've created my own Insert, Update and Delete commands due to a join that extracts a description from a second table to replace an ID from the main table. It kinda sorta works except that it only inserts the first row's data. So if I enter 10 new rows into the DataGrid it inserts the first row of data 10 times. I keep wanting to scream at it, "Move next!", but I've tried that several times to no avail.

Although code for all 3 commands are included, I've focused my efforts on the Insert. Any ideas or suggestions would be greatly appreciated!

Here are my Access 2000 stored queries:
INSERT INTO dailyInventory ( tdate, chemicalID, amtInStock )
VALUES (tdate, chemicalID, amtInStock);

DELETE dailyInventory.*
FROM dailyInventory
WHERE tdate=[tdate;] And chemicalID=[chemicalID;];

UPDATE dailyInventory SET tdate = [tdate;], chemicalID = [chemicalID;], amtInStock = [amtInStock;]
WHERE tdate=[tdate;] And chemicalID=[chemicalID;];


Code:
// deleteDailyInv
this.deleteDailyInv.CommandText = "[sqDeleteDailyInv]";
this.deleteDailyInv.CommandType = System.Data.CommandType.StoredProcedure;
this.deleteDailyInv.Connection = this.oleConn;
			
// insertDailyInv
this.insertDailyInv.CommandText = "[sqInsertDailyInv]";
this.insertDailyInv.CommandType = System.Data.CommandType.StoredProcedure;
//this.insertDailyInv.CommandText = "INSERT INTO dailyInventory (tdate, chemicalID, amtInStock) VALUES(tdate = @tdate, chemicalID = @chemicalID, amtInStock = @amtInStock)";
//this.insertDailyInv.CommandType = System.Data.CommandType.Text;
this.insertDailyInv.Connection = this.oleConn;
			
// updateDailyInv
this.updateDailyInv.CommandText = "[sqUpdateDailyInv]";
this.updateDailyInv.CommandType = System.Data.CommandType.StoredProcedure;
//this.updateDailyInv.CommandText = "UPDATE dailyInventory SET tdate = ?, chemicalID = ?, amtInStock = ? WHERE tdate = @tdate AND chemicalID = @chemicalID";
//this.updateDailyInv.CommandType = System.Data.CommandType.Text;
this.updateDailyInv.Connection = this.oleConn;
			 
// oleAdpDailyInv
this.oleAdpDailyInv.DeleteCommand = this.deleteDailyInv;
this.oleAdpDailyInv.DeleteCommand.Parameters.Add("@tdate", OleDbType.Date, 10, "tdate");
this.oleAdpDailyInv.DeleteCommand.Parameters.Add("@chemicalID", OleDbType.Integer, 5, "chemicalID");
this.oleAdpDailyInv.InsertCommand = this.insertDailyInv;
this.oleAdpDailyInv.InsertCommand.Parameters.Add("@tdate", OleDbType.Date, 10, "tdate");
this.oleAdpDailyInv.InsertCommand.Parameters.Add("@chemicalID", OleDbType.Integer, 5, "chemicalID");
this.oleAdpDailyInv.InsertCommand.Parameters.Add("@amtInStock", OleDbType.Integer, 10, "amtInStock");
this.oleAdpDailyInv.UpdateCommand = this.updateDailyInv;
this.oleAdpDailyInv.UpdateCommand.Parameters.Add("@tdate", OleDbType.Date, 10, "tdate");
this.oleAdpDailyInv.UpdateCommand.Parameters.Add("@chemicalID", OleDbType.Integer, 5, "chemicalID");
this.oleAdpDailyInv.UpdateCommand.Parameters.Add("@amtInStock", OleDbType.Integer, 10, "amtInStock");
this.oleAdpDailyInv.UpdateCommand.Parameters.Add("@tdate", OleDbType.Date, 10, "tdate");
this.oleAdpDailyInv.UpdateCommand.Parameters.Add("@chemicalID", OleDbType.Integer, 5, "chemicalID");

strFilter = "tdate='" + dtpDate.Value.ToShortDateString() + "'";
dvDailyInv.RowFilter = strFilter;

// get chemical names for dailyInventory entry
if (dvDailyInv.Count.Equals(0))
{
	strFilter = "discontinued = 0";
	dvChemicals.RowFilter = strFilter;
				
	foreach (DataRowView drChemicals in dvChemicals) 
	{
		drDailyInv = dvDailyInv.AddNew();
		drDailyInv.BeginEdit();
		drDailyInv["tdate"] = dtpDate.Value.ToShortDateString();
		drDailyInv["description"] = drChemicals["description"];
		drDailyInv["chemicalID"] = drChemicals["chemicalID"];
		drDailyInv.EndEdit();
	}
}

private void btnSave_Click(object sender, System.EventArgs e)
{
	for (int i = dvDailyInv.Count - 1; i >= 0; i--)
	{
		if (dvDailyInv[i]["amtInStock"] == (System.Convert.DBNull)) 
		{
			dvDailyInv[i].Delete();
		}
	}
	try 
	{
		oleAdpDailyInv.Update(dsChemicalTracking, "dailyInventory");
	}
	catch (DBConcurrencyException ex) 
	{
		MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK);
		string customErrorMessage;
		customErrorMessage = ex.Row[0].ToString();
		Console.WriteLine(customErrorMessage);
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top