CodeSmith Template for NameValueList

CodeSmith Template for NameValueList

Old forum URL: forums.lhotka.net/forums/t/1661.aspx


wjcomeaux posted on Wednesday, November 01, 2006

Does anyone have an example of XML that can create a NameValueList?

I am trying to generate a NameValue list that uses the ID field as well as a combination of fields for the value. So a User table would generate the properties ID, FirstName and LastName.

I will let the users Override the GetList method to provide the name in whatever format they want.

Here's what I have so far but it's throwing an Index out of range exception when I try to generate the code from it.

<Object Access="public" Type="namevaluelist" Name="NameValueListResource">

- <Properties>
- <Property Name="_ID" Description="" Type="string" DataType="AnsiString" NativeType="varchar" SystemType="System.String" IsPrimaryKey="true" IsIdentity="true">
- <ValidationRules>
  <Required />
  <StringMaxLength>10</StringMaxLength>
  </ValidationRules>
  </Property>
- <Property Name="_FirstName" Description="" Type="string" DataType="AnsiString" NativeType="varchar" SystemType="System.String">
- <ValidationRules>
  <Required />
  <StringMaxLength>50</StringMaxLength>
  </ValidationRules>
  </Property>
- <Property Name="_LastName" Description="" Type="string" DataType="AnsiString" NativeType="varchar" SystemType="System.String">
- <ValidationRules>
  <Required />
  <StringMaxLength>50</StringMaxLength>
  </ValidationRules>
  </Property>
  </Properties>
  </Object>
 
Thanks,
Will

wjcomeaux replied on Wednesday, November 01, 2006

I've gotten somewhere. I removed the ValidationRules and this allowed the class to generate but it

now emits like this

this.Add(new NameValuePair(dr.GetString(""), dr.GetString("")));

As you can see, it doesn't know what properties to get from the datareader, so now it's to figure out what attributes to emit in my XML Metadata.

Will

 

<Edit>It appears that the template for a NameValue list simply grabs the Primary Key as the value and the first non-primary key property as the name field. This is a bit of a limitation when you want to display a set of users in LastName, FirstName order. Or am I missing something here?</Edit>

JoeFallon1 replied on Thursday, November 02, 2006

You can modify the templates to suit your needs.

I added properties like these:

<%@ Property Name="KeyName" Type="System.String" Optional="False" Category="Options" Description="The column name for the Key." %>

<%@ Property Name="KeyType" Type="System.String" Optional="False" Default="String" Category="Options" Description="The Type of the Key." %>

<%@ Property Name="ValueName" Type="System.String" Optional="False" Category="Options" Description="The column name for the Value." %>

<%@ Property Name="ValueType" Type="System.String" Optional="False" Default="String" Category="Options" Description="The Type of the Value." %>

Now I can specify the 2 field names and types when I generate.

Joe

 

 

Copyright (c) Marimer LLC