Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to validate (empty space) in Decimal values

Posted on 2011-03-22
15
Medium Priority
?
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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
 
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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 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