I have a dataset that contains some 3 rows....when i add a new row and I want to insert that new row it inserts all three rows again so i end up with seven rows the 3 that were there already, a copy of the 3 that was there and then 1 for the new row.....when i just want it to insert the newest row that i added to the dataset...Is there anyway to get this done
foreach(System.Data.DataRow currentRow in _dsContacts.Tables[0].Rows) { currentRow["CustomerID"] = this._CustomerID; currentRow["TypeOfContact"] = this._ContactType; string sql = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; using(SqlConnection connect = new SqlConnection(sql)) { using(SqlCommand command = new SqlCommand("InsertContact", connect)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@TypeOfContact", currentRow["TypeOfContact"]); command.Parameters.Add("@CustomerID", currentRow["CustomerID"]); command.Parameters.Add("@IsPrimaryContact", currentRow["IsPrimaryContact"]); command.Parameters.Add("@FirstName", currentRow["FirstName"]); command.Parameters.Add("@LastName", currentRow["LastName"]); command.Parameters.Add("@Title", currentRow["Title"]); command.Parameters.Add("@CoName", currentRow["CoName"]); command.Parameters.Add("@CoAddress", currentRow["CoAddress"]); command.Parameters.Add("@CoAddress2", currentRow["CoAddress2"]); command.Parameters.Add("@City", currentRow["City"]); command.Parameters.Add("@State", currentRow["State"]); command.Parameters.Add("@ZipCode", currentRow["ZipCode"]); command.Parameters.Add("@CoPhone", currentRow["CoPhone"]); command.Parameters.Add("@CoExtension", currentRow["CoExtension"]); command.Parameters.Add("@CoMobilePhone", currentRow["Mobile Phone"]); command.Parameters.Add("@Fax", currentRow["Fax"]); command.Parameters.Add("@Pager", currentRow["Pager"]); command.Parameters.Add("@Email", currentRow["Email"]); command.Parameters.Add("@GeneralInfo", currentRow["GeneralInfo"]); command.Parameters.Add("@CustomerNotes", currentRow["Contact Notes"]); SqlParameter _ContactsIDParameter = new SqlParameter("@ContactsID", SqlDbType.Int); _ContactsIDParameter.Direction = ParameterDirection.Output; command.Parameters.Add(_ContactsIDParameter); try { connect.Open(); command.ExecuteNonQuery(); _ContactsID = (int)command.Parameters["@ContactsID"].Value; } catch(Exception ex) { Console.WriteLine(ex.Message.ToString()); } finally { connect.Close(); } } } } } |

How to Insert Only the newest row added to the dataset
Delusion7
foreach
(System.Data.DataRow currentRow in _dsContacts.Tables[0].Rows)i should have
foreach(System.Data.DataRow currentRow in _dsContacts.Tables[0].Rows[_dsContacts.Tables[0].Rows.Count - 1];
NMM
He is not using DataAdapter but use DataTable and RowState is not DataAdapter feature. He didn't write that is for last row, but for three added rows, and even not last added row. Procedure for updating database is his own so he can do that manually. AcceptChanges must be executed after he made database changes, as was stated. At the end, this is not a new .NET 2.0 and is available from 1.0.
mig16
sorry thats my mistake - I misread. No, dont use the foreach loop, just get the last row.
DataRow theLastRow = theDataTable.Rows[theDataTable.Rows.Count - 1];
thats it.
Anton Kovalev
Michael_317
ah ok cool well as long as it works for you. I would have suggested it earlier but you were after the last latest row inserted :-P
cool, glad you picked up a couple of pointers as well as upgrading to .NET 2.0. Now you can look at using Generics too!
stupots
João S. B.
Actually the rowstate() works best for me in my situation due to the fact that if i do add multiple rows to my dataset i loop through them all and if they are added it adds them all this is the code that is working for me right now I just pass a data row to my update() and insert() methods..and yes i just upgraded my app to 2.0....and I love it!
public void Save()
{
foreach (System.Data.DataRow currentRow in _dsContacts.Tables[0].Rows)
{
if (currentRow.RowState == DataRowState.Added)
{
Insert(currentRow);
this._IsNew = false;
}
else if (currentRow.RowState == DataRowState.Modified)
{
Update(currentRow);
this._IsDirty = false;
}
}
}
zz2
amendez
F.Costa
Cherian
if you want to get the "newest" row, why dont you just get the last item inserted in the datatable to do that, use the DataTable.Rows.Count - 1 and index the row:
DataRow theRow = theDataTable.Rows[theDataTable.Rows.Count - 1];