Category Archives: Asp.Net

The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %>)

Because I have pages with the markup <%= someVariable%> like

<script type="text/javascript">
            
  function ConfirmCallBack(arg)
  {
    if (arg)
     <%= this._nextPostBack%>
  }
</script>

I get the following error: "The Controls collection cannot be modified because the control contains code blocks (i.e. <% … %><% … %>)."

To correct it I had to change from

<%= this._nextPostBack%>

to

<%# this._nextPostBack%>

and in the PageLoad event add the call to Page’s DataBind method

protected void Page_Load(object sender, EventArgs e)
{
  ...
  Page.DataBind();
}

Codesmith, netTiers and reserved words

Started today using Codesmith tools today after a year or so since the last time that I used this great generation template based tool.

I had to generate all the layers of an existent portal and I decided to use the netTiers templates to accomplish this task.

And here are the problems that I had to solve:

  • In the Entities templates:

I have a table named Entity that conflicts with the auxiliary class of the netTiers templates. It seems that the templates already generate a class called Entity and because there is a table called Entity there was a conflict.

I solved that using the alias text file feature of the templates that allows me to define a alias for any table that I have. So I just inserted a new line in my alias text file like this:

Entity:MyEntity

With this alias, netTiers generated a class for my table named MyEntity that didn't conflict with his own class Entity.

  • In the Data templates:

I had a table with two foreign keys. But the problem was that the foreign keys were on the same column of the table but referencing two different tables.

The problem was that netTiers generated two methods with the same name and signature GetByColumnForeingKey.

I had to change the schema and use two columns, each one referencing a different column in a different table.

  • In the Domain templates:

I have views in my database and I had compilation problems with the generated classes for these views.

For each view there are two generated files/classes:

MyView.cs and MyViewServiceBase.generated.cs

Where MyView inherits from MyViewBase. Because MyViewBase doesn't exist there was a compilation problem. I just had to strip the "Service" word from the netTiers templates so the file/class generated was MyViewBase.cs instead of MyViewServiceBase.cs

Another problem was with a table with a "Type" column. This gives this error:

Error    1    An object reference is required for the non-static field, method, or property 'TableBase.Type.get'    Domain\TableBase.generated.cs    1394    57    Domain

The generated code was

DeepLoad(entity, false, DeepLoadType.ExcludeChildren, Type.EmptyTypes);

I had to change the template so to genreate

DeepLoad(entity, false, DeepLoadType.ExcludeChildren, System.Type.EmptyTypes);

  • In the Data.SqlClient templates:

There was an ambiguous table named Parameter. netTiers already creates a class named Parameter so I just had to add another line in my alias text file

Parameter:MyParameter

  • And finally in the web templates:

There was an ambiguous table named Page. I just had to add another line in my alias text file.

Page:MyPage

Add a custom section to web.config

If you have created a custom section like this, and if you want to add it programmatically to web.config then you have to:

  1. Use the WebConfigurationManager class and open the web

Configuration config = WebConfigurationManager.OpenWebConfiguration(path, site);

From MSDN

path

The virtual path to the configuration file.

site

The name of the application Web site, as displayed in IIS configuration.

 

  1. Create a ConfigurationSectionGroup and add it to the Configuration object

ConfigurationSectionGroup sectionGroup = new ConfigurationSectionGroup();

config.SectionGroups.Add(“GROUP_NAME”, sectionGroup);

 

  1. Create a new instance of your section (MyCustomSection) and add it to the section group

MyCustomSection section = new MyCustomSection ();

sectionGroup.Sections.Add(“SECTION_NAME”, section);

 

  1. Fill your custom section properties

section.Property1 = “0123456789”;

section.Property2 = “Lorem ipsum dolor sit amet”;

 

  1. And save the configuration

config.Save();

If you take a look at your web.config you will see that it was modified programmatically as you specified. I use this technique in the setup process of sites. Cool!

Different web.configs

In the project that I'm currently working I had a problem because my connection strings are different from the other developers: I want to work locally in my SQL Express so the connections strings are different and since we are developing with Visual Source Safe 2005 as the version control system it is not possible to change the file where the connections strings are stored.

The easiest way was not to keep the web.config file under source control but of course that wasn't the desired way because we have lots of settings that we want to keep under source control.

After googling a while I found that the appSettings section in the web.config file of an ASP.NET web application has an attribute – file – that can point to an external file where we can store additional settings.

So my initial web.config file is:

<appSettings

<add
key="DB_1_ConnectionStringName"
value="DB_1" />

<add
key="DB_2_ConnectionStringName"
value="DB_2" />

</appSettings>

 

<connectionStrings>

<add
name="DB_1"
connectionString="server=db_server;database=db_1; … "
providerName="System.Data.SqlClient" />

<add
name="DB_2"
connectionString="server=db_server;database=DB_2; … "
providerName="System.Data.SqlClient" />

</connectionStrings>

Of course, when I want to work locally in my SQL Express instance this doesn't work. So I changed the web.config file like this:

<appSettings file="custom.config"

<add
key="DB_1_ConnectionStringName"
value="DB_1" />

<add
key="DB_2_ConnectionStringName"
value="DB_2" />

</appSettings>

 

<connectionStrings>

<add
name="DB_1"
connectionString="server=db_server;database=db_1; … "
providerName="System.Data.SqlClient" />

<add
name="DB_2"
connectionString="server=db_server;database=DB_2; … "
providerName="System.Data.SqlClient" />

 

<add
name="DB_1_LOCAL"
connectionString="server=.\SQLEXPRESS;database=db_1; … "
providerName="System.Data.SqlClient" />

<add
name="DB_2_LOCAL "
connectionString="server=db_.\SQLEXPRESS;database=DB_2; … "
providerName="System.Data.SqlClient" />

 

</connectionStrings>

And my custom.config file is:

<appSettings

<add
key="DB_1_ConnectionStringName"
value="DB_1_LOCAL" />

<add
key="DB_2_ConnectionStringName"
value="DB_2_LOCAL" />

</appSettings>

 

What happens is that the settings in the custom.config file override the settings in the web.config file.

The other developers don't need to have the custom.config files because "the runtime ignores the attribute, if the specified file cannot be found" [msdn].

So I have only the custom.config file that it isn't under source control but that's no problem because I only redefine the name of the connection strings. The actual connection strings are still stored in the web.config file and under source control.

 

Setting the original file name when using Response.BinaryWrite()

Imagine you have file saved in a database. Then you have an aspx page that gets that page and sends it back to the user.

If you just simply use Response.BinaryWrite you get the browser's download dialog box and the file's name will be the aspx page's name. If you want to force the original file name you have to add an header to the response like this:

Response.AddHeader("Content-Disposition", "inline;filename=ORIGINAL_FILENAME");