link

hi,
I have foudn this article on "Optimistic Concurrency" on this
http://msdn.microsoft.com/en-us/library/aa0416cz(VS.71).aspx
I dont understand the following lines of code in it (I have written inline comments as questions):
SqlDataAdapter custDA = new SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers ORDER BY CustomerID", nwindConn);
//1. Should i put this update operations within the same method block with the select operations?
//The Update command checks for optimistic concurrency violations in the WHERE clause.
custDA.UpdateCommand = new SqlCommand("UPDATE Customers (CustomerID, CompanyName) VALUES(@CustomerID, @CompanyName) " + "WHERE CustomerID = @oldCustomerID AND CompanyName = @oldCompanyName", nwindConn); custDA.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID"); custDA.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 30, "CompanyName");
//Pass the original values to the WHERE clause parameters. SqlParameter myParm;
//2. Why should i include a SQLParameter assignment here? myParm = custDA.UpdateCommand.Parameters.Add("@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
//3. What his "myParm.SourceVersion" does? Does it sore the Original fetched value in the select
//statement?
myParm.SourceVersion = DataRowVersion.Original;
myParm = custDA.UpdateCommand.Parameters.Add("@oldCompanyName", SqlDbType.NVarChar, 30, "CompanyName"); myParm.SourceVersion = DataRowVersion.Original;
//Add the RowUpdated event handler. custDA.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated); DataSet custDS = new DataSet();
//the value custDA.Fill(custDS, "Customers"); //4. why did the author call the update() in here? Why he put Fill() and UPdate() in the same
//method block? custDA.Update(custDS, "Customers");
based on that i have written code:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
public class DBConnections
{
SqlConnection sqlConn;
SqlCommand cmdSelect,
cmdUpdate;
SqlDataAdapter da;
DataSet ds;
SqlParameter paramDateTime, paramIndex;
public DBConnections()
{
sqlConn =
new SqlConnection(@"Data Source=TYRONE\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True");

cmdSelect = new SqlCommand();
cmdUpdate = new SqlCommand();

da = new SqlDataAdapter();
ds = new DataSet();
da.SelectCommand = cmdSelect;
da.UpdateCommand = cmdUpdate;
}
public DataSet GetAllRecords()
{
SqlParameter selectParam;

da.SelectCommand.Parameters.Clear();
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.CommandText = "ProcGetAllRecords";
da.SelectCommand.Connection = sqlConn;
da.SelectCommand.Parameters.Add("@PProductIndex", SqlDbType.Int, 4, "ProductIndex");
da.SelectCommand.Parameters["@PProductIndex"].Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("@PName", SqlDbType.NVarChar, 50, "Name");
da.SelectCommand.Parameters["@PName"].Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("@PDescription", SqlDbType.NVarChar, -1, "Description");
da.SelectCommand.Parameters["@PDescription"].Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("@PCOO", SqlDbType.NVarChar, 50, "COO");
da.SelectCommand.Parameters["@PCOO"].Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("@PInsertedTime", SqlDbType.DateTime, 8, "InsertedTime");
da.SelectCommand.Parameters["@PInsertedTime"].Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("@PUpdatedTime", SqlDbType.DateTime, 8, "UpdatedTime");
da.SelectCommand.Parameters["@PUpdatedTime"].Direction = ParameterDirection.Output;
da.UpdateCommand.Parameters.Clear();
//to store the original values
paramDateTime = da.UpdateCommand.Parameters.Add("@PUpdatedTimeOLD", SqlDbType.DateTime, 8, "UpdatedTime");
paramDateTime.SourceVersion = DataRowVersion.Original;
paramIndex = da.UpdateCommand.Parameters.Add("@PProductIndexOLD", SqlDbType.Int, 4, "ProductIndex");
paramIndex.SourceVersion = DataRowVersion.Original;
try
{
da.SelectCommand.Connection.Open();
da.Fill(ds, "Products");
da.SelectCommand.Connection.Close();
}
catch
{
throw;
}

return ds;
}
public void UpdateProducts(DataSet ds)
{
da.UpdateCommand.CommandType = CommandType.StoredProcedure;
da.UpdateCommand.CommandText = "ProcUpdateProducts";
da.UpdateCommand.Connection = sqlConn;
da.UpdateCommand.Parameters.Add("@PProductIndexOLD", SqlDbType.Int, 4, "ProductIndex");
da.UpdateCommand.Parameters["@PProductIndexOLD"].Value = paramIndex.Value;
da.UpdateCommand.Parameters.Add("@PUpdatedTimeOLD", SqlDbType.DateTime, 8, "UpdatedTime");
da.UpdateCommand.Parameters["@PUpdatedTimeOLD"].Value = paramDateTime.Value;
try
{
da.UpdateCommand.Connection.Open();
da.Update(ds, "Products");
da.UpdateCommand.Connection.Close();
da.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
}
catch
{
throw;
}
}
protected static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
{
if (args.RecordsAffected == 0)
{
args.Row.RowError = "Optimistic Concurrency Violation Encountered";
args.Status = UpdateStatus.SkipCurrentRow;
}
}

}
my code wouldnt do any concurrencies handling! ...so i need to know what's going on in the article sample and what's wrong in my code as well?
TY in Advance