Welcome to the CodeSmith Community!

Modify Nettiers Templates to add a new class to the SQLClient Project

Support Forums

Feel free to ask any questions about CodeSmith here.

Modify Nettiers Templates to add a new class to the SQLClient Project

  • rated by 0 users
  • This post has 6 Replies |
  • 2 Followers
  • I am using Codesmith 5.1 with Nettiers templates. I need to add a static class that uses ADO.Net and pulls the server date from the database (its similar to called GetDate() via ExecuteScalar function in SQL Command object). This class should be autogenerated each time code generation is done and should be included in the DataAccess.SQLClient project of Nettiers. How do I accomplish that?

  • Hello,

    I would start by taking a look at the templates in the DataAccessLayer.SqlClient folder. The easiest approach would be to just add on your class to one of these templates but then you won't get a separate generated file for the class. To place the class into its own file you would need to add a template to that directory and then go into the TemplateLib folder and update the CreateTemplates.cs and FrameworkTemplates.cst. Finally you would need to update the .netTiers.cst file to render the new template.

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

  • I added a new template, modified the createtemplates.cst, frameworktemplates.cst  and nettiers.cst.

    When I run code generation, I get this exception.

     The given key was not present in the dictionary.

    An error of type "KeyNotFoundException" occured while attempting to execute this template.  To enable Just-In-Time debugging on this template set the "Debug" attribute to "True" in the "CodeTemplate" directive and add a "Debugger.Break()" statement in the template.

    The exact line of error is in the nettiers.cst file

    AddFileNode(commonNode, "NGServerDate.cs");
    Debug.WriteLine("Before setting value on template"); //this line executes fine
    this.GetTemplate("NGServerDate.cst").SetProperty("DALNameSpace", DALNameSpace); //this line is the error
    Debug.WriteLine("after setting value on template"); //this line does not execute
    this.RenderToFile("NGServerDate.cst", rootPathDAL + "\\NGServerDate.cs", true);

    This is my template file:


    #region Using directives
    using System.Drawing;
    using System.Data.SqlClient;
    #endregion

    namespace <%= DALNameSpace  %>
    {
        /// <summary>
        /// Returns server date
        /// </summary>
     public static class NGServerDate
     {
            /// <summary>
            /// Gets the name to be displayed for the collection node in the treeview.
            /// </summary>
            /// <param name="collectionName">Property name of the collection</param>
            /// <returns>Name to be displayed for the collection</returns>
          public static datetime GetServerDateDB()
            {
                SqlConnection conn = new SqlConnection("xxxxxxx removed for security");
                SqlCommand cmd = new SqlCommand("Select GetDate()", conn);
                conn.Open();
                DateTime CurrentDate = (DateTime)cmd.ExecuteScalar();
                conn.Close();
                return CurrentDate;

            }
        }
      }

    What am I doing wrong? Please help. Thanks in advance.

     

  • Sorry I did not paste the entire template file. Here it is:

     

    <%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="" ResponseEncoding="UTF-8"  NoWarn="0108,0618,1572,1573,1574,0162,2002"%>
    <%@ Property Name="DALNameSpace" Type="System.String" Category="Data" Description="Project Namespace." %>

    #region Using directives
    using System.Drawing;
    using System.Data.SqlClient;
    #endregion

    namespace <%= DALNameSpace  %>
    {
        /// <summary>
        /// Returns server date
        /// </summary>
     public static class NGServerDate
     {
            /// <summary>
            /// Gets the name to be displayed for the collection node in the treeview.
            /// </summary>
            /// <param name="collectionName">Property name of the collection</param>
            /// <returns>Name to be displayed for the collection</returns>
          public static datetime GetServerDateDB()
            {
                SqlConnection conn = new SqlConnection("");
                SqlCommand cmd = new SqlCommand("Select GetDate()", conn);
                conn.Open();
                DateTime CurrentDate = (DateTime)cmd.ExecuteScalar();
                conn.Close();
                return CurrentDate;

            }
        }
      }

  • Sorry - I think I did not update the templates correctly. Let me try a few things and retest. I'll post my findings shortly.

  • The code generation worked fine. Many thanks for your help. The new file was generated but not added to the project. How do I get the file added to the project automatically?

  • I realized later that I needed to add the new file to the Entities project.

    Here are the steps I followed:

    1. Created the new template and dropped it under '...TemplatesFolder\Entities'

    2. Add references to the new template\source file to the files:

    TemplatesFolder\UserLib\UserTemplates.cst

    TemplatesFolder\UserLib\CreateTemplates.cs

    3. Changed Nettiers.cst to add reference to the new template.

    4.  Added reference to the new source file in TemplatesFolder\VisualStudio\vsnet2005.project.cst

    Thanks for your help. Please close this thread.

     

     

Page 1 of 1 (7 items)