473,387 Members | 1,722 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,387 software developers and data experts.

DataReader.GetInt.....

Hi,

How can I get value of a SQL column defined as int.

I try
int myInt = dataReader.GetInt32(0);

I get invalid cast exception and same for
GetInt16...

whats the best way?

Thnx
Apr 20 '07 #1
3 5919
the cast error means the column is not a numeric column or null. if its
a string then you can parse it.

int i = int.Parse(datareader.GetString(0);

-- bruce (sqlwork.com)

mavrick_101 wrote:
Hi,

How can I get value of a SQL column defined as int.

I try
int myInt = dataReader.GetInt32(0);

I get invalid cast exception and same for
GetInt16...

whats the best way?

Thnx
Apr 20 '07 #2
mavrick_101 wrote:
Hi,

How can I get value of a SQL column defined as int.

I try
int myInt = dataReader.GetInt32(0);

I get invalid cast exception and same for
GetInt16...

whats the best way?

Thnx
As bruce said, the value is not an int. How do the query look like that
gets the value?

If the value may be null, you have to check that before trying to read
the value:

int myInt;
if (dataReader.IsDbNull(0) {
myInt = -1; // or whatever...
} else {
myInt = dataReader.GetInt32(0);
}

--
Göran Andersson
_____
http://www.guffa.com
Apr 20 '07 #3
Put a break on it, then set a watch for dataReader[0]. It will tell you what
type it actually is. An int should be Int32, though, as you said.


"mavrick_101" <ma********@discussions.microsoft.comwrote in message
news:D7**********************************@microsof t.com...
Hi,

How can I get value of a SQL column defined as int.

I try
int myInt = dataReader.GetInt32(0);

I get invalid cast exception and same for
GetInt16...

whats the best way?

Thnx
Apr 20 '07 #4

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

Similar topics

6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
6
by: Ravi | last post by:
Hi, I am not able to understand why a datareader needs a connection to the DB all the time. Here is what I tried. Sqlcommand cmd = ("select * from table1",con) // where con is the connection...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
2
by: Andrei Pociu | last post by:
In a typical ASP .NET Web Application (website), I'm currently using a class where I declare some public static objects. For example there's the place where I initialize the SqlConnection. Also...
1
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public...
2
by: Tarun Mistry | last post by:
Hello everyone, I am using the data reader to access the information within a results set and then pass this information onto the constructor of my classes. Im using the following syntax to...
7
by: Varangian | last post by:
Hi all, the question I want to ask if the conversion of a DataReader to a Table looping through the DataReader is better than using the Fill Method of the DataAdapter... I'm asking because...
7
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
14
by: pralu | last post by:
what getInt() do??? buffer.getInt()
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.