Click here to Skip to main content
15,891,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
i am trying to do that : equal text box value into emp class property type int and decimal
C#
txtDept.text = emp.DepID  // string = int 
txtSalary.text = emp.salary // string = decimal 
so ho i can convert that?
Posted
Comments
VJ Reddy 22-May-12 3:01am    
Thank you for accepting the solution :)
Haitham tarek 22-May-12 5:12am    
you are very welcome
i hope to be just 1 % of your experience
i am new to work just 1 week starting work in my company
so i face many problems and my time so tight

Refer this:

http://lokeshbasana.wordpress.com/2009/07/16/asp-net-convert-string-to-date-date-to-string-string-to-intstring-to-doublestring-to-decimalobject-to-intobject-tostringstring-to-byte-array/[^]


C#
string a = "1234″;

int b = Convert.ToInt16(a);



C#
string a = "1234.34″;
decimal b = decimal.Parse(a);
 
Share this answer
 
Comments
VJ Reddy 21-May-12 6:59am    
Good idea. But ToInt16 converts to Short. 5!
sravani.v 21-May-12 7:04am    
ok. Thank you Reddy
Mohamed Mitwalli 21-May-12 7:14am    
5+
sravani.v 21-May-12 7:22am    
Thank you Mitwalli
Mohamed Mitwalli 21-May-12 7:27am    
Your welcome sravani
C#
String to int
use=
Convert.ToInt32(string)

String to Decimal
Convert.ToDecimal(String);
 
Share this answer
 
v2
Comments
VJ Reddy 21-May-12 7:00am    
Good answer. 5!
Mohamed Mitwalli 21-May-12 7:21am    
5+
Haitham tarek 22-May-12 2:46am    
thanks :) +5
Use the ToString method with required format as shown below:
C#
txtDept.Text = emp.DepID.ToString();
txtSalary.Text = emp.salary.ToString("N2");

The question was not clear. If it is required to convert the string representation of the number to its native value then there are several approaches. One option is to use Convert.ToInt32, Convert.ToDecimal etc. methods as these will not throw exception even when the input value is null as shown below:
C#
emp.DepID=Convert.ToInt32(txtDept.Text);
emp.salary=Convert.ToDecimal(txtSalary.Text);
 
Share this answer
 
v2
Comments
sravani.v 21-May-12 7:03am    
My 5!
VJ Reddy 21-May-12 7:05am    
Thank you, sravani :)
Mohamed Mitwalli 21-May-12 7:15am    
5+ for good explanation
VJ Reddy 21-May-12 7:19am    
Thank you, Mohamed :)
Mohamed Mitwalli 21-May-12 7:27am    
your welcome Vj :)
Hi,
You can use emp.DepID.ToString();
txtSalary.text = emp.salary.ToString();
 
Share this answer
 
v2
Comments
VJ Reddy 21-May-12 7:00am    
Good point. 5!
Mohamed Mitwalli 21-May-12 7:16am    
5+ but you should read solution 5 for VJ and his explanation
Try This
C#
txtDept.text =Convert.ToInt32( emp.DepID ) ;// string = int
     txtSalary.text =Convert.ToDecimal( emp.salary); // string = decimal
 
Share this answer
 
Comments
Mohamed Mitwalli 21-May-12 7:16am    
5+
AspDotNetDev 23-May-12 19:15pm    
Why do you keep copying other answers?
Hi ,
Check this
C#
 emp.DepID =Convert.ToInt32( txtDept.text ) ;// string = int
emp.salary  =Convert.ToDecimal( txtSalary.text); // string = decimal


Best Regards
M.Mitwalli
 
Share this answer
 
v2
Comments
VJ Reddy 21-May-12 6:57am    
Good answer. 5! only thing is the txtDept.text and emp.DepId are to be swapped. Similarly in other statement.
Mohamed Mitwalli 21-May-12 7:13am    
Maybe he want bind retrieved data to controls
it can be like this
var emp = (from x in db.emp
select x).FirstOrDefault();
txtSalary.text =Convert.ToDecimal( emp.salary)
VJ Reddy 21-May-12 7:19am    
But I think the final statement may throw the error Cannot implicitly convert type 'decimal' to 'string'
Mohamed Mitwalli 21-May-12 7:26am    
Yes it will throw error if it contains letters like this Convert.ToDecimal("aa"); but if it already digit it will be okay Convert.ToDecimal("10.5");
VJ Reddy 21-May-12 7:35am    
I am thinking that txtSalary.Text is property of string type and a decimal value is being assigned to it.

Thank you :)
Write .ToString in both the statement.

C#
txtDept.text = emp.DepID.ToString();
txtSalary.text = emp.salary.ToString();


To Convert String Int or Decimal Use
C#
Convert.ToInt32(string_name);//To Convert into Int
Convert.ToDecimal(string_name);//To convert into decimal
 
Share this answer
 
v2
Comments
VJ Reddy 21-May-12 7:00am    
Good answer. 5!
bhagirathimfs 21-May-12 7:08am    
Thanks Reddy :)
Mohamed Mitwalli 21-May-12 7:16am    
5+
ya this works for u...

C#
txtDept.text =Convert.ToInt32( emp.DepID ) ;//int
txtSalary.text =Convert.ToDecimal( emp.salary); //decimal
 
Share this answer
 

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