I'm a newbie at linq, Ajax and c#. I am not new to SQL Server, or VB. I am getting the error:
'ReportTypeID' is not a foreign key column and cannot be used here.
Yes I looked at http://forums.asp.net/t/1254559.aspx and found one of my errors.
No, I am not using views so "...is not a foreign key column and cannot be used here?" was of little use.
As far as I can tell, it is correctly configured to handle the foreign key.
The two tables are configured as follows:
CREATE TABLE [dbo].[Report](
[Id] [INT] IDENTITY(1,1) NOT NULL,
[ReportTypeID] [INT] NOT NULL,
CONSTRAINT [PK_Report] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Report] ADD CONSTRAINT [DF_Report_ReportTypeID] DEFAULT ((1)) FOR [ReportTypeID]
GO
ALTER TABLE [dbo].[Report] WITH CHECK ADD CONSTRAINT [FK_Report_ReportType] FOREIGN KEY([ReportTypeID])
REFERENCES [dbo].[ReportType] ([TypeValue])
GO
ALTER TABLE [dbo].[Report] CHECK CONSTRAINT [FK_Report_ReportType]
GO
CREATE TABLE [dbo].[ReportType](
[TypeValue] [INT] NOT NULL,
[TypeDescr] [VARCHAR](50) NOT NULL,
CONSTRAINT [PK_ReportType] PRIMARY KEY CLUSTERED
(
[TypeValue] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
The SQL join is as follows:
SELECT * FROM Report r INNER JOIN ReportType rt ON r.ReportTypeID = rt.TypeValue
Finally, the C# dbml definitions on the appropriate columns (much attenuated) are as follows:
<Table Name="dbo.Report" Member="Reports">
<Type Name="Report">
<Column Name="ReportTypeID" Type="System.Int32" DbType="INT NOT NULL" CanBeNull="false" />
<Association Name="ReportType_Report" Member="ReportType" ThisKey="ReportTypeID" OtherKey="TypeValue" Type="ReportType" IsForeignKey="true" />
</Type>
</Table>
<Table Name="dbo.ReportType" Member="ReportTypes">
<Type Name="ReportType">
<Column Name="TypeValue" Type="System.Int32" DbType="INT NOT NULL" IsPrimaryKey="true" CanBeNull="false" />
<Association Name="ReportType_Report" Member="Reports" ThisKey="TypeValue" OtherKey="ReportTypeID" Type="Report" />
</Type>
</Table>
The SQL works correctly and there are no dis-joins in the data, the data is correct when I step through the program. The value of the ReportTypeID=1 which is valid.
The TypeValue in the ReportType table is not auto generated, it is unique and a primary key.
I suspect this has something to do with the dbml definitions. I am still definitely missing something.
edit: Add the visual relation. The setting is based on the other relation in the image which works.
The error is reported in the Ajax field template code "ForeignKeyRequired_Edit.ascx.cs"
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
if (Mode == DataBoundControlMode.Edit)
{
string foreignkey = ForeignKeyColumn.GetForeignKeyString(Row); // Error On This Line
ListItem item = DropDownList1.Items.FindByValue(foreignkey);
if (item != null)
{
DropDownList1.SelectedValue = foreignkey;
}
}
}
Edit I believe I found the problem, I need some documentation to find the answer.
There is a switch statement in the code that requires editing. I'm getting to my template, but not to my field.
case "Source": // This Works
items = StaticCache.Sources.AsQueryable().Where(att.WhereClause).Select(r => new ListItem { Text = r.Name, Value = r.Id.ToString() }).ToArray();
break;
// Problem Code - This One Doesn't - Not Sure Which Of These To Use
case "ReportTypeTable": // Based On The Table Definition -- Internal Doc Points To This One
items = StaticCache.ReportTypes.AsQueryable().Where(att.WhereClause).Select(r => new ListItem { Text = r.TypeDescr, Value = r.TypeValue.ToString() }).ToArray();
break;
Edit
The "ReportType" table is considered to be an "invalid object" by SQL Server. Could this be the problem?
edit
I'm now looking into active directory as a possible problem. Edit Not an active directory problem and the "invalid object" error was an intellesense problem (fixed).
Edit: I have part of the answer. The documentation is not clear. The answer lies in the dbml definition of the table. Fortunately I renamed each level of that table so that references to each of the levels of "ID" in the definition had unique, but related, names. The partial answer is to use the dbml table name. I no longer get this error. I just don't have any data in the dropdown list. See: http://www.seekwaytech.com/2011/02/27/asp-net-4-0-dynamic-data-foreign-keys-show-up-in-a-text-box/ and http://forums.asp.net/t/1254559.aspx in that order. (The first explains the answer to the second)
Edit Finally, getting rid of all of the garbage tests and going back to the dynamic object did the job.
<asp:DynamicControl runat="server" DataField="ReportTypeTable" DataValueField="TypeValue" DataTextField="TypeDescr" Mode="Edit" UIHint="ForeignKeyRequired" CssClass="general" />