HI,
Am getting an error on this line ; it says input string was not incorrect format please help...
int userid = Convert.ToInt32(Selected_List.SelectedItems[0].SubItems[0].Text.ToString());
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Dec 1, 2014, 11:17 PM EST
it means that Selected_List.SelectedItems[0].SubItems[0].Text does not contains a value that can be converted to int.
so user
Int32.TryParse(Selected_List.SelectedItems[0].SubItems[0].Text, out result); if it contains the data which cannot convert to int then it will return false. and no need of .ToString() also there.Michal HabalcikRank #902 · 2.3K reputationPosted Dec 1, 2014, 11:25 PM EST
int result = int.MinValue;
int userid;
int.TryParse(Selected_List.SelectedItems[0].SubItems[0].Text, out result);
if (result != int.MinValue)
userid = result;
vaibhav bhargav96 reputationPosted Dec 1, 2014, 11:34 PM EST
Check what you are getting for Selected_List.SelectedItems[0].SubItems[0].Text.
It may contain something that can not be INT,further
Text property itself of type 'string',.Tostring() is not required so change
Convert.ToInt32(Selected_List.SelectedItems[0].SubItems[0].Text.ToString());
to
Convert.ToInt32(Selected_List.SelectedItems[0].SubItems[0].Text);
Enos128 reputationPosted Dec 1, 2014, 11:42 PM EST
_imageId = userid;
Use of unassigned local variable 'userid'
Michal HabalcikRank #902 · 2.3K reputationPosted Dec 1, 2014, 11:44 PM EST
Try to check what are you actually trying to parse:
Console.WriteLine(Selected_List.SelectedItems[0].SubItems[0].Text)
VulpesRank #55 · 29.2K reputationPosted Dec 2, 2014, 5:01 AM EST
int userid;
bool isValid = int.TryParse(Selected_List.SelectedItems[0].SubItems[0].Text, out userid);
if (isValid)
{
_imageId = userid;
}
else
{
// perhaps issue some warning to user
}
Selva GanapathyRank #560 · 4.3K reputationPosted Dec 2, 2014, 7:49 PM EST
Hi Enos,
You can use either intTryParse or Int16Converter class to achieve this
if(new Int16Converter().IsValid(Selected_List.SelectedItems[0].SubItems[0].Text.ToString()))
{
int userid = Convert.ToInt32(Selected_List.SelectedItems[0].SubItems[0].Text.ToString()
}
else
{
// Message
}
Regards,
Selva Ganapathy K