473,404 Members | 2,137 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,404 software developers and data experts.

extracting integers from datarows

I have an integer value inside a datarow. Is there a more graceful way
to extract it than this:

int value = Convert.ToInt32(datarow[datacolumn].ToString());

It's already an integer. Is there a way around the conversion?

Jan 5 '07 #1
9 6439
Have you tried
int value = (int) datarow[datacolumn];
?
Marc
Jan 5 '07 #2

"Marc Gravell" <ma**********@gmail.comwrote in message
news:eP**************@TK2MSFTNGP06.phx.gbl...
Have you tried
int value = (int) datarow[datacolumn];
?
Marc
I had the same problem - doing the above, I got an invalid cast error, and
had to use the method the OP suggested (although I don't need to put the
"ToString()" on the end)
James

Jan 5 '07 #3
Hi,
<ap********@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
>I have an integer value inside a datarow. Is there a more graceful way
to extract it than this:

int value = Convert.ToInt32(datarow[datacolumn].ToString());
There is no need to convert it to string,
Convert.ToInt32(datarow[datacolumn] ); will be good.
--
Ignacio Machin
machin AT laceupsolutions com
Jan 5 '07 #4
Hi
"james" <ja***@com.comwrote in message
news:45***********************@news.zen.co.uk...
>
>
I had the same problem - doing the above, I got an invalid cast error, and
had to use the method the OP suggested (although I don't need to put the
"ToString()" on the end)
James
I think I remember having such a problem too, I got used a long time ago to
always use Convert.ToXXX .
--
Ignacio Machin
machin AT laceupsolutions com
Jan 5 '07 #5
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi
"james" <ja***@com.comwrote in message
news:45***********************@news.zen.co.uk...

I had the same problem - doing the above, I got an invalid cast error, and
had to use the method the OP suggested (although I don't need to put the
"ToString()" on the end)
James

I think I remember having such a problem too, I got used a long time ago to
always use Convert.ToXXX .
--
Ignacio Machin
machin AT laceupsolutions com
I guess it makes a kind of sense - if you're pulling a dataset from a
web service, it comes as XML which is essentially a huge string.

Jan 5 '07 #6
james <ja***@com.comwrote:
I had the same problem - doing the above, I got an invalid cast error, and
had to use the method the OP suggested (although I don't need to put the
"ToString()" on the end)
The exception should have told you what the original type was (I think
- if not, it's easy enough to do).

Sometimes you need two casts, but I think that's generally nicer than
formatting and then parsing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 5 '07 #7
Hi,

<ap********@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Ignacio Machin ( .NET/ C# MVP ) wrote:
>Hi
"james" <ja***@com.comwrote in message
news:45***********************@news.zen.co.uk.. .
>
>
I had the same problem - doing the above, I got an invalid cast error,
and
had to use the method the OP suggested (although I don't need to put
the
"ToString()" on the end)
James

I think I remember having such a problem too, I got used a long time ago
to
always use Convert.ToXXX .
--
Ignacio Machin
machin AT laceupsolutions com

I guess it makes a kind of sense - if you're pulling a dataset from a
web service, it comes as XML which is essentially a huge string.

Yes, but that's when it's serialized, as soon as it's created in memory as a
dataset instance (deserialized) it's not longer a long string. each column
will have the expected type.

--
Ignacio Machin
machin AT laceupsolutions com
Jan 5 '07 #8
HI Jon,
>
Sometimes you need two casts, but I think that's generally nicer than
formatting and then parsing.

Can you provide a case where you need two casting?

Jan 5 '07 #9
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions.com>>
wrote:
Sometimes you need two casts, but I think that's generally nicer than
formatting and then parsing.

Can you provide a case where you need two casting?
Yup - if you want something as an int, but it's currently a boxed
SqlInt32. Basically you need one cast for the unboxing, and another for
the explicit conversion:

using System;
using System.Data.SqlTypes;

class Test
{
public static void Main()
{
object o = new SqlInt32(10);
int x = (int) (SqlInt32) o;
}
}

(There are other ways of doing it, of course, but the above is about as
simple as it gets, IMO.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tony Clarke | last post by:
Hi All, I have been trying to extract data from a text file using the fscanf() functions and sscanf() functions. The file is of various characters and integers separated by semicolons, the...
2
by: Mantorok Redgormor | last post by:
If I have an array of char and there is an integer located in that char that was assigned by: int foo=10; *(int *)&array = foo; How can I extract that integer? Any portable way? I can assign...
1
by: andy.lee23 | last post by:
Hi, I've been learning C for a couple of weeks and I need some help. I have a text file and I want to extract integers from it to perform calculations. The format of the file is as follows: 3...
1
by: Dominik Luyten | last post by:
Hi, I would like to format certain DataRows in a Windows.Forms.DataGrid. I know how to do that for the columns, but I cannot find a way to assign a specific font, color etc. to a specific kind...
5
by: Nathan Sokalski | last post by:
I am writing an ASP.NET application in which I need to copy DataRows from one DataTable to another. When I use code such as the following: temprows = nodes.Select("state='PA'")...
9
by: Brad | last post by:
I have written some code to manipulate data/records in a MASTER (order header) and DETAIL (order details) tables. What I have written is too extensive to post but essentially trying to: 1....
4
by: dor | last post by:
i have an input file named input.txt where all the data looks like this: (4,10) 20 (5,3) 13 (7,19) 6 .. .. .. the numbers are random. i need to use every number in each line
3
by: creator_bob | last post by:
How do I create an array of datarows from a sorted list? I put a bunch of datarows into a sorted list to sort them. Then I got an array of the sorted elements. However, I cannot typecast them. ...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
8
by: jehugaleahsa | last post by:
Hello: We wrote an entire application where we add our DataRows to our DataTables immediately. However, we have to shut off our constraints to do this. We would like to use detached DataRows to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.