Solved

how to validate (empty space) in Decimal values

Posted on 2011-03-22
15
486 Views
Last Modified: 2012-05-11
Hi,

In my code i was trying to validate empty spaces for a decimal value field,but still am not able to success.Please some one correct me the error.

string test = System.Convert.ToString(item.FTEPercentage);
 if(!string.IsNullOrEmpty(test))

            {
                  if(item.FTEPercentage!=0)
                    {
                        cmd.Parameters.Add("@Fte", SqlDbType.Char);
                        cmd.Parameters["@Fte"].Direction = ParameterDirection.Input;
                        cmd.Parameters["@Fte"].Value = item.FTEPercentage / 100;
                    }
                  else
                   {    cmd.Parameters.Add("@Fte", SqlDbType.Char);
                        cmd.Parameters["@Fte"].Direction = ParameterDirection.Input;
                        cmd.Parameters["@Fte"].Value = DBNull.Value;
                    }
                }
                else
                {
                    cmd.Parameters.Add("@Fte", SqlDbType.Char);
                    cmd.Parameters["@Fte"].Direction = ParameterDirection.Input;
                    cmd.Parameters["@Fte"].Value = DBNull.Value;

Open in new window

0
Comment
Question by:Sha1395
  • 7
  • 7
15 Comments
 
LVL 4

Expert Comment

by:qasim_md
ID: 35187580
I can think of checking it for '' or ' ' or NULL
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35187598
I'm not sure I understand your question here, but it looks as though you are testing if the string "test" is null after setting it = to item.FEPercentage. I think you should be testing if item.FEPercentage is null with the following if statement:

IF(item.FEPercentage != null)
{
//the rest of your code here, then your string "Test"should never be null
}
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35187605
Oops sorry about the caps for IF ( I was in SQL mode)


if (item.FEPercentage != null)
{
//the rest of your code here, then your string "Test"should never be null
}
0
 

Author Comment

by:Sha1395
ID: 35187833
am getting "SPACE" for item.FEPercentage. And more over this is decimal value.

0
 

Author Comment

by:Sha1395
ID: 35187863
in my earlier condition i checked like below

IF((item.FEPercentage != null)&&(item.FEPercentage != 0))

Am not sure how to validate empty spaces, i know if i validate string then

IF(!string.isnullorEmpty(item.FEPercentage)) but as i said earlier this is decimal value.
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35187939
It looks like the item.FEPercentage is being populated with either an empty string or a single space.
Try removing all empty spaces with the replace command:
string test = item.FEPercentage.Replace(" ", "");

and then test for an empty string with:
if(test.Length > 0)
{

}
0
 

Author Comment

by:Sha1395
ID: 35188043
that's a good catch mkorbin,i will try yours ,hope it work and another thing i can't store as string becoz its a decimal value.
Any way i will try and let you know,thanks a lot.

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Expert Comment

by:mkobrin
ID: 35188315
If you need to store the value as a decimal, then once you are happy that there is data you can do the following:
decimal myDecimalValue = 0;
if(test.Length > 0)
{
myDecimalValue = Convert.ToDecimal(test);
}
0
 

Author Comment

by:Sha1395
ID: 35188381
i think i confused .

Already the value am getting is Decimal.

Item.FtePercentage is a decimal value,so as per your idea.


Decimal myDecimalvalue=0
myDecimalvalue= (item.ftepercentage).replace(" ","")

if(myDecimalValue.Length>0)

then i will put my condition here

Correct me if am wrong
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35188421
If the value you are getting is a decimal then it should always have a value unless it is null (if it has not been instanciated).

so:

if (item.FtePercentage != null)
{
//item.FtePercentage will always have a value (even if it is 0)
}

Are you sure that item.FtePercentage is a decimal and not a sting (or object) that you are popluating from a decimal that may be null (that is how it may contain a space).
0
 

Author Comment

by:Sha1395
ID: 35188443
i tried like

String FTE= item.ftepercentage (it throws exception can't store Decimal to string)

And when i debug this value its a white space.

Even my actual code i was validating

if (item.FtePercentage != null)||(item.FtePercentage != 0) but it didn't catch so it throws exception when it pass executenonquery()
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35188501
try this:
String FTE= item.ftepercentage.ToString();

The fact that you are exceuting a nonquery makes me think that you are trying to pass this value as a parameter.

Please supply the entire block of code for this query, perhaps I will be able to see what is wrong for you.
Regards,
Mike
0
 

Author Comment

by:Sha1395
ID: 35188528
Thanks a lot for your response Mike

string test = System.Convert.ToString(item.FTEPercentage);
 if(!string.IsNullOrEmpty(test))

            {
                  if(item.FTEPercentage!=0)
                    {
                        cmd.Parameters.Add("@Fte", SqlDbType.Char);
                        cmd.Parameters["@Fte"].Direction = ParameterDirection.Input;
                        cmd.Parameters["@Fte"].Value = item.FTEPercentage / 100;
                    }
                  else
                   {    cmd.Parameters.Add("@Fte", SqlDbType.Char);
                        cmd.Parameters["@Fte"].Direction = ParameterDirection.Input;
                        cmd.Parameters["@Fte"].Value = DBNull.Value;
                    }
                }
                else
                {
                    cmd.Parameters.Add("@Fte", SqlDbType.Char);
                    cmd.Parameters["@Fte"].Direction = ParameterDirection.Input;
                    cmd.Parameters["@Fte"].Value = DBNull.Value;

Open in new window


am passing this parameters to my store proc and when it try to execute because of this white space it throws exception

Object reference not set to an instance of an object.

If still you want the whole code block i will post for you
0
 
LVL 7

Accepted Solution

by:
mkobrin earned 500 total points
ID: 35188771
OK, Try this:

string test = System.Convert.ToString(item.FTEPercentage).Replace(" ", "");
            if (!string.IsNullOrEmpty(test))
            {
                if (item.FTEPercentage != 0)
                    cmd.Parameters.Add(new SqlParameter("@Fte", SqlDbType.Char)).Value = (item.FTEPercentage / 100).ToString();
                else
                    cmd.Parameters.Add(new SqlParameter("@Fte", SqlDbType.Char)).Value = DBNull.Value;
            }
            else
                cmd.Parameters.Add(new SqlParameter("@Fte", SqlDbType.Char)).Value = DBNull.Value;

Open in new window

0
 

Author Comment

by:Sha1395
ID: 35196773
hi Mike,

i was almost tried all the stuff,still i couldn't able to fix this error.now i gotta doubt this is comes from some where else (i don't know yet just guess).

Anyway i will keep trying tomm with Trim,let see how it goes,now i accept this as a sln.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now