0

Converting a sharepoint list item value to a decimal and assigning it to a radrating value using telerik. The radrating value is a decimal and when converting the list item the error occurs.

 RadRating1.Value = Convert.ToDecimal(oList["Average Rating"]);

I've also tried (decimal) but that gives another error message. Any ideas as to why it wont accept the statement?

oList population:

void loadSuggestions() 
{
        SPList olstSuggestions = oWeb.Lists["Suggestions"];
        SPQuery qSuggestions = new SPQuery();
        qSuggestions.RowLimit.Equals(10);
        qSuggestions.Query = string.Format(@"
        <Where>
                <Eq>
                <FieldRef Name='Archived' />
                <Value Type='Choice'>No</Value>
            </Eq>
        </Where>");

        SPListItemCollection oSuggestionCollection = olstSuggestions.GetItems(qSuggestions);
        int count = 1;
        foreach (SPListItem oList in oSuggestionCollection)
        {<above statement>...}
}
rdmptn
  • 5,413
  • 1
  • 16
  • 29
matt
  • 67
  • 1
  • 10
  • what is the value of an oList["Average Rating"] object for an example conversion? – Nyra Oct 08 '14 at 15:37
  • Check in debug what is the exact value of oList["Average Rating"] ? Maybe its decimal separator is different than your current culture's one? – Szymon Knop Oct 08 '14 at 15:38
  • 1.5 is an example of average rating, would that conflict with the current culture? – matt Oct 08 '14 at 15:45
  • @matt Try this out: `decimal.Parse(oList["Average Rating"], new NumberFormatInfo() { NumberDecimalSeparator = "." });` – Szymon Knop Oct 08 '14 at 15:53
  • the exact error is in the title 'input string was not in correct format'. cant post the stack trace as its too long. ok ill try that now quickly. – matt Oct 08 '14 at 15:53
  • oops missed that in title- can you edit your post and put more code in so we can see the oList and how it's populated? – Nyra Oct 08 '14 at 15:57
  • that's alright ive added the procedure and @Knp the code gives an error stating the best overloaded method match for decimal.parse(string, system.iformatprovider) has some invalid arguments. – matt Oct 08 '14 at 16:08
  • Does [this link](http://stackoverflow.com/questions/23130554/system-formatexception-input-string-was-not-in-a-correct-format-on-converting) help at all? – Nyra Oct 08 '14 at 16:13
  • if it helps the troubleshooting tips suggests 'when converting a string to datetime, parse the string to take the date before putting each variable into the datetime object'. Does this confirm it's confusing the '.' in the sharepoint list and not associating it with a decimal type? – matt Oct 08 '14 at 16:20
  • wait what datetime? datetime will come through in various formats but a common one is `dd/MM/yyyy hh:mm:ss.ttt` Are you trying to convert a string that looks like this `"1.5"` or one that looks like something like this `"10/08/2014 12:29:01.500"` – Nyra Oct 08 '14 at 16:29
  • its 1.5 but I think it's more of a matter of making the culture settings correct, its not a date it's set to a calculation in SharePoint if that gives any insight – matt Oct 08 '14 at 16:35
  • Where did you get this then? `'when converting a string to datetime, parse the string to take the date before putting each variable into the datetime object'`? Also have you tried Knp's solution? We also still need to see the declaration of oList – Nyra Oct 08 '14 at 17:06
  • it is an error in visual studio when I added the solution from Knp. Ive tried @Knp's solution but it gives me the error which I previously commented with. The SPListItem oList is only used within the foreach loop. – matt Oct 09 '14 at 07:59
  • Have you tried my answer below? It has changed a bit from the one in comment. What version of .NET are you using? – Szymon Knop Oct 09 '14 at 08:16
  • no I haven't yet, unable to access the server should be available soon and ill let you know – matt Oct 09 '14 at 08:21

1 Answers1

0

Please try the following:

 Convert.ToDecimal(oList["Average Rating"], new NumberFormatInfo() { NumberDecimalSeparator = "." });
Szymon Knop
  • 509
  • 1
  • 6
  • 16