How to format code blogging with word 2007

Today I was trying to blog with word 2007 and I found some difficulties with code. First of all I suggest you to install a Visual Studio Plugin that enable a “copy source as HTML” option that permits you to copy the code as HTML and this makes the life easier. The problem is that simply pasting the code into word will produce some strange results

    3 ”’ <summary>

    4 ”’ classe base per la definizione di proprietà comuni

    5 ”’ </summary>

    6 ”’ <remarks></remarks>

    7 <TypeConverter(GetType(ExpandableObjectConverter))> _

    8 Public
Class BaseDefinition

    9 
   17    <NotifyParentProperty(True)> _

   18    Public
Property CssClass() As
String

   19       Get

   20          Return mCssClass

   21       End
Get

   22       Set(ByVal value As
String)

   23          mCssClass = value

   24       End
Set

   25    End
Property

The code above has two problems, the first is that spacing between lines is too big, and then in HTML there are some <BR/> tags that completely makes code unreadable. After some investigation I found that it is possible to force word to generate better HTML making two substitutions. The first is substitute ^p character with ^l, the ^l character does not generate <P> tags but a simple <BR/> and this makes lines spacing smaller.

    3 ”’ <summary>
    4 ”’ classe base per la definizione di proprietà comuni
    5 ”’ </summary>
    6 ”’ <remarks></remarks>
    7 <TypeConverter(GetType(ExpandableObjectConverter))> _
    8 Public
Class BaseDefinition
    9 
   17    <NotifyParentProperty(True)> _
   18    Public
Property CssClass() As
String
   19       Get
   20          Return mCssClass
   21       End
Get
   22       Set(ByVal value As
String)
   23          mCssClass = value
   24       End
Set
   25    End
Property

Now you need to substitute also the space character with ^s (unifying space) this (do not ask me why) makes word not to generate the weird <BR /> tag.

    3 ”’ <summary>
    4 ”’ classe base per la definizione di proprietà comuni
    5 ”’ </summary>
    6 ”’ <remarks></remarks>
    7 <TypeConverter(GetType(ExpandableObjectConverter))> _
    8 Public Class BaseDefinition
    9 
   10    ”’ <summary>
   11    ”’ è necessario questo attributo per far siche il designer si accorga del 
   12    ”’ cambiamento della proprietà e fare in modo che venga serializzata
   13    ”’ </summary>
   14    ”’ <value></value>
   15    ”’ <returns></returns>
   16    ”’ <remarks></remarks>
   17    <NotifyParentProperty(True)> _
   18    Public Property CssClass() As String
   19       Get
   20          Return mCssClass
   21       End Get
   22       Set(ByVal value As String)
   23          mCssClass = value
   24       End Set
   25    End Property

Now the code looks really better but a last problem remains, the code with line numbering cannot be copied and pasted from the blog into visual studio. Even if you remove the numbering, word substitutes all occurrence of double ‘ chars with ” character, so the resulting VB.Code is not valid. At this time I really do not understand how to tell word to leave the ‘ code unchanged.

Alk.

Please follow and like us:

Error synchronizing database with red gate SQL compare

Today I was synchronizing two database with red gate sql compare, newer database has 6 more additional view, but the sync script failed with some errors. One of the view is called MatterKnowledges and the script return error “matterknowledges” already exists, even if the destination database has no such view. The problem originates from the fact that the view (SQL2000) was create with a different name and then renamed to MatterKnowledges. The solution to the problem was dropping and then recreate the view on the original database, now all works ok.

Alk.

Please follow and like us:

Reassociated object has dirty collection

Today I Hitted this exception working with NHibernate, after consulting some posts on the net I discovered that this exception is raised when a detached object is reattached to a session with lock and the object has a one-to-many relation that was changed. The strange thing is that the same code works perfectly on a test web site and throw this error in production site. Moreover I’m sure that the whole object graph is unchanged and that the collections is not changed

After a couple of minutes I realized that a control store a detatched object in asp.net session and reattach with session.lock…the problem is that in production code session state is managed in sql server, so the object graph is serialized, then deserialized at the next request and reattached with session.lock(). It seems that the operations of serialization and deserialization make nhibernate think that the collection of objects is somewhat changed.

Alk.

