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

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

问题描述

我需要将我的 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 Profiler 以检查正在发送/执行的 SQL 语句.

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

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

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