c# - 无法在 ASP.NET C# 中转换字符

标签 c# asp.net webforms char

我的数据库中有一个“电话”区域,数据类型为 char(11)

我需要在 ASP.NET C# 中进行如下转换:

char PhoneNumber = char.Parse((item.FindControl("TxtPhoneNumber") as TextBox).Text);

但我收到错误:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: String must be exactly one character long.

如何解决此错误?

最佳答案

char.Parse 应采用长度为 1 的字符串并返回 char 值。如果您想将文本框内容转换为字符数组,您可以执行以下操作:

string PhoneNumberstr = (item.FindControl("TxtPhoneNumber") as TextBox).Text);
char[] PhoneNumber = PhoneNumberstr.ToCharArray();

关于c# - 无法在 ASP.NET C# 中转换字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48242578/

相关文章:

c# - 将 TFS checkin 策略升级到 VS2015 时出现问题

c# - Flickr API 回调取消操作 C#

c# - 列出网络中所有可用的服务器

asp.net - Mono + apache2 = 服务暂时不可用 (503)

c# - 在 WebBrowser Windows Phone 中订阅回调函数 javascript

c# - 如何使用 Application.Resources 中定义的 DataTemplate?

asp.net - 超链接的字体大小问题

c# - 无法访问 asp.net 中对应的 .cs 文件

asp.net - asmx 网络服务不读取 postdata

c# - 如何阻止用户在 .net 中手动输入下载网址