1 Replies - 10682 Views - Last Post: 21 August 2012 - 05:32 AM

#1 petkovic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 29-August 11

Trouble with ObjectDataSource control in a multiuser environment.

Posted 14 August 2012 - 07:33 AM

I worked some example of multi-user environment,like in book ASP.Net Unleashed 4 - page 828 .

example is very simple. I have a book working class and methods UpdateBook and getBooksDataTable.
When the record updated by another user, you should throw exception with warning message "Concurrency Conflict !!!"
Their code is the following:
namespace ObjectDataSourceDemo
{
    [DataObject]
    public class ConflictedBooks
    {

        public ConflictedBooks()
        {
            //default constructor
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataTable getBooksDataTable()
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = WebConfigurationManager.ConnectionStrings["eBooksLibraryConnectionString"].ConnectionString;
                SqlCommand cmd = new SqlCommand("SELECT ISBN,Name,Page_Count,Publication_Date,Author,Price,CategoryID,PublisherID FROM Books ORDER BY Name", conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dtBooks = new DataTable("Books");
                try
                {
                    da.Fill(dtBooks);
                    return dtBooks;
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.Message);
                }
            }
        }

        public int UpdateBook(string name, int page_count, DateTime publication_date, string author, decimal price, int categoryID, int publisherID,
                               string original_name, int original_page_count, DateTime original_publication_date, string original_author,
                              decimal original_price, int original_categoryID, int original_publisherID, string original_isbn)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = WebConfigurationManager.ConnectionStrings["eBooksLibraryConnectionString"].ConnectionString;
            // Initialize command
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "UPDATE Books SET Name=@name ,Page_Count=@page_count,Publication_Date=@publication_date,Author=@author,Price=@price,CategoryID=@categoryID,PublisherID=@publisherID " +
                              "Where ISBN=@original_isbn AND " +
                              "Name=@original_name AND Page_Count=@original_page_count AND " +
                              "Publication_Date=@original_publication_date AND Author=@original_author AND " +
                              "Price=@original_price AND CategoryID=@original_categoryID AND PublisherID=@original_publisherID";

            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@page_Count", page_count);
            cmd.Parameters.AddWithValue("@publication_date", publication_date);
            cmd.Parameters.AddWithValue("@author", author);
            cmd.Parameters.AddWithValue("@price", price);
            cmd.Parameters.AddWithValue("@categoryID", categoryID);
            cmd.Parameters.AddWithValue("@publisherID", publisherID);

            cmd.Parameters.AddWithValue("@original_name", original_name);
            cmd.Parameters.AddWithValue("@original_page_count", original_page_count);
            cmd.Parameters.AddWithValue("@original_publication_date", original_publication_date);
            cmd.Parameters.AddWithValue("@original_author", original_author);
            cmd.Parameters.AddWithValue("@original_price", original_price);
            cmd.Parameters.AddWithValue("@original_categoryID", original_categoryID);
            cmd.Parameters.AddWithValue("@original_publisherID", original_publisherID);
            cmd.Parameters.AddWithValue("@original_isbn", original_isbn);

            using (conn)
            {

                conn.Open();

                int rowsAffected = cmd.ExecuteNonQuery();

                if (rowsAffected == 0)
                {
                    throw new ApplicationException("Concurrency Conflict !!!");
                }

                return rowsAffected;
            }
        }

    }
protected void ObjectDataSource_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            try
            {
                if (e.AffectedRows == 0)
                {
                    lblError.Text = "Concurrency Conflict !!!";
                }
                else
                {
                    Response.Write("Update Complete .");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

HTML Markup :
 <asp:GridView ID="GridView1" runat="server" 
        DataSourceID="ObjectDataSourceConflict" DataKeyNames="ISBN">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>
    
    <asp:ObjectDataSource ID="ObjectDataSourceConflict" runat="server" 
        ConflictDetection="CompareAllValues" 
        OldValuesParameterFormatString="original_{0}" SelectMethod="getBooksDataTable" 
        TypeName="ObjectDataSourceDemo.ConflictedBooks" UpdateMethod="UpdateBook" 
        onupdated="ObjectDataSource_Updated">
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="page_count" Type="Int32" />
            <asp:Parameter Name="publication_date" Type="DateTime" />
            <asp:Parameter Name="author" Type="String" />
            <asp:Parameter Name="price" Type="Decimal" />
            <asp:Parameter Name="categoryID" Type="Int32" />
            <asp:Parameter Name="publisherID" Type="Int32" />
            <asp:Parameter Name="original_name" Type="String" />
            <asp:Parameter Name="original_page_count" Type="Int32" />
            <asp:Parameter Name="original_publication_date" Type="DateTime" />
            <asp:Parameter Name="original_author" Type="String" />
            <asp:Parameter Name="original_price" Type="Decimal" />
            <asp:Parameter Name="original_categoryID" Type="Int32" />
            <asp:Parameter Name="original_publisherID" Type="Int32" />
            <asp:Parameter Name="original_isbn" Type="String" />
        </UpdateParameters>
    </asp:ObjectDataSource>



My questions are:

1) why the exception is not caught from the method ObjectDataSource_Updated when it exists try .. catch block?

2) Why in the method parameter ObjectDataSource_Updated e.AffercedRows still
value of -1 and in both cases - when the update success and when it fails?

3) Do you know someone, perhaps another way to solve this problem, or to write
a link that deals with this kind of problem!

thanks in advance !

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with ObjectDataSource control in a multiuser environment.

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Trouble with ObjectDataSource control in a multiuser environment.

Posted 21 August 2012 - 05:32 AM

try adding return @@ROWCOUNT at the end of your update. If the exception isn't being caught in the Updated event it may be because the exception may be getting thrown as part of the Updating event.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1