Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
if (string.IsNullOrEmpty(profileList[i].CurrencyID))
          { boatProfile.CurrencyID = 0; }
          else
 { boatProfile.CurrencyID=int.Parse((profileList[i].CurrencyID)); }

crrencyID is declared aspublic int? currencyID ..value in (profileList[i].CurrencyID="Dkk"
In the else part it is showing the error..""int.Parse((profileList[i].CurrencyID)) threw a sysytem.formatException""
Posted
Comments
Ron Beyer 9-Jan-14 23:38pm    
How do you expect "Dkk" to be turned into an integer?
ahmed ali mohd 9-Jan-14 23:42pm    
im parsing it
is that wrong?
ahmed ali mohd 9-Jan-14 23:42pm    
correct me if im wrong.
Ron Beyer 9-Jan-14 23:49pm    
"dkk" cannot be converted to an integer, that's what the exception is telling you. There's no way that will ever be converted to an integer, parsing or not.

use TryParse[^]

C#
else
        {
            boatProfile.CurrencyID = 0;
            int.TryParse(profileList[i].CurrencyID, out boatProfile.CurrencyID);
        }
 
Share this answer
 
Comments
TrushnaK 10-Jan-14 0:40am    
nice option....
my 5+
Karthik_Mahalingam 10-Jan-14 0:45am    
Thanks Trushnak :)
ahmed ali mohd 10-Jan-14 0:42am    
it is saying that TryParse has invalid arguments
Karthik_Mahalingam 10-Jan-14 0:45am    
try this
int.TryParse(profileList[i].CurrencyID + "", out boatProfile.CurrencyID);
ahmed ali mohd 10-Jan-14 0:50am    
same thing..tryparse has invalid arguments..is there any mscorlib problem?
C#
int j;
bool result = Int32.TryParse("-105", out j);
if (true == result)
    Console.WriteLine(j);
else
    Console.WriteLine("String could not be parsed.");


Refer:http://msdn.microsoft.com/en-us/library/bb397679.aspx[^]

This may help.
 
Share this answer
 
Comments
ahmed ali mohd 9-Jan-14 23:49pm    
sting value caanot be converted to int??
Gitanjali Singh 9-Jan-14 23:53pm    
is this the error you are getting?
ahmed ali mohd 9-Jan-14 23:56pm    
nooo ..im getting int.Parse((profileList[i].CurrencyID)) threw a sysytem.formatException
ahmed ali mohd 9-Jan-14 23:59pm    
it is an easy conversion but dont know y im getting this error..im laughing at myself
TrushnaK 10-Jan-14 0:02am    
debug and check what you get in (profileList[i].CurrencyID) .
try to convert like this:-
boatProfile.CurrencyID=Convert.ToInt16((profileList[i].CurrencyID));


You attempt to cast a numeric string to a numeric data type, following code shows example.

C#
string strInput = "10";
int intResult = 0;
try {
    intResult = Convert.ToInt16(strInput);
}
catch (Exception ex) {
    MessageBox.Show(("Error: " + ex.Message));
}



hope it helps...
 
Share this answer
 
v2
Comments
ahmed ali mohd 10-Jan-14 0:20am    
its not working :(
TrushnaK 10-Jan-14 0:23am    
can you tell me data type of CurrencyID in boatProfile..
ahmed ali mohd 10-Jan-14 0:24am    
public int? CurrencyID { get; set; } declared like this
TrushnaK 10-Jan-14 0:32am    
hi ahemd..
as i given example i demonstrate that if you have value "10" as integer then you can convert to integer using convert.ToInt16 or convert.ToInt32 or int.Tryparse etc..
think that you try to convert "DK" to integer.
either change datatype of CurrencyID to string if it not required in integer or supply integer value as string through (profileList[i].CurrencyID).
Karthik_Mahalingam 10-Jan-14 0:47am    
Hi Trushnak
you have posted VB code..
can you pls change it to C#. as the OP is posted with the label C#
First of all u can't able to cast string of characters to int but can do if string contain value of integer.
below is the code snippet that i have tried and also it's working

C#
int j;
if (int.TryParse(profileList[i].CurrencyID, out j))
    boatProfile.CurrencyID = j;
else
    MessageBox.Show("string can not be coverted to int");
 
Share this answer
 
Comments
ahmed ali mohd 10-Jan-14 0:57am    
ya gud solution ..but j value is displaying as 0
Suk@nta 10-Jan-14 9:58am    
yes..if you try to cast a string of character using 'TryParse()' methode to int then it gives output as 0 but returns false.
so according to my code it will execute else part but output will assign to j.

i unable to understand why u wants to cast a string of characters.
plz reply back..

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