PC Review


Reply
Thread Tools Rate Thread

ADO.NET 2.0 saving single space to SQL?

 
 
=?Utf-8?B?RG9uYWxkIEpvcHBh?=
Guest
Posts: n/a
 
      7th Aug 2007
We're converting from framework 1.1 to 2.0 and something interesting has
popped up. Fields that are being inserted into the database as a zero length
string in 1.1 contain a single space when inserted with 2.0 compiled code.

The code specifies a stored proc to run and set up the parameter collection.
I've verified in the debugger that in the parameter collection's item array
entries that I care about contain a zero length string (this is true for both
1.1 and 2.0).

SQL is running on a W2K3 box and the instance is SQL2K. I've checked the
database compatibility level and it is set to 80.

When I capture the insert command via SQL profiler from 1.1 the parameters
show up as a zero length string (''), but when it comes from 2.0 code the
same parameters contain a single space (' ').

Anyone have any idea what I'm missing?
 
Reply With Quote
 
 
 
 
William Vaughn
Guest
Posts: n/a
 
      7th Aug 2007
Let's see the code used to invoke the SP--especially the code that populates
the Parameters collection. It would be helpful to know what values are
passed to the problem parameter.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Donald Joppa" <(E-Mail Removed)> wrote in message
news:89F148CA-21E6-4FD3-BD97-(E-Mail Removed)...
> We're converting from framework 1.1 to 2.0 and something interesting has
> popped up. Fields that are being inserted into the database as a zero
> length
> string in 1.1 contain a single space when inserted with 2.0 compiled code.
>
> The code specifies a stored proc to run and set up the parameter
> collection.
> I've verified in the debugger that in the parameter collection's item
> array
> entries that I care about contain a zero length string (this is true for
> both
> 1.1 and 2.0).
>
> SQL is running on a W2K3 box and the instance is SQL2K. I've checked the
> database compatibility level and it is set to 80.
>
> When I capture the insert command via SQL profiler from 1.1 the parameters
> show up as a zero length string (''), but when it comes from 2.0 code the
> same parameters contain a single space (' ').
>
> Anyone have any idea what I'm missing?


 
Reply With Quote
 
=?Utf-8?B?RG9uYWxkIEpvcHBh?=
Guest
Posts: n/a
 
      7th Aug 2007
Here's the method that is called to create the parameter object

public SqlParameter AddParam(string paramName, SqlDbType paramType, object
paramValue)
{
SqlParameter param = mCmd.Parameters.Add(paramName, paramType);
param.Value = paramValue;
return param;
}

Here's an edited sample of how it is called:

db.AddParam("@EmailAddress", SqlDbType.Char, EmailInfo.EmailAddr.Value);

The value in the problem case is a string with a zero length.

The mCmd.CommandType is CommandType.StoredProcedure and the code is calling
mCmd.ExecuteNonQuery().

Note that I've looked at the mCmd.Parameters in the debugger and burrowed
down to the itemarray with 1.1 code and 2.0 code. In both cases I'm seeing
values that are empty strings, but when picked up by sql profiler the 2.0
call has a single space in the parameter...

"William Vaughn" wrote:

> Let's see the code used to invoke the SP--especially the code that populates
> the Parameters collection. It would be helpful to know what values are
> passed to the problem parameter.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant, Dad, Grandpa
> Microsoft MVP
> INETA Speaker
> www.betav.com
> www.betav.com/blog/billva
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no rights.
> __________________________________
> Visit www.hitchhikerguides.net to get more information on my latest book:
> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> -----------------------------------------------------------------------------------------------------------------------
>
> "Donald Joppa" <(E-Mail Removed)> wrote in message
> news:89F148CA-21E6-4FD3-BD97-(E-Mail Removed)...
> > We're converting from framework 1.1 to 2.0 and something interesting has
> > popped up. Fields that are being inserted into the database as a zero
> > length
> > string in 1.1 contain a single space when inserted with 2.0 compiled code.
> >
> > The code specifies a stored proc to run and set up the parameter
> > collection.
> > I've verified in the debugger that in the parameter collection's item
> > array
> > entries that I care about contain a zero length string (this is true for
> > both
> > 1.1 and 2.0).
> >
> > SQL is running on a W2K3 box and the instance is SQL2K. I've checked the
> > database compatibility level and it is set to 80.
> >
> > When I capture the insert command via SQL profiler from 1.1 the parameters
> > show up as a zero length string (''), but when it comes from 2.0 code the
> > same parameters contain a single space (' ').
> >
> > Anyone have any idea what I'm missing?

