tags:

views:

109

answers:

1

Hi All,

Consider this command, as written here:

Command.CommandText = "SELECT COUNT(ASSET) AS POSEXISTS FROM POSITIONS WHERE ASSET = @ASSET";
        Command.Parameters.Add("@ASSET",DbType.String).Value = Symbol;

So here, how should I execute this query so that, I can receive the POSEXISTS variable in the C# environment.

Soham

+1  A: 
int posExists = (int)Command.ExecuteScalar();
Adam Robinson
Hi Adam, thanks for the quick reply. I tried it, and though it gave a typecasting error, this worked betterPositionExists = Convert.ToInt16(Command.ExecuteScalar());
Soham
`Convert.ToInt32` would also work, as it does more than cast. Since it was coming out of the db, I was assuming it was an `int`. In any case, glad you got it working!
Adam Robinson
:) Same here, I also assumed it will be an int, perhaps its getting passed on as string
Soham
Just check `Command.ExecuteScalar().GetType()` to see what it is. I'll be quite surprised if `COUNT` is returning a string, but I could believe it's a `short` (`Int16`).
Adam Robinson