Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 851835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:29:34+00:00 2026-05-15T07:29:34+00:00

I’d like to use some T4 templates to generate html files derived from a

  • 0

I’d like to use some T4 templates to generate html files derived from a sql server (in this case) database schema. For each table in the database, I would like to create 2 files:

  1. tableName_List.aspx – would contain the appropriate html to display in an asp.net GridView, with a grid column defined for each db table column

  2. tableName_Edit.aspx – would contain the appropriate html to display in an asp.net FormView, with a textbox (for simplicity’s sake, for now) for each db table column

So, if I have 5 tables in the database, I would get 10 files output. I’ve been googling this and found related articles, but most of them don’t seem to address this scenario. I’ve also seen references to using subsonic for this, but I’d rather not introduce yet another technology into the mix.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:29:35+00:00Added an answer on May 15, 2026 at 7:29 am

    The code for the T4 template below will give you a relatively good start.

    You will need to add references to the appropriate versions of the Microsoft.SqlSserver Smo DLLs to the project.

    The following items need to be replaced in this code with the appropriate values for your environment:

    SERVERNAMEGOESHERE
    DATABASENAMEGOESHERE
    PROJECTNAMESPACEGOESHERE

    <#@ template language="C#v3.5" hostspecific="true" #>
    <#@ assembly name="System.Data" #>
    <#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
    <#@ assembly name="Microsoft.SqlServer.Smo" #>
    <#@ import namespace="System.Data.SqlClient" #>
    <#@ import namespace="Microsoft.SqlServer.Management.Common" #>
    <#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
    <#
        string connectionString = @"Server=SERVERNAMEGOESHERE;Trusted_Connection=True;";
        string databaseName = "DATABASENAMEGOESHERE";
        string projectNamespace = "PROJECTNAMESPACEGOESHERE";
        string relativeOutputFilePath = null;
    
        SqlConnection oneSqlConnection = new SqlConnection(connectionString);
        ServerConnection oneServerConnection = new ServerConnection(oneSqlConnection);
        Server oneServer = new Server(oneServerConnection);
        Database oneDatabase = oneServer.Databases[databaseName];
        foreach (Table oneTable in oneDatabase.Tables)
        {
            if (!oneTable.Name.Equals("sysdiagrams"))
            {
    #>
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="<#= oneTable.Name #>_List.aspx.cs" Inherits="<#= projectNamespace #>.<#= oneTable.Name #>_List" %>
    <asp:DataGrid ID="<#= oneTable.Name #>DataGrid" runat="server" AutoGenerateColumns="false">
        <Columns>
    <#
                foreach (Column oneColumn in oneTable.Columns)
                {
    #>
            <asp:BoundColumn DataField="<#= oneColumn.Name #>" HeaderText="<#= oneColumn.Name #>"></asp:BoundColumn>
    <#
                }
    #>
        </Columns>
    </asp:DataGrid>
    <#
                relativeOutputFilePath = @"\Output\" + oneTable.Name + "_List.aspx";
                TemplateHelper.WriteTemplateOutputToFile(relativeOutputFilePath, Host, GenerationEnvironment);
                GenerationEnvironment = new System.Text.StringBuilder();
    #>
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="<#= oneTable.Name #>_Edit.aspx.cs" Inherits="<#= projectNamespace #>.<#= oneTable.Name #>_Edit" %>
    <#
                foreach (Column oneColumn in oneTable.Columns)
                {
    #>
        <asp:TextBox ID="<#= oneColumn.Name #>TextBox" runat="server" />
    <#
                }
                relativeOutputFilePath = @"\Output\" + oneTable.Name + "_Edit.aspx";
                TemplateHelper.WriteTemplateOutputToFile(relativeOutputFilePath, Host, GenerationEnvironment);
                GenerationEnvironment = new System.Text.StringBuilder();
            }
        }
    #>
    <#+
    public class TemplateHelper
    {
        public static void WriteTemplateOutputToFile(
            string relativeOutputFilePath,
            Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost Host,
            System.Text.StringBuilder GenerationEnvironment)
        {
            string outputPath = System.IO.Path.GetDirectoryName(Host.TemplateFile);
            string outputFilePath = outputPath + relativeOutputFilePath;
            System.IO.File.WriteAllText(outputFilePath, GenerationEnvironment.ToString());
        }
    }
    #>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.