Click here to Skip to main content
16,008,010 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi All

C#
string UserId = comboBox1.SelectedValue.ToString();
        int a = Convert.ToInt32(UserId);


I am trying to convert the string to int .
I am getting this error "Input string was not in a correct format."

Please can anyone let me know is this the right way i am doing.


Many Thanks


suman
Posted

This would suggest to me that the userid value is not numerical.

I would have a look at int.TryParse[^]
C#
int numberValue = 0;
if(int.TryParse(UserId, out numberValue) == true)
{
  //then do what you need to do with number value.
}
 
Share this answer
 
Comments
babli3 2-Sep-14 11:26am    
Hi Thanks.
I tried this earlier , its not working either.

userid is (int) in this format 0x0000000000000576
Simon_Whale 2-Sep-14 11:32am    
I would have a read of this http://msdn.microsoft.com/en-us/library/bb311038.aspx
Sergey Alexandrovich Kryukov 2-Sep-14 12:48pm    
OP will get lost in this MSDN article.
It can be this: http://msdn.microsoft.com/en-us/library/system.int32.parse%28v=vs.110%29.aspx
—SA
Sergey Alexandrovich Kryukov 2-Sep-14 12:45pm    
Use hexadecimal format.
Please see Solution 3.
—SA
Sergey Alexandrovich Kryukov 2-Sep-14 12:45pm    
5ed.
—SA
1.You should check in debug the value of comboBox1.SelectedValue, maybe when you init your control you forgot to set a default selected value;

2.You should manage also the case when no value from the list is selected, and in that case comboBox1.SelectedValue will have as value the Empty string, and because your int ins in hexa format (new info that you did not added in your initial question after I gave you my first version of the solution) you should use int.Parse like in the next example:
C#
int a = 0;
string UserId = comboBox1.SelectedValue.ToString();
if(UserId.Length > 0) //Test for empty string!
{
   a = int.Parse(UserId,  NumberStyles.AllowHexSpecifier);
}
 
Share this answer
 
v3
 
Share this answer
 
Comments
Raul Iloc 2-Sep-14 14:02pm    
I got a negative vote to my solution, but my first version was based on the info that I have in the original question, so there was no info about hexa format, so if you vote me negative it will be nice to undo!
Sergey Alexandrovich Kryukov 2-Sep-14 14:26pm    
I just checked it up and see that it was my vote. (By the way, 2 is the positive number, not negative :-) I don't want to check it up, but I'm nearly sure that the version of your answer I saw at that moment was quite inappropriate. Your last version appeared only 20 minutes later and it is good, but it appeared only after OP's clarification (hex format) and my answer. I have no problem to up-vote it (mostly because it's still possible that I was wrong in first place), but I would advise you to be more careful. If I make a mistake in my answer and then see correct answer, I usually prefer removing mine.
—SA
Raul Iloc 3-Sep-14 1:07am    
1.Thank you SA!
2.As I said above, my first version of the solution was based on the info that I had at that point in time.
3.You have my vote.
Sergey Alexandrovich Kryukov 3-Sep-14 1:14am    
Thank you, but I hope you don't consider voting as something reciprocal. We should freely access out opinions, not trying to do reciprocal favor or revenge (which some out members do practice, which is very sad and dishonest).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900