Shadouspan
Shadouspan

Reputation: 263

Can't Convert Char in ASP.NET C#

I have a "phone" area in my database and data type is char(11).

I need to make a conversion in ASP.NET C# as below:

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

but I get an error:

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.

How can I resolve this error?

Upvotes: 2

Views: 464

Answers (2)

Maddy
Maddy

Reputation: 774

char.Parse shall take a string of length 1 and shall return a char value. If you want to convert your textboxt content to char array you can do like below:

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

Upvotes: 1

Racil Hilan
Racil Hilan

Reputation: 25351

You don't need to convert it. The database CHAR type is a string type. The only difference is that the CHAR length is fixed (in your case 11 characters).

So just send your string as is:

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

Note: The database CHAR type is not the same as the char type in c#, which is used to store one single character.

Upvotes: 1

Related Questions