Solved

how to validate (empty space) in Decimal values

Posted on 2011-03-22
15
490 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
remote mysql 8 53
Help With Simple Database Design 7 70
MYSQL responding very slow 3 67
Undefined variable with $_POST in PHP 5 40
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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