Convert text field text to integer

I need to convert text in the text box of my xaml code to an integer value in C #. I use .NET 4.0 and Visual Studio 2010. Is there a way to do this in the xaml tags themselves or do I need to write a converter in C sharp. I tried the following but did not work:

Convert.ToInt32(this.txtboxname.Text)

Any help is greatly appreciated. Thank.

+5
source share
5 answers

Suggest doing this in your code before submitting it to SQL Server.

 int userVal = int.Parse(txtboxname.Text);

Perhaps try to parse and optionally inform the user.

int? userVal;
if (int.TryParse(txtboxname.Text, out userVal) 
{
  DoSomething(userVal.Value);
}
else
{ MessageBox.Show("Hey, we need an int over here.");   }

, , , . , SQL Server.

, 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();        
}

, SQL Profiler / SQL.

+14

, /codebehind:

int i = Convert.ToInt32(txtMyTextBox.Text);

int i = int.Parse(txtMyTextBox.Text);

Text - String, .

+7

:

int x = Convert.ToInt32(this.txtboxname.Text) + 1 //You dont need the "this"
txtboxname.Text = x.ToString();

x.ToString() , .

:

  1. .
  2. - .
  3. , .

:)

+2

SQL Null , int? value :

if (this.txtboxname.Text == "" || this.txtboxname.text == null)
     value = null;
else
     value = Convert.ToInt32(this.txtboxname.Text);

, Convert.ToInt32 0!

Convert.ToInt32(null) returns 0
0
int num = int.Parse(textBox.Text);

.

0

Source: https://habr.com/ru/post/1761133/


All Articles