Accessing a foreign key's table.

Last post 05-15-2008 8:55 PM by marcind. 11 replies.

Sort Posts:

  • Accessing a foreign key's table.

    05-14-2008, 11:53 AM
    • Loading...
    • TheDuke
    • Joined on 05-14-2008, 3:28 PM
    • Posts 11

    Hi

    I'm new to asp.net and thanks to time restrictions I've had to jump in at the deep end, any help you can provide is great.
    I'm developing an Asset Register for one of our clients, a rough over view of the database is as follows.

    Assets

    • asset_id
    • asset_tag
    • model_id
    • asset_serial
    • ...

    Models

    • model_id
    • manufacturer_id
    • model_name

    Manufacturers

    • manufacturer_id
    • manufacturer_name

    Using DynamicData I've built a custom page to display the Assets table and I'd like the gridview to display the manufacturer_name field. I've tried using "Model.Manufacturer" and "Model.Manufacturer.manufacturer_name" for the datafield property with no luck, it doesn't recognise the collumn exists.  Is this something that DynamicData has support for, if not can anyone point me in the right direction.

    Many Thanks
    Ben

  • Re: Accessing a foreign key's table.

    05-14-2008, 1:32 PM
    • Loading...
    • marcind
    • Joined on 09-06-2007, 1:11 AM
    • Redmond, WA
    • Posts 187

    Hi Ben,

    you could do this by adding a custom property to your Asset class:

    public partial class Asset {

    public string ManufacturerName { get { return this.Manufacturer.manufacturer_name; } }

    }

    Marcin Dobosz
    SDE, ASP.NET Team, Microsoft
    Get the Dynamic Data Futures 7/2 update.
  • Re: Accessing a foreign key's table.

    05-14-2008, 1:39 PM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 5:11 PM
    • Newton-le-Willows, UK
    • Posts 319

    you need to point at the oject name i.e. Manufacturers not manufacturer_id this will cause DynamicData to use the foreign key table if the field name you wish to show in the DropdownList then you will need to attribute it like so:

    [MetadatatypeAttribute(ManufacturersMetaData)]
    public class Manufacturers
    {
    }

    public class ManufacturersMetaData
    {
      [DisplayColumnAttribute("manufacturer_name")]
      public object manufacturer_name {get; set;}
    }

    See Dynamic Data Attributes on MaĆ­ra Wenzel's Blog you will find a list of most of the DynamicData Attributes the on I think you need is DisplayColumnAttribute which "Specifies which column to display (in filters or in foreign key links). By default, Dynamic Data uses the first column of type string that it finds."

    Hope this helps

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
    Filed under: ,
  • Re: Accessing a foreign key's table.

    05-14-2008, 1:43 PM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 5:11 PM
    • Newton-le-Willows, UK
    • Posts 319

    Hi Marcin your Signiture still shows Check out the latest 4/23 Dynamic Data Preview update. Big Smile you should be on 5/12 Wink

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
  • Re: Accessing a foreign key's table.

    05-14-2008, 1:56 PM
    Answer
    • Loading...
    • marcind
    • Joined on 09-06-2007, 1:11 AM
    • Redmond, WA
    • Posts 187

    Steve: thanks, I've just gotten back from a 2 week vacation and not everything has been updated. should be fixed now.

    Back to Ben's issue: What I think he is trying to do - and correct me if I'm wrong - is to display an additional column in the Asset's grid view that would display the Asset's Model's Manufacturer's name. This can only be done by having an extra property on the Asset class to represent the extra column. However, I did make a small mistake in my initial response, and it should be:

    public partial class Asset {
      public string ManufacturerName { 
        get { 
          return this.Model.Manufacturer.manufacturer_name;
        }
      }
    }
    
     
    Marcin Dobosz
    SDE, ASP.NET Team, Microsoft
    Get the Dynamic Data Futures 7/2 update.
  • Re: Accessing a foreign key's table.

    05-14-2008, 2:11 PM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 5:11 PM
    • Newton-le-Willows, UK
    • Posts 319

    I've ha a re-read of his original question and yes I think your answer is what he is looking for. His problem sounded like one I'd had and just jumped to the wrong conclusion.

    Oh and your signature correct now Big Smile

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
    Filed under:
  • Re: Accessing a foreign key's table.

    05-14-2008, 2:15 PM
    • Loading...
    • TheDuke
    • Joined on 05-14-2008, 3:28 PM
    • Posts 11

    Thanks for the quick replies, I will give this a try.  On a connected issue, I would like to be able to filter a models DropDownList whilst in edit mode using the manufacturers field.  I think the best way to do this would be to build a user control with linq-to-sql datasources and the necessary methods to filter a models DropDownList, what I'm not sure on is how to then get the value of the models dropDownList (i.e. model_id) back to the GridView ready for the update.  Do I need to get the user control to set a property in the assets class?

    And lastly on a side note, i remember reading that there were plans to provide an OrderBy attribute to the Dynamic Filters, does anyone know if this has been or can be implented in the new code drop?

    Many thanks.

  • Re: Accessing a foreign key's table.

    05-14-2008, 2:23 PM
    Answer
    • Loading...
    • scothu
    • Joined on 12-10-2007, 6:54 AM
    • Redmond, WA
    • Posts 106
    • AspNetTeam

    You can apply a DisplayColumnAttribute for foreign key tables. This attribute takes SortColumn and SortDecending properties to determine how things are sorted. I know these will be used when editing a row, not sure if these affect the filters as well. Marcin?

    Scott Hunter
    ASP.NET
  • Re: Accessing a foreign key's table.

    05-14-2008, 4:44 PM
    Answer
    • Loading...
    • marcind
    • Joined on 09-06-2007, 1:11 AM
    • Redmond, WA
    • Posts 187

    Yes, these should work for filters as well.

    Regarding the cascading dropdown issue it should be doable. I had done this before for filters (something like a car shopping site: first choose make, then model, which filters the list of available cars) but the sample was rough and I do not have it available right now. However, it is a bit tricky as I recall I had to write my own linq queries, and writing something that would be generic enough for all situations is nontrivial. I'll try to get it working again and I will let you know.

    Marcin Dobosz
    SDE, ASP.NET Team, Microsoft
    Get the Dynamic Data Futures 7/2 update.
  • Re: Accessing a foreign key's table.

    05-14-2008, 6:13 PM
    • Loading...
    • TheDuke
    • Joined on 05-14-2008, 3:28 PM
    • Posts 11

    EDITED: 14:09 15th May

    Got it working, thanks very much Marcin, would appreciate help with the cascading drop downs if you get the time.

  • Re: Accessing a foreign key's table.

    05-15-2008, 8:01 PM
    • Loading...
    • TheDuke
    • Joined on 05-14-2008, 3:28 PM
    • Posts 11

    Marcin

    I think i've figuured out the cascading drop downs, I'd appreciate input on any improvements you can think of. I used a custom Field Template with 2 linq to sql data sources, source for the control is below.

    ASCX file

    <asp:LinqDataSource ID="lqsManufacturers" runat="server"
    ContextTypeName="AssetRegisterDataContext"
    TableName="Manufacturers" OrderBy="manufacturer_name">
    </asp:LinqDataSource>

    <asp:LinqDataSource ID="lqsModels" runat="server"
    ContextTypeName="AssetRegisterDataContext" TableName="Models"
    Where="manufacturer_id == @manufacturer_id">
    <WhereParameters>
    <asp:ControlParameter ControlID="manufacturersDropDown" Name="manufacturer_id"
    PropertyName="SelectedValue" Type="Int32" />
    </WhereParameters>
    </asp:LinqDataSource>

    <asp:Label runat="server" Text="Manufacturer" />
    <asp:DropDownList ID="manufacturersDropDown" runat="server"
    DataSourceID="lqsManufacturers" DataTextField="manufacturer_name"
    DataValueField="manufacturer_id" AutoPostBack="true" />
    <br />
    <asp:Label runat="server" Text="Model" />
    <asp:DropDownList ID="modelsDropDown" runat="server"
    DataSourceID="lqsModels" DataTextField="model_name"
    DataValueField="model_id" />

     

    Code behind .cs

    1    public partial class DynamicData_FieldTemplates_ManufacturerModel : System.Web.DynamicData.FieldTemplateUserControl
    2    {
    3    	// Set up the original values.
    4    	protected override void OnDataBinding(EventArgs e)
    5    	{
    6    		base.OnDataBinding(e);
    7    		object val = FieldValue;
    8    
    9    		// Get the manufactuer_id for the current model
    10   		AssetRegisterDataContext db = new AssetRegisterDataContext();
    11   		var models = from mo in db.Models
    12   					 where mo.model_id == (int)val
    13   					 select mo.Manufacturer.manufacturer_id;
    14   
    15   		// Set the Manufacturer drop down to the value from the model record.
    16   		manufacturersDropDown.SelectedValue = models.Single().ToString();
    17   		// Now set the Model drop down to the value from the database.
    18   		modelsDropDown.SelectedValue = val.ToString();
    19   	}
    20   	protected override void ExtractValues(IOrderedDictionary dictionary)
    21   	{
    22   		dictionary[Column.Name] = modelsDropDown.SelectedValue;
    23   	}
    24   	public override Control DataControl
    25   	{
    26   		get
    27   		{
    28   			return modelsDropDown;
    29   		}
    30   	}
    31   }
    
     
  • Re: Accessing a foreign key's table.

    05-15-2008, 8:55 PM
    • Loading...
    • marcind
    • Joined on 09-06-2007, 1:11 AM
    • Redmond, WA
    • Posts 187

    Ben,

    that looks pretty good. As I mentioned earlier, the one downside is that it is specific to that particular fk/parent relationship. Writing something generic would be more complicated, but I hope to have a sample working soon.

    Marcin Dobosz
    SDE, ASP.NET Team, Microsoft
    Get the Dynamic Data Futures 7/2 update.
Page 1 of 1 (12 items)