data concurrency

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    #1

    data concurrency

    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!
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: data concurrency

    Mike,

    You will have to have some sort of a timestamp on your table. When
    updating your values, you will want to begin a transaction, checking to see
    if the timestamp that you have and the timestamp on the server have changed.
    If they have, then you don't do anything, otherwise, you save the values
    (because the record hasn't changed).

    I don't think that your algorithm is a good one, as you will find that
    the timestamp that you have is always different than the original one.
    Also, how do you mitigate the changes that were made, with the changes you
    want to be made? What if a field was changed that you changed and that
    write should overwrite the change you would have made? You need to be more
    aware, and possibly make your users more aware of the concurrency issue and
    then proceed based on user input, or through some very well-defined rules
    for when this happens.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message
    news:uI0QIX5SDH A.1572@TK2MSFTN GP12.phx.gbl...[color=blue]
    > 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![/color]


    Comment

    Working...