Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics 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 Display Modes
  #1 (permalink)  
Old March 12th, 2009, 02:20 AM
Authorized User
 
Join Date: Mar 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
Question Using a dropdown box in a GridView - How do I update the record with the dd's value?

I have a GridView control that gets its data correctly from a table (called Activity) with three columns:
  • Id - Automatically generated primary key
  • Name - Name of the activity
  • Category - Category of the activity
The [Category] field is populated by a dropdown list box that gets the categories from another table (and it works fine). When I update a record, I can change the [Name] of the activity (because it's a default textbox generated by the GridView), and the record is updated fine. But the value of the ddl box is not inserted into the record.

I'm pretty sure I need to change my UpdateCommand of the sqlDataSource that feeds my GridView, and/or add code to my dropdown box's but I'm at a loss of how to do that. Here is the template field of my GridView:

Code:
<EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" 
        runat="server" AutoPostBack="False" DataSourceID="SqlDataSource3" 
        DataTextField="Name" DataValueField="Name">
    </asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
</ItemTemplate>
And the UpdateCommand of my data source:

Code:
"UPDATE [Activity] SET [Name] = @Name, [Category] = @Category WHERE [Id] = @original_Id 
AND [Name] = @original_Name AND (([Category] = @original_Category) OR ([Category] IS NULL AND
 @original_Category IS NULL))"
I've seen similar questions asked many times, but can't seem to find one that's exactly my problem. If you know of an article or forum post that would help me out, I'd love to see it. Thanks in advance!

Last edited by RushNP774; March 12th, 2009 at 03:04 PM. Reason: Broke the SQL command up in different lines
  #2 (permalink)  
Old March 12th, 2009, 10:58 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

What update parameters are declared in the SqlDataSource?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #3 (permalink)  
Old March 12th, 2009, 03:02 PM
Authorized User
 
Join Date: Mar 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Lee Dumond View Post
What update parameters are declared in the SqlDataSource?
Thanks for the reply Lee. I'm confused about what you requested because I thought I posted the UpdateCommand. Did you want this instead:

Code:
<UpdateParameters>
   <asp:Parameter Name="Name" Type="String" />
   <asp:Parameter Name="Category" Type="Int32" />
   <asp:Parameter Name="original_Id" Type="Int32" />
   <asp:Parameter Name="original_Name" Type="String" />
   <asp:Parameter Name="original_Category" Type="Int32" />
</UpdateParameters>
It's the code that was automatically generated by VWD when I created the SqlDataSource. It does seem odd because there's no link to the DropDownBox's value in there. I converted the "Categories" column to a template field, and populated it with a dropdown box that pulls the names from my Categories table (and it works surprisingly :)), but just doesn't update.

I'd be happy to post more of my code if it would help you out, I just didn't want to flood my post with code that wasn't relevant to the problem. Thanks again!
  #4 (permalink)  
Old March 12th, 2009, 03:18 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by RushNP774 View Post
It does seem odd because there's no link to the DropDownBox's value in there.
BINGO.

Yes, that's why I wanted to see your Update parameters, because, as I suspected, there's no ControlParameter that passes the selected value of the dropdownlist. Hence, the Update statement has no idea where to get the @Category value from.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #5 (permalink)  
Old March 12th, 2009, 03:27 PM
Authorized User
 
Join Date: Mar 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Lee Dumond View Post
BINGO.

Yes, that's why I wanted to see your Update parameters, because, as I suspected, there's no ControlParameter that passes the selected value of the dropdownlist. Hence, the Update statement has no idea where to get the @Category value from.
Haha, I thought I had something right . I'm not new to programming, just new to C#, so I have a decent handle on program logic and the flow. ASP.NET is just so new to me that I don't know many of the intricacies of how to accomplish even the most basic stuff. Could you be so kind as to point me to a tutorial that might help me out?

Thanks!
  #6 (permalink)  
Old March 12th, 2009, 09:53 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Actually, looking at what you're trying to do a bit more closely, it looks like what you have is probably OK. From here, you should be able to simply bind the SelectedValue property of the DropDownList using two-way databinding, like this:

Code:
<EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" 
        runat="server" AutoPostBack="False" DataSourceID="SqlDataSource3" 
        DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Category") %>'>
    </asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
</ItemTemplate>
That should work.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #7 (permalink)  
Old March 13th, 2009, 01:27 AM
Authorized User
 
Join Date: Mar 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Lee Dumond View Post
Actually, looking at what you're trying to do a bit more closely, it looks like what you have is probably OK. From here, you should be able to simply bind the SelectedValue property of the DropDownList using two-way databinding, like this:
I tried that and pretty sure I entered it exactly as you typed it (copy & paste FTW). It compiled fine, but when I went to "edit" the record, I received this error:

[ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value]

I don't really know what that means, but it seems odd that the DDL couldn't find a value, because it was working fine before I put the Bind command in. I also tried:
  1. Entering a record manually and typed in a Category for it to see if the fact that the Category value for that item was NULL to begin with. No dice. Although I did figure out that the Bind in the ItemTemplate command works because I could see the value I typed in. I guess that's a plus.
  2. Moved SqlDataSource3 (the one populating my dropdown list) to the top of the page to see if for some reason the page wasn't populating the DDL because it hadn't arrived at it in the code yet. Didn't help either.
  3. Changed "Name" in the DataTextField="Name" DataValueField="Name" properties to "Category", and got a different error. I don't know what those properties really do, but I thought I'd try. I got a different error when I did that...
  4. Added AppendDataBoundItems="True" to the DDL, but that didn't help either.
  5. Added "<asp:ListItem Value="0">Select Category</asp:ListItem>" between the asp:DropDownList control to see if manually forcing a default value in there would help (and then let it populate from my other table after that), but that didn't help either.
Someone over on the MS forums had this same problem on this thread: http://forums.asp.net/t/1056921.aspx, and some of the suggestions worked for some people, but not me.

The Beginning ASP.NET book makes all this stuff look so easy. I knew it sounded too good to be true . Oh well, I'll keep at it, because this should be an easy fix.
  #8 (permalink)  
Old March 13th, 2009, 02:13 AM
Authorized User
 
Join Date: Mar 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Lee Dumond View Post
Actually, looking at what you're trying to do a bit more closely, it looks like what you have is probably OK. From here, you should be able to simply bind the SelectedValue property of the DropDownList using two-way databinding, like this:
Thanks Lee, I tried that, but no dice. I had a huge reply typed to you that explained a bunch of things I tried, but vBulletin doesn't save your ViewState, and I lost it . Long story short, I added the SelectedValue property to my DDL, and got this error:

ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

I'm guessing that it's because the instant the GridView goes into edit mode, the DDL is created but has a null value in it because it hasn't pulled the data from the DataSource yet. Many people all over the net are having this trouble too, and I've tried a ton of things. I also added

Code:
<asp:ListItem Value="0">Select Category</asp:ListItem>
Inside the DDL, hoping that if I forced a default value into it, that it would work, but no. I guess the Bind command tries to run before I have the chance to populate the DDL at all.

Based on their suggestions, I also added AppendDataBoundItems="True" to it, but it doesn't really fix anything. Funny though, when I run the site after adding that, but without the Bind command, the process runs without errors, but the row doesn't update.

I appreciate your sticking with this.

Last edited by RushNP774; March 13th, 2009 at 02:18 AM.
  #9 (permalink)  
Old March 13th, 2009, 02:58 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

I tried this on the Northwind sample database and it definitely works. This is the accepted way of doing this, trust me.

The reason you are getting this error is that you have Categories in the ID-Name-Category table that do not exist in the seperate Categories table. This could be due to a completely missing record, or even a slight typo.

Let's say you have this:

Code:
ID    Name          Category
============================
1     Bananna       Fruit
2     Ford          Car
3     Bagel         Bread
4     Apple         Fruit
5     Chevy         Car
6     Pepsi         Beverage
And let's say you have in your categories table:

Code:
Category
========
Fruit
Car
Bread
As the Gridview binds, everything will be fine up until record #6. Record number 6 will attempt to set the SelectedValue of the DropDownList to "Beverage", but it will fail because the DropDown list is bound the the Categories table, and thus contains only the Items Fuit, Car, and Bread. It cannot find a value of "Beverage" to set the SelectedValue to, and thus, it says:

Quote:
Hey! The value of category for this record is "Beverage", but the DropDownList that is supposed to represent that in the GridView has no item with a value of "Beverage", and thus, I cannot do SelectedValue="Beverage"!!!
Or, in computerese:

Code:
ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Look for this in your data, and you will be able to fix the problem.

After you do that, you should PREVENT this problem from happening by establishing a foreign key relationship between the two tables, and ONLY storing the IDs from Table 2 in Table 1. In other words:

Code:
ID    Name          CategoryID
==============================
1     Bananna       1
2     Ford          2
3     Bagel         3
4     Apple         1
5     Chevy         2
6     Pepsi         4
Code:
ID   Category
========
1    Fruit
2    Car
3    Bread
4    Beverage
By establishing a key relationship between the tables, you will never be able to store a CategoryID in Table 1 which does not already exist as a, ID in Table 2. Under that scenario, this error can never occur.

Hope this makes sense for you.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #10 (permalink)  
Old March 13th, 2009, 04:05 AM
Authorized User
 
Join Date: Mar 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Wow, Lee, this is gonna be a long post.

Quote:
The reason you are getting this error is that you have Categories in the ID-Name-Category table that do not exist in the separate Categories table. This could be due to a completely missing record, or even a slight typo.
I understand what you are saying, but I don't think that's the case. I have only one record in the [Activities] table at the moment, and have edited it manually with different values to test them out. No matter what I have in the category cell (an Id, or text that matches/doesn't match one of the values from the [Categories] table), it will throw that error at runtime as long as the SelectedValue='<%# Bind("Category") %>' statement is there.

Quote:
As the Gridview binds, everything will be fine up until record #6. Record number 6 will attempt to set the SelectedValue of the DropDownList to "Beverage", but it will fail because the DropDown list is bound the the Categories table, and thus contains only the Items Fuit, Car, and Bread. It cannot find a value of "Beverage" to set the SelectedValue to, and thus, it says:
As long as I'm not in edit mode, my GridView pulls the data from my Category column (in [Activities] table), and displays it fine. The problem simply comes when I go to edit the item. Maybe I'm thinking that "Binding" is doing something a bit differently than what I intend. I only want to display the dropdown box when I edit the item, and update the record in the [Activities] table with the value from it. I may be misunderstanding what you're saying (probably so, it's 3:00 in the morning), but It seems like you think I'm trying to bind the data that's already in my [Activities] table to the dropdown, but it's not.

Quote:
...you should PREVENT this problem from happening by establishing a foreign key relationship between the two tables, and ONLY storing the IDs from Table 2 in Table 1. In other words...
That's what I had in mind when I originally envisioned these two tables, but I tried that and couldn't get it to work. I tried it again as per your suggestion, and here is what my tables look like:

Code:
Table1 - Activities
==============
Id,  int (primary key)
Name, nchar(75)
CategoryId, int

Table2 - Categories
===============
Id, int (primary key)
Name, nchar(75)
I then created a diagram and dropped the "Id" from the [Categories] table onto the "CategoryId" field of [Activities] table. I think that's correct.

Now it's all well & good, but:
  • I still am getting my error when editing records from my GridView
  • My GridView displays the "CategoryID" of the activity, instead of the "Name" it is linked to. I thought creating a relationship would cause the GridView to display the name, but I guess not. Guess I'll have to write more code to display that.
I hope I'm making sense. My next reply will be after I've had some sleep Thanks again Lee!
 


Thread Tools
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
gridview using dropdown komalpriya ASP.NET 2.0 Professional 1 November 1st, 2007 12:34 AM
gridview with a dropdown list paullam71 ASP.NET 2.0 Basics 0 July 22nd, 2007 09:43 AM
Dropdown Box problem migarich ASP.NET 1.0 and 1.1 Basics 3 March 22nd, 2006 09:28 AM
dropdown list not selecting record on page load whyulil ASP.NET 1.0 and 1.1 Basics 1 September 8th, 2004 08:29 AM
update dropdown values mateenmohd SQL Server 2000 0 October 9th, 2003 03:23 AM



All times are GMT -4. The time now is 01:51 AM.


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