473,242 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 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 2021
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Luigi Ida' via SQLMonster.com | last post by:
Hi, I have a php application connected through odbc to a sqlserver database. When I try to execute select queries on a smalldatetime table field I receive this message: Warning: Numeric value...
3
by: Le | last post by:
So here's my dilemma. I need an automated SQL Server (2000) job that runs once once per hour. This jobs should: 1) Retrieve unread email for the user "Le" from our Exchange Server, and insert...
2
by: Mac | last post by:
I have imported some data to sql2k from my old system. Somehow, it imported invoice amount to char type. I just created another column called invamt2 type NUMERIC so I can copy or convert...
5
by: Roy Gourgi | last post by:
Hi, I am used to working in Visual FoxPro and I would like to be able to create a database and store and retrieve information from it. What is the simplest way to do it and what should I be...
2
by: jaYPee | last post by:
I have no problem setting the selectcommand in sqldataadapter to fetch record from sqlserver w/ where clause in parent table. however, my problem is on how can i fetch the child table which is...
6
by: M.A. Oude Kotte | last post by:
Hi All, I hope this is the correct mailing list for this question. But neither postgresql.org nor google could help me out on this subject. I did find one disturbing topic on the mailing list...
1
by: ahujasatna | last post by:
Hi all!! i am working on C#, ASP.Net with Sql server2000 and i am facing a problem, my Question is: "How to insert and retrieve images to/from Sqlserver2000 Database" I created a table...
5
by: Ken | last post by:
I'm trying to run a loop to capture column property information from a table in my datasource. Can anybody see where this is going wrong? Dim tbl As New DataTable Dim col As DataColumn Dim x...
1
by: Navs | last post by:
Hi, I am trying to insert a TIME value in a SQL Server 2008 database using a simple ODBC C program. I follow the steps below: 1. Connect and Allocate Statement handle 2. Prepare a Statement...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.