Wednesday, March 28, 2012

string.format Problem

I am trying to fill in a summary in the footer of my grid view but I keep getting an error

Message="Format specifier was invalid."
make sure your method argumements are in the right format

lblsummary.Text = String.Format("Total hrs: {0:d}", hrtotalsTotal)

e.Row.Cells(2).Text = (hrtotalsTotal / 60).ToString(

"d")

I got this format from msdn what is causing this

Hi,

What is your hrtotalsTotal value?

From what I can see, you're trying to convert a double to DateTime presentation format... It does not make any sense. You should use TimeSpan object. And I think your hrtotalsTotal is actually a total number of minutes... anyways, try this:

Dim totalHoursAs Double = 3.4'3 hours 24 minutesDim tsAs TimeSpan = TimeSpan.FromHours(totalHours)Response.Write(ts.ToString())

hrtotalstotal is declared as a decimal and is the sum of a grid view col.

dim hrtotalstotal as decimal

I am just deviding hrstotalstotal/60 to get total hrs. in a decimal format ie 75 min = 1.15 hrs....

nothing to do with any dates or times.

e.Row.Cells(2).Text = (hrtotalsTotal / 60).ToString("d")


ok then.

My example should help you out on this.


Your format specifier is invalid becoz you have declared the variable as decimal and using the integer format specifier.

Try this

lblsummary.Text = String.Format("Total hrs: {0:f}", hrtotalsTotal)
e.Row.Cells(2).Text = (hrtotalsTotal / 60).ToString("f")

Thanks


where can I get a list of the format specifier

Check this linkhttp://blog.g9th.com/2007/01/29/string-formats-in-c.aspx

Thanks

0 comments:

Post a Comment