Assign variable to a datareader

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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.
 
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
 
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 said:
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

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?
 
Back
Top