>
>

 
Reply With Quote
 
=?Utf-8?B?RG9uYWxkIEpvcHBh?=
Guest
Posts: n/a
 
      7th Aug 2007
I've created a much simpler test that demonstrates this on my machine when
compiled with the .NET 2.0 framework.

Here's the C# code:

public static void InsertRecord()
{
SqlConnection conn = new
SqlConnection("Server=.;Database=SqlDataAdapterTest;Trusted_Connection=Yes;");
SqlCommand cmd = new SqlCommand("AddRecord", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@TextField", SqlDbType.Char);
param.Value = string.Empty;
cmd.Parameters.Add(param);

try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (conn != null)
conn.Close();
}
}

Here's the simple stored procedure:

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO


CREATE PROCEDURE dbo.AddRecord

@TextField varchar(50) = null

as

Insert into table1
(TextField)
values (@TextField)


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Here's the table definitiion:

CREATE TABLE [Table1] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[TextField] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

Here's the query that shows that the length of the column is 1 and the space:

select datalength(textfield), '[' + textfield + ']' from table1


"Donald Joppa" wrote:

> We're converting from framework 1.1 to 2.0 and something interesting has
> popped up. Fields that are being inserted into the database as a zero length
> string in 1.1 contain a single space when inserted with 2.0 compiled code.
>
> The code specifies a stored proc to run and set up the parameter collection.
> I've verified in the debugger that in the parameter collection's item array
> entries that I care about contain a zero length string (this is true for both
> 1.1 and 2.0).
>
> SQL is running on a W2K3 box and the instance is SQL2K. I've checked the
> database compatibility level and it is set to 80.
>
> When I capture the insert command via SQL profiler from 1.1 the parameters
> show up as a zero length string (''), but when it comes from 2.0 code the
> same parameters contain a single space (' ').
>
> Anyone have any idea what I'm missing?

 
Reply With Quote
 
=?Utf-8?B?RG9uYWxkIEpvcHBh?=
Guest
Posts: n/a
 
      7th Aug 2007
William see my post to my original question. I've been able to reproduce the
issue in a much simpler scenario. Including c# code, table definition, and
stored procedure definition.

"William Vaughn" wrote:

> Let's see the code used to invoke the SP--especially the code that populates
> the Parameters collection. It would be helpful to know what values are
> passed to the problem parameter.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant, Dad, Grandpa
> Microsoft MVP
> INETA Speaker
> www.betav.com
> www.betav.com/blog/billva
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no rights.
> __________________________________
> Visit www.hitchhikerguides.net to get more information on my latest book:
> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> -----------------------------------------------------------------------------------------------------------------------
>
> "Donald Joppa" <(E-Mail Removed)> wrote in message
> news:89F148CA-21E6-4FD3-BD97-(E-Mail Removed)...
> > We're converting from framework 1.1 to 2.0 and something interesting has
> > popped up. Fields that are being inserted into the database as a zero
> > length
> > string in 1.1 contain a single space when inserted with 2.0 compiled code.
> >
> > The code specifies a stored proc to run and set up the parameter
> > collection.
> > I've verified in the debugger that in the parameter collection's item
> > array
> > entries that I care about contain a zero length string (this is true for
> > both
> > 1.1 and 2.0).
> >
> > SQL is running on a W2K3 box and the instance is SQL2K. I've checked the
> > database compatibility level and it is set to 80.
> >
> > When I capture the insert command via SQL profiler from 1.1 the parameters
> > show up as a zero length string (''), but when it comes from 2.0 code the
> > same parameters contain a single space (' ').
> >
> > Anyone have any idea what I'm missing?

>
>

 
Reply With Quote
 
=?Utf-8?B?RG9uYWxkIEpvcHBh?=
Guest
Posts: n/a
 
      7th Aug 2007
It turns out that changing the parameter to a type of varchar or using the
method signature that only specifies the colunm name and the value avoids
this problem. It seems likely that it is a bug in in the implicit conversion
from string to char to varchar. I think it should either work like 1.1 or it
should throw an exception.

"Donald Joppa" wrote:

