Convert textbox text to integer
Solution 1
Suggest do this in your code-behind before sending down to SQL Server.
int userVal = int.Parse(txtboxname.Text);
Perhaps try to parse and optionally let the user know.
int? userVal;
if (int.TryParse(txtboxname.Text, out userVal)
{
DoSomething(userVal.Value);
}
else
{ MessageBox.Show("Hey, we need an int over here."); }
The exception you note means that you're not including the value in the call to the stored proc. Try setting a debugger breakpoint in your code at the time you call down into the code that builds the call to SQL Server.
Ensure you're actually attaching the parameter to the SqlCommand.
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@ParamName", SqlDbType.Int);
cmd.Parameters["@ParamName"].Value = newName;
conn.Open();
string someReturn = (string)cmd.ExecuteScalar();
}
Perhaps fire up SQL Profiler on your database to inspect the SQL statement being sent/executed.
Solution 2
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.
Solution 3
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 put number in the text box.
- You click on the button or something running the function.
- You see your number just bigger than your number by one in your text box.
:)
Solution 4
int num = int.Parse(textBox.Text);
try this. It worked for me
zack
Updated on July 09, 2022Comments
-
zack almost 2 years
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.
-
Alex Essilfie over 13 yearsI thought the first solution you provided (
int i = Convert. . . . .
) is the same one the OP says he tried. -
zack over 13 yearsI did the same thing. I am still getting the same error. I am sure that its not an integer problem. It is getting converted fine and getting passed to the method (DoSomething() as you mentioned here) that calls the stored procedure. I set a breakpoint at this method and the argument seems to be holding an integer value. The call to the stored procedure however still fails with the same error as above.
-
zack over 13 yearsand if i run the stored procedure from sql server 2008 environment it runs fine. exec stored_procedure_name 7654 or any other integer value instead of 7654.
-
Dave Swersky over 13 years@Alex: It is. The OP had the right method, but wanted to know if the same thing can be done in XAML. It can't.
-
zack over 13 yearsNo it doesnt allow null. and i am validating that in my c# code before converting it.
-
p.campbell over 13 years@VP: perhaps something going wrong with your parameter being attached to the call?
-
zack over 13 yearslet me try to check if i can what you are trying to say. I will write back asap
-
Dominique Mathieu over 3 yearstxtboxname.text must be writen as the fisrt one: txtboxname.Text