I've just finished writing some unit tests to test some SqlParameter
functions I created for a DAL helper class. I have included the base
version of the method below. One of the tests I wrote is a boundary
test and checks to make sure a size of -1 throw an
'ArgumentOutOfRange' exception. For some reason, it does
not....however, anything less than -1 does.

Does anyone know why?

BTW: I'm using .Net 2.0 and Ado.NET

public SqlParameter createStringParameter(string paramName,
int size)
{
SqlParameter result = new SqlParameter(paramName,
SqlDbType.VarChar);
result.Size = size;
result.Value = DBNull.Value;
return result;
}

Cheers,

Ed

RE: SqlParamater where type=SqlDbType.VarChar allows a size of -1!!! by KerryMoorman

KerryMoorman
Tue Apr 08 21:25:00 CDT 2008

Ed,

I believe that both -1 and 0 have valid special meanings.

Kerry Moorman


"BillDoorNZ@gmail.com" wrote:

> I've just finished writing some unit tests to test some SqlParameter
> functions I created for a DAL helper class. I have included the base
> version of the method below. One of the tests I wrote is a boundary
> test and checks to make sure a size of -1 throw an
> 'ArgumentOutOfRange' exception. For some reason, it does
> not....however, anything less than -1 does.
>
> Does anyone know why?
>
> BTW: I'm using .Net 2.0 and Ado.NET
>
> public SqlParameter createStringParameter(string paramName,
> int size)
> {
> SqlParameter result = new SqlParameter(paramName,
> SqlDbType.VarChar);
> result.Size = size;
> result.Value = DBNull.Value;
> return result;
> }
>
> Cheers,
>
> Ed
>

RE: SqlParamater where type=SqlDbType.VarChar allows a size of -1! by MisbahArefin

MisbahArefin
Wed Apr 09 10:28:00 CDT 2008

if you dont specify the size of a SqlDbType.VarChar type parameter then
ADO.NET will set the size to the length of the .Value property - this affects
your query plans since cached query plans depend on parameter sizes

http://www.rocksthoughts.com/blog/archive/2008/02/19/linq-to-sql-v-1-0-hickups.aspx
the above url discusses LINQ to SQL but the same principle applies to
ADO.NET parameters


--
Misbah Arefin
https://mcp.support.microsoft.com/profile/MISBAH.AREFIN
http://www.linkedin.com/in/misbaharefin


"Kerry Moorman" wrote:

> Ed,
>
> I believe that both -1 and 0 have valid special meanings.
>
> Kerry Moorman
>
>
> "BillDoorNZ@gmail.com" wrote:
>
> > I've just finished writing some unit tests to test some SqlParameter
> > functions I created for a DAL helper class. I have included the base
> > version of the method below. One of the tests I wrote is a boundary
> > test and checks to make sure a size of -1 throw an
> > 'ArgumentOutOfRange' exception. For some reason, it does
> > not....however, anything less than -1 does.
> >
> > Does anyone know why?
> >
> > BTW: I'm using .Net 2.0 and Ado.NET
> >
> > public SqlParameter createStringParameter(string paramName,
> > int size)
> > {
> > SqlParameter result = new SqlParameter(paramName,
> > SqlDbType.VarChar);
> > result.Size = size;
> > result.Value = DBNull.Value;
> > return result;
> > }
> >
> > Cheers,
> >
> > Ed
> >