There is a bug in the generated code.

Everything to do with Ulrich Schumacher's .NET 2.0 architecture

Moderator: uschumacher

There is a bug in the generated code.

Postby swarner on Mon Jul 09, 2007 2:08 pm

I found a problem with the generated code for SQL Server. I am getting an SQL error when trying to save a null in a nullable non-string column. I modified your templates to set the database type for nullable columns.

I added the following to your template:
[code]
public static string GetSqlDbType(IColumn column)
{
// scan value types
switch (column.DataTypeName)
{
case \"bigint\": return \"BigInt\";
case \"binary\": return \"Binary\";
case \"bit\": return \"Bit\";
case \"char\": return \"Char\";
case \"datetime\": return \"DateTime\";
case \"decimal\": return \"Decimal\";
case \"float\": return \"Float\";
case \"image\": return \"Image\";
case \"int\": return \"Int\";
case \"money\": return \"Int\";
case \"nchar\": return \"NChar\";
case \"ntext\": return \"NText\";
case \"nvarchar\": return \"NVarChar\";
case \"real\": return \"Real\";
case \"smalldatetime\": return \"SmallDateTime\";
case \"smallint\": return \"SmallInt\";
case \"smallmoney\": return \"SmallMoney\";
case \"text\": return \"Text\";
case \"timestamp\": return \"Timestamp\";
case \"tinyint\": return \"TinyInt\";
case \"udt\": return \"Udt\";
case \"uniqueidentifier\": return \"UniqueIdentifier\";
case \"varbinary\": return \"VarBinary\";
case \"varchar\": return \"VarChar\";
case \"variant\": return \"Variant\";
case \"xml\": return \"Xml\";
default : return \"XX-Fix-Template-for-\" + column.DataTypeName;
}
}
[/code]

I changed renderParameter as follows:
[code]
private void renderParameter(IColumn column, string memberName)
{
if (column.IsNullable)
{
if (Utils.IsValueType(column))
Out.Write(\"if (!this.\" + memberName + \".HasValue)\");
else
{
if (column.LanguageType.Equals(\"string\"))
Out.Write(\"if (String.IsNullOrEmpty(this.\" + memberName + \"))\");
else
Out.Write(\"if (this.\" + memberName + \" == null)\");
}

Out.Write(\"{\");
Out.IncTab();

// SQL Server bug?
if (column.DataTypeName.Equals(\"image\"))
Out.Write(\"cmd.Parameters.AddWithValue(\\\"\" + this.getParameterName(column) + \"\\\", new byte[0]);\");
else
{
Out.Write(\"cmd.Parameters.AddWithValue(\\\"\" + this.getParameterName(column) + \"\\\", DBNull.Value);\");
Out.Write(\"cmd.Parameters[\\\"\" + this.getParameterName(column) + \"\\\"].SqlDbType = SqlDbType.\"
+ Utils.GetSqlDbType(column) + \";\");
}

Out.DecTab();
Out.Write(\"}\");
Out.Write(\"else\");
Out.IncTab();

if (Utils.IsValueType(column))
Out.Write(\"cmd.Parameters.AddWithValue(\\\"\" + this.getParameterName(column) + \"\\\", this.\" + memberName + \".Value);\");
else
Out.Write(\"cmd.Parameters.AddWithValue(\\\"\" + this.getParameterName(column) + \"\\\", this.\" + memberName + \");\");

Out.DecTab();
}
else
{
if (this.optimisticLocking && column.Equals(this.versionColumn))
{
}
else
Out.Write(\"cmd.Parameters.AddWithValue(\\\"\" + this.getParameterName(column) + \"\\\", this.\" + memberName + \");\");
}
}

[/code]
swarner
Lurker
 
Posts: 1
Joined: Mon Jul 09, 2007 1:53 pm

Return to usGDO - Generated Data Objects

Who is online

Users browsing this forum: No registered users and 1 guest

cron