Answered by:
ListItem Error

Question
-
User1715683220 posted
I watched all of the videos about Dynamic Data on this site. I thought this was very cool, so I tried it out but I am getting errors centered around this code:
1 public partial class ForeignKey_EditField : System.Web.DynamicData.FieldTemplateUserControl {
2 protected void Page_Load(object sender, EventArgs e) {
3 if (DropDownList1.Items.Count == 0) {
4 if (!Column.IsRequired) {
5 DropDownList1.Items.Add(new ListItem("[Not Set]", ""));
6 }
7
8 PopulateListControl(DropDownList1);
9 }
10 }
11
12 protected override void OnDataBinding(EventArgs e) {
13 base.OnDataBinding(e);
14
15 if (Mode == DataBoundControlMode.Edit) {
16 string foreignkey = ForeignKeyColumn.GetForeignKeyString(Row);
17 ListItem item = DropDownList1.Items.FindByValue(foreignkey);
18 if (item != null) {
19 DropDownList1.SelectedValue = foreignkey;
20 }
21 }
22 }
23
24 protected override void ExtractValues(IOrderedDictionary dictionary) {
25 // If it's an empty string, change it to null
26 string val = DropDownList1.SelectedValue;
27 if (val == String.Empty)
28 val = null;
29
30 ExtractForeignKey(dictionary, val);
31 }
32
33 public override Control DataControl {
34 get {
35 return DropDownList1;
36 }
37 }
38 }
39
Underlined is what VS 2008 underlines. Errors for each underlined code are:
Error 1 'ListItem' does not contain a constructor that takes '2' arguments
Error 2 The best overloaded method match for 'System.Web.UI.WebControls.ListItemCollection.Add(string)' has some invalid arguments
Error 3 Argument '1': cannot convert from 'ListItem' to 'string'
Error 4 Cannot implicitly convert type 'System.Web.UI.WebControls.ListItem' to 'ListItem'
Here is the definition for ListItem that Dynamic Data generates:
1 [Table(Name="dbo.ListItems")] 2 public partial class ListItem : INotifyPropertyChanging, INotifyPropertyChanged 3 { 4 5 private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); 6 7 private int _ListID; 8 9 private int _ListItemID; 10 11 private string _ListItem1; 12 13 private EntityRef<LIST> _List; 14 15 #region Extensibility Method Definitions 16 partial void OnLoaded(); 17 partial void OnValidate(System.Data.Linq.ChangeAction action); 18 partial void OnCreated(); 19 partial void OnListIDChanging(int value); 20 partial void OnListIDChanged(); 21 partial void OnListItemIDChanging(int value); 22 partial void OnListItemIDChanged(); 23 partial void OnListItem1Changing(string value); 24 partial void OnListItem1Changed(); 25 #endregion 26 27 public ListItem() 28 { 29 this._List = default(EntityRef<LIST>); 30 OnCreated(); 31 } 32 33 [Column(Storage="_ListID", DbType="Int NOT NULL")] 34 public int ListID 35 { 36 get 37 { 38 return this._ListID; 39 } 40 set 41 { 42 if ((this._ListID != value)) 43 { 44 if (this._List.HasLoadedOrAssignedValue) 45 { 46 throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); 47 } 48 this.OnListIDChanging(value); 49 this.SendPropertyChanging(); 50 this._ListID = value; 51 this.SendPropertyChanged("ListID"); 52 this.OnListIDChanged(); 53 } 54 } 55 } 56 57 [Column(Storage="_ListItemID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] 58 public int ListItemID 59 { 60 get 61 { 62 return this._ListItemID; 63 } 64 set 65 { 66 if ((this._ListItemID != value)) 67 { 68 this.OnListItemIDChanging(value); 69 this.SendPropertyChanging(); 70 this._ListItemID = value; 71 this.SendPropertyChanged("ListItemID"); 72 this.OnListItemIDChanged(); 73 } 74 } 75 } 76 77 [Column(Name="ListItem", Storage="_ListItem1", DbType="NVarChar(100) NOT NULL", CanBeNull=false)] 78 public string ListItem1 79 { 80 get 81 { 82 return this._ListItem1; 83 } 84 set 85 { 86 if ((this._ListItem1 != value)) 87 { 88 this.OnListItem1Changing(value); 89 this.SendPropertyChanging(); 90 this._ListItem1 = value; 91 this.SendPropertyChanged("ListItem1"); 92 this.OnListItem1Changed(); 93 } 94 } 95 } 96 97 [Association(Name="List_ListItem", Storage="_List", ThisKey="ListID", OtherKey="ListID", IsForeignKey=true)] 98 public List List 99 { 100 get 101 { 102 return this._List.Entity; 103 } 104 set 105 { 106 List previousValue = this._List.Entity; 107 if (((previousValue != value) 108 || (this._List.HasLoadedOrAssignedValue == false))) 109 { 110 this.SendPropertyChanging(); 111 if ((previousValue != null)) 112 { 113 this._List.Entity = null; 114 previousValue.ListItems.Remove(this); 115 } 116 this._List.Entity = value; 117 if ((value != null)) 118 { 119 value.ListItems.Add(this); 120 this._ListID = value.ListID; 121 } 122 else 123 { 124 this._ListID = default(int); 125 } 126 this.SendPropertyChanged("List"); 127 } 128 } 129 } 130 131 public event PropertyChangingEventHandler PropertyChanging; 132 133 public event PropertyChangedEventHandler PropertyChanged; 134 135 protected virtual void SendPropertyChanging() 136 { 137 if ((this.PropertyChanging != null)) 138 { 139 this.PropertyChanging(this, emptyChangingEventArgs); 140 } 141 } 142 143 protected virtual void SendPropertyChanged(String propertyName) 144 { 145 if ((this.PropertyChanged != null)) 146 { 147 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 148 } 149 } 150 }
Can anyone comment?
Sunday, December 21, 2008 9:10 PM
Answers
-
User1641955678 posted
Note that the naming of your entity classes comes from the ORM and not from Dynamic Data. The fact that the name conflict causes issue is to be expected, though I think you could have avoided it by changing the namespace of the field template.
David
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 22, 2008 2:53 AM
All replies
-
User1715683220 posted
Not long after I posted, I noticed that one the attribute at the top of the second code block I posted. The name of one of my tables was ListItems, and Linq to SQL had named the ORM object ListItem which was conflictin with the .net type ListItem. I changed the object name to LstItem and it worked.
I think Dynamic Data should have been smarter than this.
Sunday, December 21, 2008 11:06 PM -
User1641955678 posted
Note that the naming of your entity classes comes from the ORM and not from Dynamic Data. The fact that the name conflict causes issue is to be expected, though I think you could have avoided it by changing the namespace of the field template.
David
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 22, 2008 2:53 AM -
User1715683220 posted
Oh! Thanks for the info!
Monday, December 22, 2008 8:14 AM