Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems converting float to a string in C#

Status
Not open for further replies.

iRead

Programmer
Mar 12, 2004
25
US
The value of the variable I’m trying to write to fTotal comes from a SQL query on a column with a type of float. I think my problem may have something to do with boxing and unboxing variables. I plan to research this to understand it better. Any input on what causes the errors in the two code examples below will be invaluable to my understanding of this. In each example the error rendered follows the // on the line generating the error.

Example 1:
float fTotal = 0.00F;
fTotal = (r.GetFloat(0)); //Exception Details: System.InvalidCastException: Specified cast is not valid
LTotal.Text = (fTotal.ToString());

Example 2:
float fTotal = 0.00F;
fTotal = (r.GetString(0)); //Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'float'
LTotal.Text = (fTotal.ToString());

Thank you all for your patience and help!
iRead
 
Okay here is all of the code associated with the error I'm getting

The value of the variable I’m trying to write to LTotal comes from a SQL query. The firstName column is type varChar. The column amount is float. The character column writes to the variable fine. The float column causes the error in the commented line below it.

The real problem is in the code below which throws the exception I need some method any method to get that amount column to a variable.
_______________________________________________________________________
private void btnReport_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection(ConnectionString());
SqlCommand comm = new SqlCommand("select firstName, amount from Test", conn);
comm.Connection.Open();
SqlDataReader r =
comm.ExecuteReader(CommandBehavior.CloseConnection);
while(r.Read())
{

LName.Text = (r.GetString(0));
//Exception Details: System.InvalidCastException: Specified cast is not valid.
LTotal.Text = (r.GetString(1));

}
r.Close();
conn.Close();
}
_______________________________________________________________________

Thank you all for your patience and help!
iRead
 
You should write:
LTotal.Text = System.Convert.ToString(r.GetFloat(1)); // 2nd column (amount) is a float
-obislavu-
 
Thank you obislavu!
I had tried that and still got an error. To be truthful I tried many, many approaches with no success.

This was posted to me on another forum. This led to a resolution of the problem!

_______________________________________________________________________________

As you can see, the .NET Framework mapping to a SQL type of float is actually "double".

handy chart to have around, I'd think!
________________________________________________________________________________

Handy to have around doesn’t cover it. Would have saved me hours!
This code works perfectly:

double dTotal = (r.GetDouble(1));
LTotal.Text = (dTotal.ToString());

Thank you!
iRead
 
float fTotal = 0.00F;
fTotal = (r.GetFloat(0)); //Exception Details: System.InvalidCastException: Specified cast is not valid

Try this instead:
Code:
double total = r.GetDouble(0);

The SQL Server float type is 64-bits, where the C# float is only 32-bits wide.
 
octothorpe,
Still thank you for your input! It's right on target.
Thanks
iRead
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top