> I've created a much simpler test that demonstrates this on my machine when
> compiled with the .NET 2.0 framework.
>
> Here's the C# code:
>
> public static void InsertRecord()
> {
> SqlConnection conn = new
> SqlConnection("Server=.;Database=SqlDataAdapterTest;Trusted_Connection=Yes;");
> SqlCommand cmd = new SqlCommand("AddRecord", conn);
> cmd.CommandType = CommandType.StoredProcedure;
> SqlParameter param = new SqlParameter("@TextField", SqlDbType.Char);
> param.Value = string.Empty;
> cmd.Parameters.Add(param);
>
> try
> {
> conn.Open();
> cmd.ExecuteNonQuery();
> }
> catch (Exception ex)
> {
> Console.WriteLine(ex.ToString());
> }
> finally
> {
> if (conn != null)
> conn.Close();
> }
> }
>
> Here's the simple stored procedure:
>
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS OFF
> GO
>
>
> CREATE PROCEDURE dbo.AddRecord
>
> @TextField varchar(50) = null
>
> as
>
> Insert into table1
> (TextField)
> values (@TextField)
>
>
> GO
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS ON
> GO
>
> Here's the table definitiion:
>
> CREATE TABLE [Table1] (
> [ID] [int] IDENTITY (1, 1) NOT NULL ,
> [TextField] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ) ON [PRIMARY]
> GO
>
> Here's the query that shows that the length of the column is 1 and the space:
>
> select datalength(textfield), '[' + textfield + ']' from table1
>
>
> "Donald Joppa" wrote:
>
> > We're converting from framework 1.1 to 2.0 and something interesting has
> > popped up. Fields that are being inserted into the database as a zero length
> > string in 1.1 contain a single space when inserted with 2.0 compiled code.
> >
> > The code specifies a stored proc to run and set up the parameter collection.
> > I've verified in the debugger that in the parameter collection's item array
> > entries that I care about contain a zero length string (this is true for both
> > 1.1 and 2.0).
> >
> > SQL is running on a W2K3 box and the instance is SQL2K. I've checked the
> > database compatibility level and it is set to 80.
> >
> > When I capture the insert command via SQL profiler from 1.1 the parameters
> > show up as a zero length string (''), but when it comes from 2.0 code the
> > same parameters contain a single space (' ').
> >
> > Anyone have any idea what I'm missing?

 
Reply With Quote
 
William Vaughn
Guest
Posts: n/a
 
      8th Aug 2007
Ah, why are you using Char? There are very few (good) reasons to do so. I
try to avoid it since space is usually not a consideration. I use it for
FIXED-length text values like the StateCode which has to be two characters
(and only two characters) and the value must exist in the ValidStates table.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Donald Joppa" <(E-Mail Removed)> wrote in message
news:44E6DF78-AE72-40CE-A1A5-(E-Mail Removed)...
> William see my post to my original question. I've been able to reproduce
> the
> issue in a much simpler scenario. Including c# code, table definition, and
> stored procedure definition.
>
> "William Vaughn" wrote:
>
>> Let's see the code used to invoke the SP--especially the code that
>> populates
>> the Parameters collection. It would be helpful to know what values are
>> passed to the problem parameter.
>>
>> --
>> ____________________________________
>> William (Bill) Vaughn
>> Author, Mentor, Consultant, Dad, Grandpa
>> Microsoft MVP
>> INETA Speaker
>> www.betav.com
>> www.betav.com/blog/billva
>> Please reply only to the newsgroup so that others can benefit.
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>> __________________________________
>> Visit www.hitchhikerguides.net to get more information on my latest book:
>> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
>> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
>> -----------------------------------------------------------------------------------------------------------------------
>>
>> "Donald Joppa" <(E-Mail Removed)> wrote in message
>> news:89F148CA-21E6-4FD3-BD97-(E-Mail Removed)...
>> > We're converting from framework 1.1 to 2.0 and something interesting
>> > has
>> > popped up. Fields that are being inserted into the database as a zero
>> > length
>> > string in 1.1 contain a single space when inserted with 2.0 compiled
>> > code.
>> >
>> > The code specifies a stored proc to run and set up the parameter
>> > collection.
>> > I've verified in the debugger that in the parameter collection's item
>> > array
>> > entries that I care about contain a zero length string (this is true
>> > for
>> > both
>> > 1.1 and 2.0).
>> >
>> > SQL is running on a W2K3 box and the instance is SQL2K. I've checked
>> > the
>> > database compatibility level and it is set to 80.
>> >
>> > When I capture the insert command via SQL profiler from 1.1 the
>> > parameters
>> > show up as a zero length string (''), but when it comes from 2.0 code
>> > the
>> > same parameters contain a single space (' ').
>> >
>> > Anyone have any idea what I'm missing?

