Convert textbox text to integer

前端 未结 5 1545
感情败类
感情败类 2020-12-09 06:28

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

相关标签:
5条回答
  • 2020-12-09 07:09
    int num = int.Parse(textBox.Text);
    

    try this. It worked for me

    0 讨论(0)
  • 2020-12-09 07:13

    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.

    0 讨论(0)
  • 2020-12-09 07:17

    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:

    1. You put number in the text box.
    2. You click on the button or something running the function.
    3. You see your number just bigger than your number by one in your text box.

    :)

    0 讨论(0)
  • 2020-12-09 07:22

    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
    
    0 讨论(0)
  • 2020-12-09 07:32

    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.

    0 讨论(0)
提交回复
热议问题