Solved

how to validate (empty space) in Decimal values

Posted on 2011-03-22
15
492 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
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

628 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