Problem with ObjectDataSource and Update

N

Norbert Ruessmann

Hello group,

I have a business object, which contains Orders. Each Order has a list of
OrderItems. Orders are collected in a list List<Order>, OrderItems for each
order are of type v<OrderItems>. From a database view this is a one to many
relation.

I define two GridView controls: one for displaying/editing the Orders,
another one for displaying/editing the OrderItems. I also define two
datasource controls (ObjectDataSource) for each grid.

The Grid for Orders is selectable, therefore for the DataSource for
OrderItems uses a select method which gets the id of the order selected in
first grid, and as parameter for this method I use the SelectIndex of the
Grid for Orders (there is a tutorial on http://asp.net about how to do this)
.. This works fine. If I select 10 Orders into the grid (from database), and
then select (using 'select' link in grid's row) one of them in the GridView,
the corresponding OrderItems are automatically displayed in the second grid.


Now my problem: I want to edit the OrderItems. I have to specify an update
method which gets an OrderItem object. The class OrderItem must have a
default constructor, which is called by the databinding mechanism internally
before calling the Update method I specified. But: my Update method does not
know which OrderItem needs to be updated.
To undestand this problem: When the form is display, I select all relevant
Oders and their OrderItems into my business objects. Then I use ASP.NET
Session State to keep these Items. When the user wants to edit an item, the
item kept in Session State needs to be updated.
But how do I find the item which needs to be updated? The only solution I
see is that I must search all Orders, and then all OrderItems of each Order,
until I find the item which needs to be updated. But for me this is not a
solution.

Any other ideas, or links for better understanding data binding?


Thanks in advance

Norbert Ruessmann
 
W

Walter Wang [MSFT]

Hi Norbert,

It's my understanding that OrderItem should have an unique key (if it's
returned from database, the primary key). When you bind the OrderItem list
to the GridView, the key should be set in GridView's DataKeys property.
Then when you update the OrderItem, this key can be used to find the
OrderItem from the list you saved in your session state.

Also, you could handle the Updating event of ObjectDataSource and add
additional parameters if required by your update method.

#Using Parameters with the ObjectDataSource Control
http://msdn2.microsoft.com/en-us/library/57hkzhy5.aspx

#GridView.DataKeys Property (System.Web.UI.WebControls)
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.
datakeys.aspx

If you need further information on this, would you please post some code?
Thank you.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Norbert,

Would you please let me know what do you think of my previous reply? Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
N

Norbert Ruessmann

Hi Walter,

thanks for your first reply -- it is OK.

There is something that I do not like very much, but that's not related to
your reply. I don't like the way databinding works in ASP.NET.

I cannot tell you exactly, what I do not like. But it is related to updating
data in my business object; the Update() method gets the new data, and I
have to do some searching to find the real business object I have to
update.

Regards
Norbert Ruessmann
 
W

Walter Wang [MSFT]

Hi Norbert,

Thank you very much for letting us know your opinion about our products.

I understand your concerns about ASP.NET data binding. Since ASP.NET pages
are stateless, its data binding mechanism is different from WinForm data
binding. After you bound your business objects to the GridView, the
business objects are disconnected from the GridView. To update them, we
have to pass the updated data back to the underlying DataSource. For
SqlDataSource, it's automatically using the DataKeys to find the correct
record to update in database. For ObjectDataSource, we will have to
implement it using similar approach to determine which object to update.

In your specific scenario, I understand that you have a list of Orders
saved in Session state. I believe you can use the SelectedValue of the
first GridView to know which Order (use its primary key) first, then we can
use the OrderItem id (or similar fields) to find which OrderItem of the
Order to update.

We will be glad to be of assistance if you need further information
regarding this post or any other programming related questions. You can
find all MSDN Managed Newsgroups here:
http://msdn.microsoft.com/subscriptions/managednewsgroups/list.aspx

Also, you're welcome to submit your feedback at
http://connect.microsoft.com/Main/content/content.aspx?ContentID=2220 which
is monitored by our product team directly.

Would you please reply here (or email me directly if you wish) to let me
know whether or not my reply is useful to your question? Thank you.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Y

Yan-Hong Huang[MSFT]

Hi Norbert,

This is Yanhong, manager of MSDN newsgroup support team. Walter discussed
this issue with me.

The mechanism of web application is a little bit different from windows
form application. That is why some behavior is different here. Walter
posted more rationales in his last reply. If there is any unclear, please
feel free to reply here and we are glad to work with you closely.

Surely as Walter mentioned, you can also submit product suggestion at our
connect web site. We appreciate any feedback from you.

Thanks very much and have a good day.

Sincerely,
Yanhong Huang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top