Question
I have the following code and it is giving related to curly braces and stuff.
<#@ template language="C#" debug="True" hostspecific="True" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.SqlClient" #>
namespace MyProject.Entities
{
public class
{
<#
string connectionString =
"Server=localhost;Database=GridViewGuy;Trusted_Connection=true";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
System.Data.DataTable schema = conn.GetSchema("TABLES");
foreach(System.Data.DataRow row in schema.Rows)
{
#>
public class <#= row["TABLE_NAME"].ToString() #>
{
}
}
}
}
Can anyone spot the problem?
Solution
The reason why it is not compiling is because you don't have a corresponding closing brace for the foreach block inside <# #>
tags. You need to make the following change:
foreach(System.Data.DataRow row in schema.Rows)
{
#>
public class <#= row["TABLE_NAME"].ToString()#>
{
}
<#
} //this was missing.
#>
Additionally, keep in mind that your code will create a class with no name followed by a list of nested classes with the names of your tables. Like this:
public class
{
public class Table1
{
}
public class Table2
{
}
//... and so on..
}
This may not be what you are trying to accomplish.
OTHER TIPS
In your first block, you start a code block
foreach(System.Data.DataRow row in schema.Rows)
{
#>
but never terminate it. Somewhere below you need this:
<# } #>
edit - it looks like it would be the closing curly brace just below the nested class definition
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow