locked
Problem in displaying values for foreign keys in Dynamic data asp.net web app RRS feed

  • Question

  • User-222463275 posted

    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:

    1. PromotionCode

    2. IsActive

    3. PromotionDiscountValues (which basically contains the PromotionID values defined for CustomerPromotionDiscountID)

    4. 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?

    Thursday, November 4, 2010 10:14 AM

Answers

  • User-330204900 posted

    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.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 11:07 AM

All replies

  • User-330204900 posted

    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.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 11:07 AM
  • User-222463275 posted

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


    Thursday, November 4, 2010 11:49 AM
  • User-330204900 posted

    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.

    Thursday, November 4, 2010 1:46 PM
  • User-222463275 posted

    Hey Steve,

    Your solution was perfect :)

    Thanks...

    Monday, November 8, 2010 5:48 AM
  • User-330204900 posted

    Your welcome [:)] 

    Monday, November 8, 2010 6:38 AM
  • User-253730406 posted

    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?

    Sunday, November 18, 2012 4:56 AM
  • User-330204900 posted

    Hi shilpak, not sure what you are asking?

    Sunday, November 18, 2012 12:23 PM