I need to convert the text in the textbox of my xaml code to an integer value in C#. I am using .NET 4.0 and Visual Studio 2010. Is there a way to do it in xaml tags itself or do i need to write a converter in C sharp. I tried the following but is not working:
Convert.ToInt32(this.txtboxname.Text)
Any help is much appreciated. Thanks.
This question is related to
c#
sql-server
stored-procedures
type-conversion
If your SQL database allows Null values for that field try using int? value
like that :
if (this.txtboxname.Text == "" || this.txtboxname.text == null)
value = null;
else
value = Convert.ToInt32(this.txtboxname.Text);
Take care that Convert.ToInt32 of a null value returns 0 !
Convert.ToInt32(null) returns 0
int num = int.Parse(textBox.Text);
try this. It worked for me
Example:
int x = Convert.ToInt32(this.txtboxname.Text) + 1 //You dont need the "this"
txtboxname.Text = x.ToString();
The x.ToString()
makes the integer into string to show that in the text box.
Result:
:)
You don't need to write a converter, just do this in your handler/codebehind:
int i = Convert.ToInt32(txtMyTextBox.Text);
OR
int i = int.Parse(txtMyTextBox.Text);
The Text
property of your textbox is a String
type, so you have to perform the conversion in the code.
Source: Stackoverflow.com