我的数据库中有一个“电话”区域,数据类型为 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/