Index and length must refer to a location within the string. Parameter name: length

Posted by arv0307-23059 under ASP.NET on 9/4/2013 | Points: 10 | Views : 4191 | Status : [Member] | Replies : 2
Index and length must refer to a location within the string.
Parameter name: length



if (CurrencyData.Count() != 0)
{
foreach (var curr in CurrencyData)
{
decimal DollarVal = 1 / Convert.ToDecimal(curr.NAV012DollarValue);
txtDollarValue.Text = Convert.ToString(DollarVal).Substring(0,5);
}
please suggest me friends




Responses

Posted by: Bandi on: 9/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
try this once....
if (CurrencyData.Count() != 0) 

{
foreach (var curr in CurrencyData)
{
decimal DollarVal = 1 / Convert.ToDecimal(curr.NAV012DollarValue);
string str = Convert.ToString(DollarVal);
txtDollarValue.Text = str.Substring(0,str.Length);
}


Substring(index, Length) means Index = 0, Length=5
If there is no data of length 5 then it will throw error

Decimal to string conversion formats:
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

arv0307-23059, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/4/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
check this
CurrencyData.Count()

may be no data or no index have





Mark as Answer if its helpful to you

arv0307-23059, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response