SELECT @@IDENTITY issue

B

buzz

I am using Compact Framework; C#.

I have a situation where I need to retrieve the autoincrement value from the
primary key of a table.
I use the following code:

DataRow dr = _dsCall.Tables[0].NewRow();

_dsCall.Tables[0].Rows.Add(dr);
_daCall.Update(_dsCall, "Calls");

SqlCeCommand cmd = new SqlCeCommand("SELECT @@IDENTITY",
Database.Connection);
caid = (int)cmd.ExecuteScalar();

The ExecuteScalar cast fails, because it is returning a decimal value, even
though the primary key is an INT!!??

To resolve this, and convert to a string and back:
caid = Int32.Parse(cmd.ExecuateScalar().ToString());

Any ideas as to why ExecuteScalar is returning a decimal??

Thanks.
 
W

wanderer

Not sure, but you might want to use
Convert.ToInt32(decimal_value) instead of converting to
String, parsing, and converting...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top