Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bug in cmd.Parameters statement and byte?

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I have some code I am converting from 2008 to 2013 and found a situation where the code is handled differently.

I have a statement:

cmd.Parameters.Add(new SqlParameter("@NameType", (byte)1))

This works correctly in 2008 and 2013 and puts a "1" in the value field

If I do:

cmd.Parameters.Add(new SqlParameter("@NameType", (byte)0))

it puts a "0" in the value of the parameter in VS2008 but in VS2013, it puts a null.

I am trying to find out if this is an issue only with cmd.Parameters.Add or is it with areas?

I did do:

(byte)dr["NameType"])

And if the value is 0 it will return a 0.

Since this is a fairly large project I need to find out if there are other areas where this could be an issue.

Thanks,

Tom
 
Tom

Have you tried,

dim mycommand as new sqlcommand
dim shipmentid as string = ("12345")


with mycommand

.Parameters.Add("myparam", SqlDbType.Bit, 1, shipmentid) 'Note you dont need the "@" here in myparam.

end with

(parameter name as string, SqlDbType as system,data.SqlDbType, size as integer, source column as string)



I use this construct in VS 2012 without any problems
 
The problem is not that my format doesn't work (it does for (byte)1 and anything else).

It is just that (byte)0 should have passed 0 correctly and it doesn't seem to (or it messes up in the Add method).

I am just trying to find out if this is an anomaly and it is the only place where it is an error.

Thanks,

Tom
 
Ok Tom


Have you iterated through your parms after values etc. have been assigned to see what's actually in them.

For X As Int32 = 0 To cmd.Parameters.Count - 1
Response.Write(cmd.Parameters(X).ParameterName & "Name: " & cmd.Parameters(X).Value & " Size is. " & cmd.Parameters(X).Size & "<br />")
Next
 
Yes.

The value actually is null. It gave me an error since it was trying to move the null into tinyint.

When I run it in VS 2008, the value is a 0.
 
Do you need to use a type of byte? Are the values only going to be 0 or 1? If so, then use a bit type.
 
You miss my point.

It isn't working correctly. I know how to fix it.

I am trying to find out if this is the only bug. It should be setting the tinyint to a 0 and isn't.

Either 2008 or 2013 is wrong.

If I pass a (byte)0 it should be 0. (byte)1 is equal to 1.
 
I don't know the answer to that. You will have to search or look on the MS website to see if there is any information regarding it.
In the meantime, just do the fix to get your code working.
 
When I test this using VB, it wouldn't even let me cast an integer to a byte, it would only accept strings. It could just be something relevant to C#.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top