?
Solved

To Char

Posted on 2005-05-16
20
Medium Priority
?
432 Views
Last Modified: 2010-05-18
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
Comment
Question by:gvector1
  • 7
  • 6
  • 5
  • +1
19 Comments
 
LVL 10

Accepted Solution

by:
smegghead earned 1000 total points
ID: 14014973
you should check it against DBNull.Value before trying to cast it.
0
 
LVL 9

Assisted Solution

by:BurntSky
BurntSky earned 1000 total points
ID: 14015588
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14018777
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:BurntSky
ID: 14020296
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
 

Author Comment

by:gvector1
ID: 14020330
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
 
LVL 10

Expert Comment

by:smegghead
ID: 14020343
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
 
LVL 10

Expert Comment

by:smegghead
ID: 14020349
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
 

Author Comment

by:gvector1
ID: 14020596
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
 
LVL 10

Expert Comment

by:smegghead
ID: 14020621
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
 
LVL 9

Expert Comment

by:BurntSky
ID: 14020633
char c = (char)'0';

But theres really no point in explicitly casting it:

char c = '0';
0
 
LVL 10

Expert Comment

by:smegghead
ID: 14020654
but that will set it to ascii 48.. not to 0

0
 

Author Comment

by:gvector1
ID: 14020693
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
 
LVL 10

Expert Comment

by:smegghead
ID: 14020741
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
 
LVL 9

Expert Comment

by:BurntSky
ID: 14020749
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
 
LVL 9

Expert Comment

by:BurntSky
ID: 14020759
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
 

Author Comment

by:gvector1
ID: 14020795
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
 
LVL 10

Expert Comment

by:smegghead
ID: 14020818
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
 
LVL 9

Expert Comment

by:BurntSky
ID: 14020843
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
 

Author Comment

by:gvector1
ID: 14021658
Thanks again,
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question