View Single Post
  #10 (permalink)  
Old March 13th, 2009, 04:05 AM
RushNP774 RushNP774 is offline
Authorized User
Join Date: Mar 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts

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

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.

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: 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:

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!