471,336 Members | 1,261 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

How to Retrieve a Numeric Type from a SqlServer table?

Hello,

I am attempting to retrieve a numeric type froma a table.

I get an invalid cast exception when I use the following code in my data
layer:

//The problem definitley occurs in the line"id = (int)
cmdSelect.ExecuteScalar();"

public int GetId(string handle)

{

string strSelect = null;

SqlCommand cmdSelect;

int id = 0;
strSelect = "Select [ID] FROM members WHERE handle = @handle";

SqlConnection conDb = new SqlConnection( _connStr );

cmdSelect = new SqlCommand(strSelect, conDb);

cmdSelect.Parameters.Add("@handle", handle);

conDb.Open();

id = (int) cmdSelect.ExecuteScalar();

conDb.Close();

return id;

}

So how does one normally extract a numeric type? Do I have to cast it as a
decimal or double?

Thanks in advance.

Frank


Jan 11 '06 #1
2 1941
Frank,

Yes, this is the case. You will have to convert it manually, either
casting to a double, and then cast that to int (for which a cast applies).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank" <fk******@pfmail.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
Hello,

I am attempting to retrieve a numeric type froma a table.

I get an invalid cast exception when I use the following code in my data
layer:

//The problem definitley occurs in the line"id = (int)
cmdSelect.ExecuteScalar();"

public int GetId(string handle)

{

string strSelect = null;

SqlCommand cmdSelect;

int id = 0;
strSelect = "Select [ID] FROM members WHERE handle = @handle";

SqlConnection conDb = new SqlConnection( _connStr );

cmdSelect = new SqlCommand(strSelect, conDb);

cmdSelect.Parameters.Add("@handle", handle);

conDb.Open();

id = (int) cmdSelect.ExecuteScalar();

conDb.Close();

return id;

}

So how does one normally extract a numeric type? Do I have to cast it as a
decimal or double?

Thanks in advance.

Frank


Jan 11 '06 #2
Hi,

"Frank" <fk******@pfmail.com> wrote in message
news:eQ**************@TK2MSFTNGP14.phx.gbl...
Hello,

I am attempting to retrieve a numeric type froma a table.

I get an invalid cast exception when I use the following code in my data
layer:

//The problem definitley occurs in the line"id = (int)
cmdSelect.ExecuteScalar();"


The problem here is taht you have no idea what ExecuteScalar will return,
it's possible that the return type is not castable to int, I would use
Convert.ToInt32 instead.
and even this is not 100% sure as you can get an overflow depending of the
value received.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by M.A. Oude Kotte | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.