ExecuteScalar

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I need to return a sum of units from a inventory table in code. I'm using a
ExecuteScalar statement. I have the following code,

OleDbCommand t_command = new OleDbCommand(m_sumstring, connection)
connection.Open();
t_command.ExecuteScalar();

I see lots of similar examples but they never show how to actually get the
data. So how do I read the value that is returned?

int m_data= t_command.ExecuteScalar();

doesn't work.
 
Dave said:
I need to return a sum of units from a inventory table in code. I'm using a
ExecuteScalar statement. I have the following code,

OleDbCommand t_command = new OleDbCommand(m_sumstring, connection)
connection.Open();
t_command.ExecuteScalar();

I see lots of similar examples but they never show how to actually get the
data. So how do I read the value that is returned?

int m_data= t_command.ExecuteScalar();

doesn't work.

ExecuteScalar returns Object. Just cast it to whatever data type is
appropriate:

int m_data = (int)t_command.ExecuteScalar();

Tom Dacon
Dacon Software Consulting
 
Dave said:
I need to return a sum of units from a inventory table in code. I'm using a
ExecuteScalar statement. I have the following code,

OleDbCommand t_command = new OleDbCommand(m_sumstring, connection)
connection.Open();
t_command.ExecuteScalar();

I see lots of similar examples but they never show how to actually get the
data. So how do I read the value that is returned?

int m_data= t_command.ExecuteScalar();

doesn't work.

The ExectueScalar method itself does not know that you'll be returning the
integer sum of units from an inventory table. It only knows to return the
first column from the first record result, which is an Object.

You'll need to convert that object to your desired type.

in m_data = Convert.ToInt16(t_command.ExecuteScalar());

-Scott
 
Back
Top