Format text in label to amount

S

staeri

I'm populating a label with an amount like this:

lblBudget.Text = Generic_database_functions.GetValue("SELECT
ISNULL(Sum(Budget), 0) FROM vwPROJECTForecast")

I want the amount in the label to be shown as "10.000" not "10000".
How can I format the amount?

I'm very grateful for help!

// S
 
M

Mark Rae [MVP]

I'm populating a label with an amount like this:

lblBudget.Text = Generic_database_functions.GetValue("SELECT
ISNULL(Sum(Budget), 0) FROM vwPROJECTForecast")

I want the amount in the label to be shown as "10.000" not "10000".
How can I format the amount?


I'm presuming that . is the thousand separator for the culture you're
using...

lblBudget.Text =
Convert.ToDecimal(Generic_database_functions.GetValue("SELECT
ISNULL(Sum(Budget), 0) FROM vwPROJECTForecast")).ToString("#.##0")
 
S

staeri

I'm presuming that . is the thousand separator for the culture you're
using...

lblBudget.Text =
Convert.ToDecimal(Generic_database_functions.GetValue("SELECT
ISNULL(Sum(Budget), 0) FROM vwPROJECTForecast")).ToString("#.##0")

Thank you for the help. Unfortunately I receive error message: "Input
string was not in a correct format".

(Yes, "." is the thousand separator).

// S
 
S

staeri

What datatype does GetValue return...?

It returns a string and it looks like this:

Shared Function GetValue(ByVal strSP As String)
Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlCommand(strSP, myConnection)
myConnection.Open()
Dim strValue As String = myCommand.ExecuteScalar().ToString()
myConnection.Close()

Return strValue
End Function

// S
 
M

Mark Rae [MVP]

It returns a string and it looks like this:

Shared Function GetValue(ByVal strSP As String)
Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlCommand(strSP, myConnection)
myConnection.Open()
Dim strValue As String = myCommand.ExecuteScalar().ToString()
myConnection.Close()

Return strValue
End Function

OK, then. Please try the following and tell me where it fails:

Dim strGetValue As String
strGetValue = Generic_database_functions.GetValue("SELECT
ISNULL(Sum(Budget), 0) FROM vwPROJECTForecast")
Dim decValue As Decimal
decValue = Convert.ToDecimal(strGetValue)
lblBudget.Text = decValue.ToString("#.##0")



BTW, you should really put some exception handling in your function or, at
the very least, use the Using syntax... As it stands, any error in the
ExecuteScaler line is liable to leave your connection open...
 
S

staeri

OK, then. Please try the following and tell me where it fails:

Dim strGetValue As String
strGetValue = Generic_database_functions.GetValue("SELECT
ISNULL(Sum(Budget), 0) FROM vwPROJECTForecast")
Dim decValue As Decimal
decValue = Convert.ToDecimal(strGetValue)
lblBudget.Text = decValue.ToString("#.##0")

BTW, you should really put some exception handling in your function or, at
the very least, use the Using syntax... As it stands, any error in the
ExecuteScaler line is liable to leave your connection open...

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net- Dölj citerad text -

- Visa citerad text -

The problem is solved! Your code was completely correct and it was a
formula with additions containing the converted variables that caused
the error. Thank you for the help!

/ S
 
M

Mark Rae [MVP]

The problem is solved! Your code was completely correct and it was a
formula with additions containing the converted variables that caused
the error. Thank you for the help!

Phew! Thought I was going crazy for a second... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top