 |
| ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 3rd, 2008, 05:05 PM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Can't get GridView to update
I'm trying to do some very simple databinding with a GridView but can't get it to update. I have a select statement set in an SqlDataSource that returns a result table like this:
MajorStoneID | BandStyleID | TypeID | CutID | Weight | Color | Clarity | Qty | UnitPrice | StoneName | CutName
StoneName and CutName come from joins with other tables. The update procedure for the SqlDataSource updates the following fields:
TypeID | CutID | Weight | Color | Clarity | Qty | UnitPrice
These are the only fields available for editing in the GridView. The problem is, that when I press update with a breakpoint on SqlDataSource.Updating, and look at each parameter in e.Command.Parameters, I see that the values have not been updated. They are still the original values. If I set a breakpoint on GridView.Updating, the new values collection also does not have the updated values. Why aren't they updating?!
|
|

February 3rd, 2008, 05:48 PM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
To simplify the problem, if I set up my SqlDataSource the same way, then drop a datagrid on the form and autogenerate the columns and edit buttons, I still get the problem. After clicking update, the NewValues collection of the GridViewUpdateEventArgs does not contain the updated values.
|
|

February 5th, 2008, 12:04 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've (FINALLY) discovered the problem. In my Page_Load event, I call DataBind() to bind other bindings on the page. This prevents the grid from updating for some reason. I don't know why yet, or how to bind the page...but if I remove DataBind from Page_Load, updating is successful. Hope this helps someone. I'll post back if I find out more.
|
|

February 5th, 2008, 03:57 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
When you call DataBind in Page_Load, you tell your controls to data bind. This means fresh data is retrieved from the database as soon as the page loads. This in turn means any customizations to the data (e.g. edits) are overwritten.
To avoid that problem, wrap the call to DataBind in a check for PosatBack. E.g.:
if (!Page.IsPostBack)
{
}
This way, DataBind is only called when the page initially loads and leaves your edits alone on postback.
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

February 11th, 2008, 06:45 PM
|
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
My advice would be to steer clear of the sqldatasource and use a 3-tiered approach to retrieving, displaying and collecting data.
Get the data from your database using a sqldatareader and store it in a custom collection by defining your own custom object. Then bind to the collection using the pageload event and when page.ispostback = false. The only other time you'll need to call the bind method is when you change the data. I'd also advise staying clear of the update/delete events of the gridview too.
You can do all of this in very minimal code.
|
|

February 15th, 2008, 12:45 PM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I also had to make sure I update certain page elements in several other events after moving the DataBind call into the !IsPostback section.
A three tier approach would've been overkill for this page but yes it is a more robust design. Thanks.
|
|
 |