Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert textbox text to integer

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.

like image 696
zack Avatar asked Aug 23 '10 15:08

zack


2 Answers

int num = int.Parse(textBox.Text);

try this. It worked for me

like image 103
Chameena Hiruni Avatar answered Sep 30 '22 09:09

Chameena Hiruni


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.

like image 23
p.campbell Avatar answered Sep 30 '22 10:09

p.campbell