Check if NOT an INT - if so, set to 0

Hi,


1.
Is there a way that I can check if the value is an INT and if its not set it to ‘0’.  Can I do this in one line of code?

Something like .. (if NOT INT (row.Cells["Reject01"].Value),0)



2. I have a datagrid.  I would only like to loop through the rows that have an entry against them, see image:  Ignore the rows that do not have any entry
SmashAndGrabAsked:
Who is Participating?
 
it_saigeDeveloperCommented:
Answered here: http:/Q_28642985.html#a40685630

-saige-
0
 
ste5anSenior DeveloperCommented:
Test for NULL, DbNull or empty.
0
 
it_saigeDeveloperCommented:
Not one line but two lines (untested and off the top of my head):
int temp;
row.Cells["Reject01"].Value = row.Cells["Reject01"].Value != null && !string.IsNullOrEmpty(row.Cells["Reject01"].Value) ? int.TryParse(row.Cells["Reject01"].Value, out temp) ? temp.ToString() : "0" : "";

Open in new window

Which written verbose means:
int temp;
string result;
if (row.Cells["Reject01"].Value != null && !string.IsNullOrEmpty(row.Cells["Reject01"].Value))
{
	if (int.TryParse(row.Cells["Reject01"].Value, out temp))
	{
		result = temp.ToString();
	}
	else
	{
		result = "0";
	}
}
else
{
	result = "";
}
row.Cells["Reject01"].Value = result;

Open in new window


IMHO, the verbose method is easier to read and debug.  However, they will both produce the same results.

-saige-
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
SmashAndGrabAuthor Commented:
Thanks.

Is there a short method that I could fit in here:
updateCode.PNG
I just wanted to make the code easier.  perhaps a callable function?
0
 
ste5anSenior DeveloperCommented:
Use parameterized commands to avoid SQL injection.
0
 
SmashAndGrabAuthor Commented:
That validating part we were talking about.

I’m trying the event you said about but not having much luck.

1.       I can’t seem to get my ValidateTheEntry() function to return a 0 if the entry is either “” or NULL or string.
2.      I only need to validate certain columns in the datagrid, I don’t need to validate the EAN or the description for instance and yet the column index don’t seem to be logical?



private void dgCleanseData_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            //validating cell
            //should only validate certain cells i.e. not the description
            if (e.ColumnIndex == 3 || e.ColumnIndex == 7)
            {
                ValidateTheEntry(e.FormattedValue.ToString());
            }
        }




public int ValidateTheEntry(string number)
        {
            int temp;
            string result;
            if (number != null && !string.IsNullOrEmpty(number))
            {
                if (int.TryParse(number, out temp))
                {
                    result = temp.ToString();
                }
                else
                {
                    result = "0";
                }
            }
            else
            {
                result = "";
            }
            number = result;
            return Convert.ToInt32(result);
        }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.