Okay, here are the two main methods involved in this drama.
<code>
public void CalculatePreproduction(int vpKey)
{
// a database helper which contains the SqlConnection
DataCon dcon = new DataCon(helper.ConnectionString);
StringBuilder selectAssignments = new StringBuilder();
selectAssignments.Append("select * from PPTB1003 where VP_LNR =
").Append(vpKey);
// helper.LoadTable() loads the data from the DB into a DataTable
that's returned by the SQL statement
DataTable allAssignments = helper.LoadTable("PPTB1003",
dcon.Connection, selectAssignments.ToString(), false);
SqlTransaction trans = dcon.Connection.BeginTransaction();
try
{
// any assignment for that id found?
if (allAssignments != null)
{
Invoice invoice = new Invoice(FetchSalaryTable(DateTime.Now),
allDiscounts);
foreach (DataRow row in allAssignments.Rows)
{
// DataInContainer puts all data from the row in an object.
Invoice.Calculate() then calculates the prices.
// WriteInput finally writes the results to 2 different tables
WriteInput(invoice.Calculate(DataInContainer(row)) , dcon, trans);
}
trans.Commit();
}
}
catch(Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
trans.Dispose();
// this Dispoce() contains a call of the SqlConnection.Close() method
dcon.Dispose();
}
}
private void WriteInput(AssignmentContainer data, DataCon dcon,
SqlTransaction trans)
{
// has this assignment got a valid assignment ID?
if (data.AssignmentKey > 0)
{
StringBuilder update1003 = new StringBuilder();
update1003.Append("update PPTB1003 set");
// [...]
// several fields, that need to be updated
// [...]
update1003.Append(" where ASSIGNMENT_ID =
").Append(data.AssignmentKey);
SqlCommand cmd = dcon.Connection.CreateCommand();
cmd.CommandText = update1003.ToString();
cmd.CommandType = CommandType.Text;
cmd.Transaction = trans;
try
{
cmd.ExecuteNonQuery();
}
catch(Exception exc)
{
throw new Exception("Error while storing the data of assignment " +
data.AssignmentNo + ":\n" + exc.ToString(), exc);
}
StringBuilder update1004 = new StringBuilder();
update1004.Append("update PPTB1004 set SUPERVISOR_INST_SL =
").Append(data.SupervisorInstitute).Append(" where ASSIGNMENT_ID =
").Append(data.AssignmentKey);
cmd = dcon.Connection.CreateCommand();
cmd.CommandText = update1004.ToString();
cmd.CommandType = CommandType.Text;
cmd.Transaction = trans;
try
{
cmd.ExecuteNonQuery();
}
catch(Exception exc)
{
throw new Exception("Error while storing for consistency of appraisal
for assignment " + data.AssignmentNo + ":\n" + exc.ToString(), exc);
}
}
}
</code>
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!