• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

To Char

I have a line of code that is pulling a value from a char column in my database and loading it into a class member that is defined as a char.  When pulled from the database it is pulled as an object, so I have to cast it back to a char as I load it into the class member.  When I attempt to do that with an empty value, I get the following error :

Additional information: String must be only one character long.

How can I handle blank value when trying to cast from a object to a char????

Thanks,
Kendal
0
gvector1
Asked:
gvector1
  • 7
  • 6
  • 5
  • +1
2 Solutions
 
smeggheadCommented:
you should check it against DBNull.Value before trying to cast it.
0
 
BurntSkyCommented:
You could try something like this:

char c = dataRow["character"] == DBNull.Value || dataRow["character"].ToString().Length == 0 ? ' ' : (char)dataRow["character"];

I don't know how the Convert.ToChar() method handles empty characters but you could give it a shot too:

char c = Convert.ToChar(dataRow["character"]);
0
 
Bob LearnedCommented:
I would assume that if the text is not 1 character, then use a string type instead of a character.  Or, use Substring() to get the first character, and then stored it in a character variable.

Bob
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
BurntSkyCommented:
Bob, my guess was that by not being 1 character, it was 0 characters.  But I will definitely agree with you; if more than one character is being returned, it's a string.  You should use something like this:

char c = dataRow["someString"].ToString()[0];
0
 
gvector1Author Commented:
I will have to accept BurntSkys reply as it works excellent.  One alteration had to be made though:

Instead of (char)dataRow["character"];
It had to be Convert.ToChar(dataRow["character"]);

Thanks,
Kendal
0
 
smeggheadCommented:
If your cast usually works, but doesn't when there is a null value, then you should only need...

if (dr["mychar"]==DBNull.Value) return (char)0;
else return (char)dr["mychar"];

Smg.
0
 
smeggheadCommented:
gvector,

Wasn't I the first one to suggest checking the value against DBNull.Value ?? why do the points go to someone else ??

Smg.
0
 
gvector1Author Commented:
When trying to cast to a char like this : (char)0
I get an invalid cast error.

As far as the points, I was not only basing it on the DB.Null comparison, but also the length comparison and the possible return values.  Since the DB.Null does have a factor in the solution, I will make a request for the points to be split.

Kendal
0
 
smeggheadCommented:
Ok, thanks.. sorry to be a pain.

PS. I've just tried the following line, and didn't get an error.

char test=(char)0;

Smg.
0
 
BurntSkyCommented:
char c = (char)'0';

But theres really no point in explicitly casting it:

char c = '0';
0
 
smeggheadCommented:
but that will set it to ascii 48.. not to 0

0
 
gvector1Author Commented:
I am using a static routine for this code like follows

public static char CharValue(object obj)
            {
                  return (obj == DBNull.Value || obj.ToString().Length == 0 ? ' ' : Convert.ToChar(obj));
            }

It works until I attempt to change the Convert.ToChar(obj) to (char)obj, it then returns an error of "Invalid Cast".
0
 
smeggheadCommented:
what is the actual data type coming back from the database ?? can you debug your app and do a 'quick watch' on the obj variable.. it will tell you what the underlying type is.. if it's not char, then you won't be able to cast, you'll have to use Convert.

Smg.
0
 
BurntSkyCommented:
ohhh... try this then:

char c = '\0'

gvector1, is this a runtime error?  try comparing it to the aforementioned null character too:

return obj == DBNull.Value || obj.ToString().Length == 0 || obj == '\0' ? ' ' : (char)obj;
0
 
BurntSkyCommented:
smg makes a good point.  I don't know how well the SQL char datatype and the .NET char datatype match up.  There could me an anomoly that causes some incompatibility issues.  ADO.NET is crap when it comes to handling stuff like this.
0
 
gvector1Author Commented:
It is a MySql char datatype and yes it is a runtime error.  I don't have to compare to '\0'.  Using the aformentioned method above works, it just gives me an error if I change the code to cast instead of the Convert.  It works okay, I just thought this was a good subject and it would be nice to try and figure out why the cast will not work.
0
 
smeggheadCommented:
It might be coming back as a byte or integer or something else. The only way you'll know is to look at the object variable.

Smg.
0
 
BurntSkyCommented:
Yeah, I don't understand why the explicit cast doesn't work in this situation.  Like I said, ADO.NET is crap when it comes to dealing with types and nulls.
0
 
gvector1Author Commented:
Thanks again,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now