How to Insert Only the newest row added to the dataset  
Author Message
Tryin2Bgood





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

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;





















SqlParameter _ContactsIDParameter =
new
_ContactsIDParameter.Direction = ParameterDirection.Output;
command.Parameters.Add(_ContactsIDParameter);

try

{
connect.Open();
command.ExecuteNonQuery();
_ContactsID = (
int
}
catch(Exception ex)
{

Console.WriteLine(ex.Message.ToString());
}
finally

{
connect.Close();
}
}
}
}
}





Visual C#20  
 
 
ahmedilyas





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

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];



 
 
Tryin2Bgood





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

So instead of

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];



 
 
ahmedilyas





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

to get the last (latest hopefully) row inserted, correct!

 
 
Tryin2Bgood





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

Actually I tried that and i get this error...that foreach can not operate on System.Data.DataRow because it does not contain a definition for GetEnumerator()

 
 
ahmedilyas





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

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.



 
 
Tryin2Bgood





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

you da MAN!

 
 
boban.s





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

You can use RowState to deterimine what is the status of every table row. For your case you will have Unchanged, Added, Deleted and Modified status of rows. You will loop all table rows, if row has RowsState equal to DataRowState.Added you will execute insert method(procedure) for this row, if the RowState is DataRowState.Modified then execute update method(procedure) and if is deleted then delete procedure. After this all is done, execute AcceptChanges table method to clear all rows statuses or refill the table from database.

 
 
ahmedilyas





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

the OP is not using a DataAdapter therefore that won't really apply. However when you execute the Update() method of the DataAdapter it will automatically call AcceptChanges on the dataset, so you dont need to worry about this. As well as all of this, if we were still to do this, it won't quite work as if you AcceptChanges() then you wont be able to find out the last/latest added row since all changes have been committed to the dataset. Even going through each row and checking the rowstate property wont quite work since there maybe several rows that have been added.

 
 
Tryin2Bgood





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

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;
}
}
}




 
 
ahmedilyas





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

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!



 
 
boban.s





PostPosted: Visual C# Language, How to Insert Only the newest row added to the dataset Top

 
the OP is not using a DataAdapter therefore that won't really apply. However when you execute the Update() method of the DataAdapter it will automatically call AcceptChanges on the dataset, so you dont need to worry about this. As well as all of this, if we were still to do this, it won't quite work as if you AcceptChanges() then you wont be able to find out the last/latest added row since all changes have been committed to the dataset. Even going through each row and checking the rowstate property wont quite work since there maybe several rows that have been added.

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.