From: Dave on
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.
From: Tom Dacon on
"Dave" <Dave(a)noWeb.com> wrote in message
news:325F62F9-0144-4421-BE19-BE0A982ED304(a)microsoft.com...
>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



From: Scott M. on

"Dave" <Dave(a)noWeb.com> wrote in message
news:325F62F9-0144-4421-BE19-BE0A982ED304(a)microsoft.com...
>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


From: Patrice on
Doesn't work ? That is ? Is this a compile time error because you didn't
cast the object ?

Also the doc
(http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(VS.80).aspx)
almost always a short but usable sample...

--
Patrice

"Dave" <Dave(a)noWeb.com> a écrit dans le message de
news:325F62F9-0144-4421-BE19-BE0A982ED304(a)microsoft.com...
>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.

From: Scott M. on
Oops! Typo, make that:

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

-Scott


"Scott M." <s-mar(a)nospam.nospam> wrote in message
news:%23RZRIkEhKHA.5528(a)TK2MSFTNGP05.phx.gbl...
>
> "Dave" <Dave(a)noWeb.com> wrote in message
> news:325F62F9-0144-4421-BE19-BE0A982ED304(a)microsoft.com...
>>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
>