I have the following tables in my database which are as given under:
Table 1: CREATE TABLE [dbo].[PromotionDiscountValues]( [PromotionID] [int] IDENTITY(1,1) NOT NULL, [DiscountAmount] [int] NOT NULL, CONSTRAINT [PK_PromotionDiscountValues] PRIMARY KEY CLUSTERED ( [PromotionID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE
= OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Table 2: CREATE TABLE [dbo].[PromotionCodes]( [PromotionCode] nchar NOT NULL, [CustomerPromotionDiscountID] [int] NOT NULL, [EmployeePromotionDiscountID] [int] NOT NULL, [IsActive] [bit] NOT NULL, CONSTRAINT [PK_PromotionCodes_1] PRIMARY KEY CLUSTERED
( [PromotionCode] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
There is a foreign key relationship between columns (1) PromotionID of table PromotionDiscountValues and CustomerPromotionDiscountID of table PromotionCodes and (2) PromotionID of table PromotionDiscountValues and EmployeePromotionDiscountID of table PromotionCodes
When i browse the PromotionCodes table in the dynamic data listing, the following columns get displayed:
PromotionCode
IsActive
PromotionDiscountValues (which basically contains the PromotionID values defined for CustomerPromotionDiscountID)
PromotionDiscountValues1 (which basically contains the PromotionID values defined for EmployeePromotionDiscountID)
My requirement is instead of getting the PromotionID values displayed for columns CustomerPromotionDiscountID & EmployeePromotionDiscountID, it should display the respective DiscountAmount. Which is not happenning for some reason.
I somehow found a workaround where if i define the datatype for columns CustomerPromotionDiscountID and EmployeePromotionDiscountID as nchar or nvarchar, it displays the DiscountAmount.
Wanted to know if there is any better way of doing this without changing the datatype for my columns? Or am I missing something?
[MetadataType(typeof(PromotionCodeMetadata))]
public partial class PromotionCode
{
public override string ToString()
{
return this.PromotionCode1;
}
internal class PromotionCodeMetadata
{
[Display(Name = "Promotion Code")]
public Object PromotionCode1 { get; set; }
[Display(Name = "Customer Promotion Discount")]
public Object PromotionDiscountValue { get; set; }
[Display(Name = "Employee Promotion Discount")]
public Object PromotionDiscountValue1 { get; set; }
}
}
[DisplayColumn("DiscountAmount")]
public partial class PromotionDiscountValue
{
//public override string ToString()
//{
// return this.DiscountAmount.ToString();
//}
}
Also note the override of the ToString() this sorts out the issue of which column to display when you want something more complicated, like FistName + " " + LastName.
Metadata classesDynamic Data 4
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Marked as answer by harisri7862k4u on Nov 08, 2010 09:48 AM
Hi Harisri7862k4u, they will always be namespace issues, when the errors go away of the metadata still does not show then add a bugus property to the metadata class and if you get no error then the namespace is still wrong.
Dynamic Data 4
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
It is Old One But My question is How does override ToString knows this should work only for
PromotionCode1. What happens if you want to change more then one column?
harisri7862k...
Member
9 Points
54 Posts
Problem in displaying values for foreign keys in Dynamic data asp.net web app
Nov 04, 2010 02:14 PM|LINK
I have the following tables in my database which are as given under:
Table 1: CREATE TABLE [dbo].[PromotionDiscountValues]( [PromotionID] [int] IDENTITY(1,1) NOT NULL, [DiscountAmount] [int] NOT NULL, CONSTRAINT [PK_PromotionDiscountValues] PRIMARY KEY CLUSTERED ( [PromotionID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Table 2: CREATE TABLE [dbo].[PromotionCodes]( [PromotionCode] nchar NOT NULL, [CustomerPromotionDiscountID] [int] NOT NULL, [EmployeePromotionDiscountID] [int] NOT NULL, [IsActive] [bit] NOT NULL, CONSTRAINT [PK_PromotionCodes_1] PRIMARY KEY CLUSTERED ( [PromotionCode] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
There is a foreign key relationship between columns (1) PromotionID of table PromotionDiscountValues and CustomerPromotionDiscountID of table PromotionCodes and (2) PromotionID of table PromotionDiscountValues and EmployeePromotionDiscountID of table PromotionCodes
When i browse the PromotionCodes table in the dynamic data listing, the following columns get displayed:
PromotionCode
IsActive
PromotionDiscountValues (which basically contains the PromotionID values defined for CustomerPromotionDiscountID)
PromotionDiscountValues1 (which basically contains the PromotionID values defined for EmployeePromotionDiscountID)
My requirement is instead of getting the PromotionID values displayed for columns CustomerPromotionDiscountID & EmployeePromotionDiscountID, it should display the respective DiscountAmount. Which is not happenning for some reason.
I somehow found a workaround where if i define the datatype for columns CustomerPromotionDiscountID and EmployeePromotionDiscountID as nchar or nvarchar, it displays the DiscountAmount.
Wanted to know if there is any better way of doing this without changing the datatype for my columns? Or am I missing something?
Foreign key Column Dynamic Data .net c# 4.0
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Problem in displaying values for foreign keys in Dynamic data asp.net web app
Nov 04, 2010 03:07 PM|LINK
I think you will need some metadata like this:
[MetadataType(typeof(PromotionCodeMetadata))] public partial class PromotionCode { public override string ToString() { return this.PromotionCode1; } internal class PromotionCodeMetadata { [Display(Name = "Promotion Code")] public Object PromotionCode1 { get; set; } [Display(Name = "Customer Promotion Discount")] public Object PromotionDiscountValue { get; set; } [Display(Name = "Employee Promotion Discount")] public Object PromotionDiscountValue1 { get; set; } } } [DisplayColumn("DiscountAmount")] public partial class PromotionDiscountValue { //public override string ToString() //{ // return this.DiscountAmount.ToString(); //} }Also note the override of the ToString() this sorts out the issue of which column to display when you want something more complicated, like FistName + " " + LastName.
Metadata classes Dynamic Data 4
Always seeking an elegant solution.
harisri7862k...
Member
9 Points
54 Posts
Re: Problem in displaying values for foreign keys in Dynamic data asp.net web app
Nov 04, 2010 03:49 PM|LINK
Hey Steve,
Thanks for the reply. I am working on this. getting a few errors; let me see if I can resolve them.
Will get back to you soon :)
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Problem in displaying values for foreign keys in Dynamic data asp.net web app
Nov 04, 2010 05:46 PM|LINK
Hi Harisri7862k4u, they will always be namespace issues, when the errors go away of the metadata still does not show then add a bugus property to the metadata class and if you get no error then the namespace is still wrong.
Dynamic Data 4
Always seeking an elegant solution.
harisri7862k...
Member
9 Points
54 Posts
Re: Problem in displaying values for foreign keys in Dynamic data asp.net web app
Nov 08, 2010 09:48 AM|LINK
Hey Steve,
Your solution was perfect :)
Thanks...
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Problem in displaying values for foreign keys in Dynamic data asp.net web app
Nov 08, 2010 10:38 AM|LINK
Your welcome [:)]
Dynamic Data 4
Always seeking an elegant solution.
shilpak
Member
4 Points
8 Posts
Re: Problem in displaying values for foreign keys in Dynamic data asp.net web app
Nov 18, 2012 08:56 AM|LINK
It is Old One But My question is How does override ToString knows this should work only for PromotionCode1. What happens if you want to change more then one column?
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Problem in displaying values for foreign keys in Dynamic data asp.net web app
Nov 18, 2012 04:23 PM|LINK
Hi shilpak, not sure what you are asking?
Always seeking an elegant solution.