SqlConnection and ExecuteScalar

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

I store the value that returns the ExecuteScalar method of the SqlConnection
object in a variable (object type). The problem is that if I close the
connection object, I can't read the content of the variable. Why occurs
this?

Thank you.
..
 
Alberto said:
I store the value that returns the ExecuteScalar method of the SqlConnection
object in a variable (object type). The problem is that if I close the
connection object, I can't read the content of the variable. Why occurs
this?

Could you give a short but complete example which demonstrates the
problem?
 
This is the code:
try
{
Connect();
SqlCommand cmd = new SqlCommand(sql, cnn);
Object obj = cmd.ExecuteScalar();
return obj;
}
catch {...}
finally {Disconect();}

Thanks.
 
Alberto said:
This is the code:
try
{
Connect();
SqlCommand cmd = new SqlCommand(sql, cnn);
Object obj = cmd.ExecuteScalar();
return obj;
}
catch {...}
finally {Disconect();}

That's not a complete example - it's not something I can run. However,
what are you seeing when you then try to use the returned value?

What is the type of the scalar being returned?
 
HI Alberto,

That code should runs fine, the only reason I can think of for it not
working if is the return value is an Image or Binary type.

A good idea would be if you post the command text and the table columns's
types.


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Alberto said:
This is the code:
try
{
Connect();
SqlCommand cmd = new SqlCommand(sql, cnn);
Object obj = cmd.ExecuteScalar();
return obj;
}
catch {...}
finally {Disconect();}

Thanks.
 
Sorry. The code works fine. The trouble was in the cast of the value.

Thanks anyway.

Ignacio Machin ( .NET/ C# MVP ) said:
HI Alberto,

That code should runs fine, the only reason I can think of for it not
working if is the return value is an Image or Binary type.

A good idea would be if you post the command text and the table columns's
types.


Cheers,
 
Back
Top