Solved

how to validate (empty space) in Decimal values

Posted on 2011-03-22
15
489 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Suggested Solutions

Title # Comments Views Activity
Mysql sync between 3-4 mysql db 4 42
MySQL Grouping 2 48
insert into database through form with dynamic fields. 2 40
Mysql Left Join Case 10 70
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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