转换文本框的文本为整 [英] Convert textbox text to integer

查看:121
本文介绍了转换文本框的文本为整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在我的XAML代码文本框中的文本转换为C#的整数值。我使用.NET 4.0和Visual Studio 2010中有没有办法做到这一点在XAML标记本身或者我需要用C来写尖锐的转换器。我尝试以下,但不工作:

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.

推荐答案

建议发送到SQL Server之前,你这样做的代码隐藏。

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.");   }

您注意到表示异常,你不包括在调用存储过程中的值。试着在你来电垂成构建调用SQL Server的代码的时间在你的代码中设置一个断点调试器。

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.

确保您实际上是附加参数给SqlCommand。

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

也许火了SQL事件探查器数据库来检查SQL语句发送/执行。

Perhaps fire up SQL Profiler on your database to inspect the SQL statement being sent/executed.

这篇关于转换文本框的文本为整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