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

Assign variable to a datareader

I need to assign data from a data reader to a variable. here is a simpiet of
code,

OleDbDataReader dtrBMail = cmd.ExecuteReader();
if (dtrBMail.Read()){
int t_mob_var_send_no;
t_mob_var_send_no=
(int)dtrBMail["var"].ToSingle();

}

The error I receive in the last line is, CS0117: 'string' does not contain a
definition for 'ToSingle'

How do I correct this?
Nov 16 '05 #1
3 2928
David,

Yes, you will have to call the static ToSingle method on the Convert
class, or call the static Parse method on the Single structure, like so:

// Convert from a string to a single.
float Convert.ToSingle(dtrBMail["var"]);

However, since you are converting to an integer, I would just call the
ToInt32 method.

If the field is one of these types already, then I would suggest a cast,
since the type is already that. This way, you don't have to execute more
code to get the same result.

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

"Da***@web.com" <Da*********@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
I need to assign data from a data reader to a variable. here is a simpiet
of
code,

OleDbDataReader dtrBMail = cmd.ExecuteReader();
if (dtrBMail.Read()){
int t_mob_var_send_no;
t_mob_var_send_no=
(int)dtrBMail["var"].ToSingle();

}

The error I receive in the last line is, CS0117: 'string' does not contain
a
definition for 'ToSingle'

How do I correct this?

Nov 16 '05 #2
This indicates there is no ToSingle() method in the String class. Which
suggests that dtrBMail["var"] must be something like a VarChar that maps to
a String. So I would do one of the following:

t_mob_var_send_no = Convert.ToInt32(dtrBMail["var"]);
t_mob_var_send_no = dtrBMail.GetInt32(i); // where "i" is the ordinal seq #
of the field

No need to go from string to single to int, just go straight to int.

--Bob

"Da***@web.com" <Da*********@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
I need to assign data from a data reader to a variable. here is a simpiet
of
code,

OleDbDataReader dtrBMail = cmd.ExecuteReader();
if (dtrBMail.Read()){
int t_mob_var_send_no;
t_mob_var_send_no=
(int)dtrBMail["var"].ToSingle();

}

The error I receive in the last line is, CS0117: 'string' does not contain
a
definition for 'ToSingle'

How do I correct this?

Nov 16 '05 #3
That fixed the problem. But now I have a follow up question. Now that I have
the value and change the value I need to update the value in the table. When
I use the parameter command,

cmd.Parameters.Add("@var_new", OleDbType.Int32).Value= t_mob_var_send_no;

I receive the error,

CS0117: 'System.Data.OleDb.OleDbType' does not contain a definition for
'Int32'

even though it looks like to me in the documentation that Int32 is defined.
What's the problem?

"Bob Grommes" wrote:
This indicates there is no ToSingle() method in the String class. Which
suggests that dtrBMail["var"] must be something like a VarChar that maps to
a String. So I would do one of the following:

t_mob_var_send_no = Convert.ToInt32(dtrBMail["var"]);
t_mob_var_send_no = dtrBMail.GetInt32(i); // where "i" is the ordinal seq #
of the field

No need to go from string to single to int, just go straight to int.

--Bob

"Da***@web.com" <Da*********@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
I need to assign data from a data reader to a variable. here is a simpiet
of
code,

OleDbDataReader dtrBMail = cmd.ExecuteReader();
if (dtrBMail.Read()){
int t_mob_var_send_no;
t_mob_var_send_no=
(int)dtrBMail["var"].ToSingle();

}

The error I receive in the last line is, CS0117: 'string' does not contain
a
definition for 'ToSingle'

How do I correct this?


Nov 16 '05 #4

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

Similar topics

2
by: Jim via DotNetMonster.com | last post by:
Hi, I'm passing a variable to another page through a querystring. I then want to use that variable to retrieve records from a database to poulate a dropdownlist. I can read the variable from the...
5
by: Jim via DotNetMonster.com | last post by:
Hi, I need to pass the value of a variable from one function to another but I don't seem to get the value. I declare the variable outside all functions. What I'm trying to do is that when the...
4
by: Jim via DotNetMonster.com | last post by:
Hi, How can I assign the result of a function to a variable. I need to get the result so that I can query the database again. What I'm trying is:...
1
by: Jim via DotNetMonster.com | last post by:
Hi, I'm trying to set one of the columns in the datareader to a variable so that I can use it in a different function. When I do that then all the other values in the data reader don't show up....
2
by: Shapper | last post by:
Hello, I have a DataReader which returns only one record from a database: ID Name Content 12 hello world Is it possible to create a variable named var_hello which value is "world"?
0
by: JJ_377 | last post by:
The following doesn't assign value to the dropdownlist - WHY? ___________________________________________________________________ In a user control (ascx named USACustomer) : Public Property...
6
by: david | last post by:
I try to use "for" loop to retrieve and assign values in web form. The code is in the following. But it can not be compiled. What I want to do is: txtQ1.Text =...
4
by: Learner | last post by:
Hello, This my a method to call a stored proce and uses a DataReader to read the data in the below method I am trying to assign a null value to my datareader variable Dim datareader As...
2
by: bcshaw | last post by:
Hey all I have a vb.net/asp.net dynamic website connected to an access database using a oledb connection. I use a datareader to retrieve the results of a query and iterate through the results using...
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:
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
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.