11

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.

zack
  • 7,115
  • 14
  • 53
  • 63
  • I forgot to mention that I am using SQL server 2008 in the backend. I need to use this conversion for my sql server field which is of the type "int". I hope that I am clear. Thanks again. – zack Aug 23 '10 at 15:10
  • 1
    What do you mean it's not working? Who is consuming this data? Are you calling a stored proc, or need to call a repo method with an int? – p.campbell Aug 23 '10 at 15:10
  • I mean I am passing this int as an argument to a stored procedure in sql server 2008. But I am getting this error "Procedure or function 'name_of_SP' expects parameter '@par_name', which was not supplied." – zack Aug 23 '10 at 15:13
  • Check the spelling of your parameter name. – adam0101 Aug 23 '10 at 15:56
  • Yup I did that as I know that its case sensitive and all. And FYI here is how I call the SP from my C#. public string my_method_name(int my_int_value) { return (string)this.ExecScalar("dbo.usp_some_name_here", this.CreateParameter("@argument_name",my_int_value)); } – zack Aug 23 '10 at 16:10
  • What's the signature of your stored procedure? What statements are within? Please edit your question with these details. – p.campbell Aug 23 '10 at 16:14
  • Does not allow null. Length of that field is 4. Here is how I call it: int tmpvarint; if (int.TryParse(this.txtboxname.Text, out tmpvarint)) { this.my_method_name(tmpvarint); } – zack Aug 23 '10 at 16:14
  • And again FYI. exec store_procedure_name 6574 (or any other int value here) statement works perfectly fine in sql server 2008 environment. I dont understand whats wrong with the call to the procedure from my C# code. – zack Aug 23 '10 at 16:27

5 Answers5

24

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.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • I 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 Aug 23 '10 at 16:03
  • and 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. – zack Aug 23 '10 at 16:05
  • @VP: perhaps something going wrong with your parameter being attached to the call? – p.campbell Aug 23 '10 at 16:40
  • let me try to check if i can what you are trying to say. I will write back asap – zack Aug 23 '10 at 17:53
9

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.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
2

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.

:)

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Tomer Gan
  • 21
  • 1
1
int num = int.Parse(textBox.Text);

try this. It worked for me

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

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
Arslan
  • 569
  • 1
  • 4
  • 14