Please follow and like us:

Nhibernate bug?

I replicated a bug in nhibernate. I have a simple mapping for a simple class 

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Ca1" assembly="Ca1" default-lazy="false" default-access="property"> <class name="Ca1.Cliente" table="Clienti" > <id name="Id" column="Id" type="int" unsaved-value="0"> <generator class="identity" /> </id> <property name="Nome" type="String" column="Nome" insert="true" update="true" /> <property name="Indirizzo" type="String" column="Indirizzo" not-null="false" insert="true" update="true" /> <property name="LocalizedData" type="string" update="false" formula='(SELECT ClientiLoc.loctext FROM ClientiLoc WHERE ClientiLoc.clieid = Id and ClientiLoc.lang = :CultureFilter.LangId)'/> </class> </hibernate-mapping>

The only particular issue is that LocalizedData Property is a formula and takes data from another table that contains localized data, the current locale is specified as a filter. The problem originates from the following simple code

using (NHibernate.ISession session = Helpers.CreateSession()) { using (NHibernate.ITransaction tran = session.BeginTransaction()) { IFilter flt = session.EnableFilter("CultureFilter"); flt.SetParameter("LangId", "it"); ICriteria criteria = session.CreateCriteria(typeof(Ca1.Cliente)); //criteria.Add(NHibernate.Expression.Expression.Like("Nome", "Gian%")); criteria.Add(NHibernate.Expression.Expression.Eq("LocalizedData", "loc_it")); criteria.Add(NHibernate.Expression.Expression.Like("Nome", "Gian%")); IList<Ca1.Cliente> result = criteria.List<Cliente>(); Console.WriteLine("Retrieved {0} objects", result.Count); } }

This code simply creates a criteria filter to select a cliente entity that has both the name like ‘Gian%’ and the localized italian version equal to “loc_it”. All works well and the query generated intercepted by the sql profiler is.

exec sp_executesql N'SELECT this_.Id as Id0_0_, this_.Nome as Nome0_0_, this_.Indirizzo as Indirizzo0_0_, (SELECT ClientiLoc.loctext FROM ClientiLoc WHERE ClientiLoc.clieid = this_.Id and ClientiLoc.lang = @p0) as formula0_0_ FROM Clienti this_ WHERE (SELECT ClientiLoc.loctext FROM ClientiLoc WHERE ClientiLoc.clieid = this_.Id and ClientiLoc.lang = @p1) = @p2 and this_.Nome like @p3', N'@p0 nvarchar(2),@p1 nvarchar(2),@p2 nvarchar(6),@p3 nvarchar(5)',@p0=N'it',@p1=N'it',@p2=N'loc_it',@p3=N'Gian%'

The sql is quite messy, the query is OK but has a strange duplicated parameter ‘it’ even if I put only criteria for localized query. The real problem originates if the two criteria are inverted in order.

criteria.Add(NHibernate.Expression.Expression.Like("Nome", "Gian%")); criteria.Add(NHibernate.Expression.Expression.Eq("LocalizedData", "loc_it"));

This simple change breaks the query:

exec sp_executesql N'SELECT this_.Id as Id0_0_, this_.Nome as Nome0_0_, this_.Indirizzo as Indirizzo0_0_, (SELECT ClientiLoc.loctext FROM ClientiLoc WHERE ClientiLoc.clieid = this_.Id and ClientiLoc.lang = @p0) as formula0_0_ FROM Clienti this_ WHERE this_.Nome like @p1 and (SELECT ClientiLoc.loctext FROM ClientiLoc WHERE ClientiLoc.clieid = this_.Id and ClientiLoc.lang = @p2) = @p3', N'@p0 nvarchar(2),@p1 nvarchar(2),@p2 nvarchar(5),@p3 nvarchar(6)',@p0=N'it',@p1=N'it',@p2=N'Gian%',@p3=N'loc_it'

Even is sql is quite messy you can notice that the parameters order is wrong. The second criteria ClientiLoc.lang = @p2 is wrong because @p2 parameter does not contain the ‘it’ value but the ‘Gian%’ value.

This problem happens every time I have a formula property that use a filter. To work correctly, every criteria on filter property must be the first criteria in the criteria collection.

Alk.

Please follow and like us: