Can anyone give me any advice on catching data concurrency errors? I
want to amend the code below so that if the record to be amended has
already changed since it was taken from the table, the error will be
trapped and the code will go into a loop where it continues trying to
update the value until it finds that the record has not been amended
since it was taken from the table, and so can successfully update.
SqlConnection objConnectionAt tPurchLNX = new
SqlConnection(C onfigurationSet tings.AppSettin gs["strConnectTest "]);
string strPurpleCards = "SELECT * FROM purple_cards WHERE Serial =
" + Session["SerialNumb er"];
//create a new DataSet object
DataSet objDataSetPurpl eCards = new DataSet();
//create a new DataAdapter using the connection object and select
statement
SqlDataAdapter objDataAdapterP urpleCards = new
SqlDataAdapter( strPurpleCards, objConnectionAt tPurchLNX);
//fill the dataset with data from the DataAdapter object
objDataAdapterP urpleCards.Fill (objDataSetPurp leCards,
"PurpleCard s");
//declare a variable to reference the table
DataTable objTablePurpleC ards =
objDataSetPurpl eCards.Tables["PurpleCard s"];
//edit an exisiting row in the table
DataRow objRow = null;
objRow = objTablePurpleC ards.Rows[0];
objRow["Cash"] = Convert.ToDoubl e(Session["Cash"]) + dblTotal;
try
{
SqlCommand objCommandPurpl eCards = new SqlCommand();
objCommandPurpl eCards.Connecti on = objConnectionAt tPurchLNX;
objCommandPurpl eCards.CommandT ext = "UPDATE Purple_Cards SET Cash
= " + objRow["Cash"] + " WHERE Serial = " + Session["SerialNumb er"];
objDataAdapterP urpleCards.Upda teCommand = objCommandPurpl eCards;
// now do the update
objDataAdapterP urpleCards.Upda te(objDataSetPu rpleCards,
"PurpleCard s");
//refresh data and update Session["Cash"]
SqlCommand objCommandPurpl eCardsRefresh = new
SqlCommand(strP urpleCards, objConnectionAt tPurchLNX);
SqlDataReader objDataReaderPu rpleCardsRefres h = null;
objConnectionAt tPurchLNX.Open( );
objDataReaderPu rpleCardsRefres h =
objCommandPurpl eCardsRefresh.E xecuteReader();
if (objDataReaderP urpleCardsRefre sh.Read() == true)
{
double dblCash;
dblCash =
Convert.ToDoubl e(objDataReader PurpleCardsRefr esh["CASH"]);
dblCash = Math.Round(dblC ash, 2);
Session["Cash"] = dblCash;
lblBalance.Text = "Balance : £" + Session["Cash"];
}
}
catch (Exception objError)
{
lbl1.Text = Convert.ToStrin g(objError);
}
objConnectionAt tPurchLNX.Close ();
Thanks,
Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
want to amend the code below so that if the record to be amended has
already changed since it was taken from the table, the error will be
trapped and the code will go into a loop where it continues trying to
update the value until it finds that the record has not been amended
since it was taken from the table, and so can successfully update.
SqlConnection objConnectionAt tPurchLNX = new
SqlConnection(C onfigurationSet tings.AppSettin gs["strConnectTest "]);
string strPurpleCards = "SELECT * FROM purple_cards WHERE Serial =
" + Session["SerialNumb er"];
//create a new DataSet object
DataSet objDataSetPurpl eCards = new DataSet();
//create a new DataAdapter using the connection object and select
statement
SqlDataAdapter objDataAdapterP urpleCards = new
SqlDataAdapter( strPurpleCards, objConnectionAt tPurchLNX);
//fill the dataset with data from the DataAdapter object
objDataAdapterP urpleCards.Fill (objDataSetPurp leCards,
"PurpleCard s");
//declare a variable to reference the table
DataTable objTablePurpleC ards =
objDataSetPurpl eCards.Tables["PurpleCard s"];
//edit an exisiting row in the table
DataRow objRow = null;
objRow = objTablePurpleC ards.Rows[0];
objRow["Cash"] = Convert.ToDoubl e(Session["Cash"]) + dblTotal;
try
{
SqlCommand objCommandPurpl eCards = new SqlCommand();
objCommandPurpl eCards.Connecti on = objConnectionAt tPurchLNX;
objCommandPurpl eCards.CommandT ext = "UPDATE Purple_Cards SET Cash
= " + objRow["Cash"] + " WHERE Serial = " + Session["SerialNumb er"];
objDataAdapterP urpleCards.Upda teCommand = objCommandPurpl eCards;
// now do the update
objDataAdapterP urpleCards.Upda te(objDataSetPu rpleCards,
"PurpleCard s");
//refresh data and update Session["Cash"]
SqlCommand objCommandPurpl eCardsRefresh = new
SqlCommand(strP urpleCards, objConnectionAt tPurchLNX);
SqlDataReader objDataReaderPu rpleCardsRefres h = null;
objConnectionAt tPurchLNX.Open( );
objDataReaderPu rpleCardsRefres h =
objCommandPurpl eCardsRefresh.E xecuteReader();
if (objDataReaderP urpleCardsRefre sh.Read() == true)
{
double dblCash;
dblCash =
Convert.ToDoubl e(objDataReader PurpleCardsRefr esh["CASH"]);
dblCash = Math.Round(dblC ash, 2);
Session["Cash"] = dblCash;
lblBalance.Text = "Balance : £" + Session["Cash"];
}
}
catch (Exception objError)
{
lbl1.Text = Convert.ToStrin g(objError);
}
objConnectionAt tPurchLNX.Close ();
Thanks,
Mike
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Comment