views:

157

answers:

4

Hi, 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.

+2  A: 

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
I thought the first solution you provided (`int i = Convert. . . . .`) is the same one the OP says he tried.
Alex Essilfie
@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.
Dave Swersky
+4  A: 

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
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.
VP
and if i run the stored procedure from sql server 2008 environment it runs fine. exec stored_procedure_name 7654or any other integer value instead of 7654.
VP
@VP: perhaps something going wrong with your parameter being attached to the call?
p.campbell
let me try to check if i can what you are trying to say. I will write back asap
VP
A: 

1- you know the best answer " The Converter "

2- but if you don't use converter than use any of the above solution

saurabh
why down vote , atleast give me a reason.
saurabh
A: 

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
Ars
No it doesnt allow null. and i am validating that in my c# code before converting it.
VP