>>
>>


 
Reply With Quote
 
=?Utf-8?B?RG9uYWxkIEpvcHBh?=
Guest
Posts: n/a
 
      8th Aug 2007
It is code someone else wrote quite some time ago and all of the string
parameters in the code were identified as char (that's been fixed now). In
the stored procedure and in the database columns those fields are varchar...

"William Vaughn" wrote:

> Ah, why are you using Char? There are very few (good) reasons to do so. I
> try to avoid it since space is usually not a consideration. I use it for
> FIXED-length text values like the StateCode which has to be two characters
> (and only two characters) and the value must exist in the ValidStates table.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant, Dad, Grandpa
> Microsoft MVP
> INETA Speaker
> www.betav.com
> www.betav.com/blog/billva
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no rights.
> __________________________________
> Visit www.hitchhikerguides.net to get more information on my latest book:
> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> -----------------------------------------------------------------------------------------------------------------------
>
> "Donald Joppa" <(E-Mail Removed)> wrote in message
> news:44E6DF78-AE72-40CE-A1A5-(E-Mail Removed)...
> > William see my post to my original question. I've been able to reproduce
> > the
> > issue in a much simpler scenario. Including c# code, table definition, and
> > stored procedure definition.
> >
> > "William Vaughn" wrote:
> >
> >> Let's see the code used to invoke the SP--especially the code that
> >> populates
> >> the Parameters collection. It would be helpful to know what values are
> >> passed to the problem parameter.
> >>
> >> --
> >> ____________________________________
> >> William (Bill) Vaughn
> >> Author, Mentor, Consultant, Dad, Grandpa
> >> Microsoft MVP
> >> INETA Speaker
> >> www.betav.com
> >> www.betav.com/blog/billva
> >> Please reply only to the newsgroup so that others can benefit.
> >> This posting is provided "AS IS" with no warranties, and confers no
> >> rights.
> >> __________________________________
> >> Visit www.hitchhikerguides.net to get more information on my latest book:
> >> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> >> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> >> -----------------------------------------------------------------------------------------------------------------------
> >>
> >> "Donald Joppa" <(E-Mail Removed)> wrote in message
> >> news:89F148CA-21E6-4FD3-BD97-(E-Mail Removed)...
> >> > We're converting from framework 1.1 to 2.0 and something interesting
> >> > has
> >> > popped up. Fields that are being inserted into the database as a zero
> >> > length
> >> > string in 1.1 contain a single space when inserted with 2.0 compiled
> >> > code.
> >> >
> >> > The code specifies a stored proc to run and set up the parameter
> >> > collection.
> >> > I've verified in the debugger that in the parameter collection's item
> >> > array
> >> > entries that I care about contain a zero length string (this is true
> >> > for
> >> > both
> >> > 1.1 and 2.0).
> >> >
> >> > SQL is running on a W2K3 box and the instance is SQL2K. I've checked
> >> > the
> >> > database compatibility level and it is set to 80.
> >> >
> >> > When I capture the insert command via SQL profiler from 1.1 the
> >> > parameters
> >> > show up as a zero length string (''), but when it comes from 2.0 code
> >> > the
> >> > same parameters contain a single space (' ').
> >> >
> >> > Anyone have any idea what I'm missing?
> >>
> >>

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook email changes spacing from single space to double space Bocephus Microsoft Outlook Discussion 1 22nd Feb 2010 10:19 PM
too much space between lines in a list (single space for page setu =?Utf-8?B?MDFhbm5l?= Microsoft Word Document Management 2 25th Apr 2007 09:00 PM
how do I convert Outlook automatic double space to single space? =?Utf-8?B?ZGF2aWR1cA==?= Microsoft Outlook Installation 1 22nd Aug 2006 04:05 PM
Go from single space to double space lines on a WORD doc =?Utf-8?B?QXJ0aHVy?= Microsoft Word Document Management 1 13th Aug 2006 08:09 PM
Converting Single Space to Double Space - AutoShape Question Leonard M. Wapner Microsoft Word New Users 1 6th Jul 2004 09:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:29 AM.