Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 3rd, 2008, 05:05 PM
Authorized User
 
Join Date: Jan 2005
Location: TX USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?!

  #2 (permalink)  
Old February 3rd, 2008, 05:48 PM
Authorized User
 
Join Date: Jan 2005
Location: TX USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

  #3 (permalink)  
Old February 5th, 2008, 12:04 AM
Authorized User
 
Join Date: Jan 2005
Location: TX USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

  #4 (permalink)  
Old February 5th, 2008, 03:57 AM
Imar's Avatar
Wrox Author
Points: 72,038, Level: 100
Points: 72,038, Level: 100 Points: 72,038, Level: 100 Points: 72,038, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,080
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
  #5 (permalink)  
Old February 11th, 2008, 06:45 PM
Authorized User
Points: 192, Level: 3
Points: 192, Level: 3 Points: 192, Level: 3 Points: 192, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: God's country , United Kingdom.
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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.

  #6 (permalink)  
Old February 15th, 2008, 12:45 PM
Authorized User
 
Join Date: Jan 2005
Location: TX USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with gridview update. ccarpenter ASP.NET 2.0 Basics 0 March 20th, 2008 09:18 AM
using gridview control, how to update? aaronabdiel ASP.NET 2.0 Basics 2 February 13th, 2008 08:41 AM
Gridview using Update *only* pbranumfl ASP.NET 2.0 Basics 2 December 4th, 2006 03:21 PM
Gridview Update Issue jpgregg ASP.NET 2.0 Professional 0 September 1st, 2006 07:35 PM
FOXPRO gridview update webclown ASP.NET 1.x and 2.0 Application Design 0 May 1st, 2006 12:43 PM



All times are GMT -4. The time now is 12:32 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.