How to Insert Only the newest row added to the dataset

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





Answer this question

How to Insert Only the newest row added to the dataset

  • Delusion7

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



  • NMM

     ahmedilyas wrote:
    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.



  • 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

    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.

  • 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

    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()

  • 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

    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.

  • amendez

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

  • F.Costa

    you da MAN!

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



  • How to Insert Only the newest row added to the dataset