Saving from Datatable

Posts   
 
    
rai
User
Posts: 41
Joined: 25-Jan-2007
# Posted on: 03-Mar-2008 23:25:15   

please can anyone help me on this... I am saving to data from Datatable row by row.....Before saving it iam checking it whether the record exists in the database or not. Example : here is my code TransactionsEntity saveentity = new TransactionsEntity(); Sales = (DataTable)Session["Data"]; foreach (DataRow r in StSales.Rows) { EntityCollection checkrecords = StreetSaleRcpt.GetCustomerReturns(ddlCustomer.SelectedItem.ToString);

if (checkrecords.Count <= 0) { savestreetentity.TransactionDate = Convert.ToDateTime(r["TransactionDate"]); savestreetentity.PaymentDate = Convert.ToDateTime(txtPaymentdate.Text); savestreetentity.PeriodMonth = Convert.ToInt32(txtPeriodMonth.Text); savestreetentity.PeriodYear = Convert.ToInt32(txtPeriodYear.Text); StreetSaleRcpt.Save(savestreetentity); } } else lblError.Text = "Transaction already exists"

Well the problem is it save row by row in database... Example if row2 already exists in the database it will give error but however it will already save row1 in the database.

My question is is there a way to rollback everything if one of the rows already exists. Rather than saving it......

suggesting. I am using llblgen 2.5...adapter visual studio 2005 using C# database is sql server 2005.....

hope i had provided all the requirements........looking forward for sugesstions!!!!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Mar-2008 02:03:45   

Hi rai, you should use Transactions. Please see LLBLGenPro Help - Using the generated code - Adapter - Transactions. Now, as you are using an object to perform the save,

StreetSaleRcpt.Save(savestreetentity);

you have to do one of the following options:

A. Collect all the entities you have to save, pass them to your StreetSaleRcpt.Save method and manage the transaction inside that method, or save the whole Collection (adapter.SaveEntityCollection start a transaction implicit).

B. Make an overload of your method that receive an DataAccessAdapter object. That object will be used inside to grab the objects. Outside you could start/end/rollback the transaction.

By the way...

foreach (DataRow r in StSales.Rows)
{
     EntityCollection checkrecords = StreetSaleRcpt.GetCustomerReturns(ddlCustomer.SelectedItem.ToString);
...

Why do you do this way? Shouldn't be better if you call this method outside the loop?

Cheers.

David Elizondo | LLBLGen Support Team
rai
User
Posts: 41
Joined: 25-Jan-2007
# Posted on: 04-Mar-2008 04:23:46   

Hi daelmo,

As you mention to use transactions thats what im trying now but it only saves one record. here is my code:

this is wat my datatable looks like:

Record Transdate Supply Returns RE 29/02/2008 42 12 IS 28/02/2008 20 6 ...... protected void Update_Click(object sender, EventArgs e) { DataAccessadapter adapter = new DataAccessadapter(); SalesEntity sales = new SalesEntity(); SalesData = (Datatable)Session["Data"]; try { adapter.StartTransaction(IsolationLevel.ReadCommitted,"AddRecord"); foreach(DataRow r in SalesData.Rows) { sales.transdate = Convert.ToDateTime(r["Transdate"]); sales.record = Convert.ToString(r["Record"]); sales.supply = Convert.Toint32(r["Supply"]); sales.returns = Convert.Toint32(r["Returns"]);

      adapter.SaveEntity(sales);

   }
 adapter.Commit();

} catch { adapter.Rollback(); } finally { adapter.Dispose() } }

This code however only saves one record. Can help me out how to save all the data once.

Thanks..................

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Mar-2008 10:41:48   

Cause you are using the same instance of the SalesEntity, please try the following:

protected void Update_Click(object sender, EventArgs e) { DataAccessadapter adapter = new DataAccessadapter(); SalesData = (Datatable)Session["Data"]; try { adapter.StartTransaction(IsolationLevel.ReadCommitted,"AddRecord"); foreach(DataRow r in SalesData.Rows) { SalesEntity sales = new SalesEntity(); sales.transdate = Convert.ToDateTime(r["Transdate"]); sales.record = Convert.ToString(r["Record"]); sales.supply = Convert.Toint32(r["Supply"]); sales.returns = Convert.Toint32(r["Returns"]);

     adapter.SaveEntity(sales);

 }
 adapter.Commit();

} catch { adapter.Rollback(); } finally { adapter.Dispose() } }

rai
User
Posts: 41
Joined: 25-Jan-2007
# Posted on: 04-Mar-2008 10:56:28   

thank you so much....its working now.