newbie: Converting DOUBLE to STRING

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

Guest

Hello -

Using C#, i am trying to convert a double value to string, so that it can be
assigned to a LABEL control on my Web Form Page. However, i continue to get
"invalid cast" errors. My code is as follows:

double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

I would have expected that the string "0.5" would be the result. Obviously,
i don't konw something basic. If someone could point out what i'm doing
wrong, that would be great.

Thanks,
 
double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
This is an instance of a standard textbox webcontrol. For example:

protected System.Web.UI.WebControls.TextBox txtResult;
 
Unfortunately, this does not work either. Hence, why i've written out the
"System.Convert.ToString"... to try and "force" the cast.
 
re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
;) I didn't even notice that :)

G.

Juan T. Llibre said:
re:

That *does* work...if you change .text to .Text

Not :

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
 
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
Gabriel Lozano-Morán said:
;) I didn't even notice that :)

G.
 
re:
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

Why are you using a LinkButton and not a Label there ?
A LinkButton control is a submit button.

Why do you want a result placed on a Submit Button ?

Aren't you declaring your Label like this :

Label lblResult = (Label)e.Item.FindControl("lblResult");

?

I haven't checked this out, but try this :

((Label)e.Item.FindControl(lblResult)).Text = iR.ToString();



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
Gabriel Lozano-Morán said:
;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

does FindControl really find a LinkButton?
try splitting the statement:
string s = iR.ToString();
LinkButton lb = (LinkButton)e.Item.FindControl(lblResult);
lb.Text = s;
and see where the error is.

Hans Kesting

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET
would not allow this to compile, so this is not the problem,
although, i my sample code below, this was indeed correct.

The *real* code is:

double iS =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried
this with manual values, following the sample below, and i still get
errors.... The error generated is:

A first chance exception of type 'System.InvalidCastException'
occurred in marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
Gabriel Lozano-Morán said:
;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server"
Text=""></asp:Label> </div>
</form>
</body>
</html>
---------



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

message double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult?
Instance of TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
Guys,

Very sorry for the mix-up... the problem was the (LinkButton) cast, not the
double. It should be a (TextBox) cast.

Again, thanks for your help!

charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
Gabriel Lozano-Morán said:
;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
Hmmm.. my hunch was on the right track.

I'll only add that, if it's a Textbox you are using,
you should change its identifier to txt, instead of lbl :

((Texbox)e.Item.FindControl(txtResult)).Text = iR.ToString();

....as opposed to :
((Label)e.Item.FindControl(lblResult)).Text = iR.ToString();




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

charliewest said:
Guys,

Very sorry for the mix-up... the problem was the (LinkButton) cast, not the
double. It should be a (TextBox) cast.

Again, thanks for your help!

charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

charliewest said:
Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
:

;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
Yes, all good ideas. Thanks Juan.

Juan T. Llibre said:
Hmmm.. my hunch was on the right track.

I'll only add that, if it's a Textbox you are using,
you should change its identifier to txt, instead of lbl :

((Texbox)e.Item.FindControl(txtResult)).Text = iR.ToString();

....as opposed to :
((Label)e.Item.FindControl(lblResult)).Text = iR.ToString();




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

charliewest said:
Guys,

Very sorry for the mix-up... the problem was the (LinkButton) cast, not the
double. It should be a (TextBox) cast.

Again, thanks for your help!

charliewest said:
To illustrate my last thread, the following does not work:

double iS = 5;
double iT = 10;
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text = iR.ToString();

I get the error listed below.

:

Thanks for the help. As for the ".text" vs. ".Text", the VS.NET would not
allow this to compile, so this is not the problem, although, i my sample code
below, this was indeed correct.

The *real* code is:

double iS = (Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsSafe"]));
double iT =
(Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["ObsScores"]));
double iR = iS/iT;
((LinkButton)e.Item.FindControl(lblResult)).Text =
System.Convert.ToString(iR);

The above works fine for all other operations. In fact, i've tried this with
manual values, following the sample below, and i still get errors.... The
error generated is:

A first chance exception of type 'System.InvalidCastException' occurred in
marsh.dll
Additional information: Specified cast is not valid.

Any thoughts?
:

;) I didn't even notice that :)

G.

re:
This should normally work.

That *does* work...if you change .text to .Text

Not :
txtResult.text = System.Convert.ToString(r);
but :
txtResult.Text = System.Convert.ToString(r);

DoubleToString.aspx :
--------------------------------
<%@ page language="C#" %>
<script language = "C#" runat="server">
void Page_Load(){
double i = 5;
double s = 10;
double r = i/s;
txtResult.Text = System.Convert.ToString(r);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Double To String</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="txtResult" Runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
---------



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

This should normally work. What is the type of txtResult? Instance of
TextBox class or a control you created yourself?

Gabriel Lozano-Morán
 
double i = 5;
double s = 10;
double r = i/s;
txtResult.text = System.Convert.ToString(r);

The more efective way is to use implicit conversion which is triggered
by the operator's '+' side effect:

double r = 5/10;
txtResult.Text = "" + r;

the last line will implicitly call ToString method of the r object.

it looks much nicer, furthermore, in cases where object tipe is not
known, it is possible to avoid explicit reflection by using same trick:
double r=5/10;
object rr=r;

txtResult.Text = "" + rr;


:) it's nice to see that old tricks from Java are still working under c#
 
Back
Top