SmashAndGrab
asked on
Check if NOT an INT - if so, set to 0
Test for NULL, DbNull or empty.
Not one line but two lines (untested and off the top of my head):
IMHO, the verbose method is easier to read and debug. However, they will both produce the same results.
-saige-
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" : "";
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;
IMHO, the verbose method is easier to read and debug. However, they will both produce the same results.
-saige-
ASKER
Use parameterized commands to avoid SQL injection.
ASKER
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_CellValidati ng(object sender, DataGridViewCellValidating EventArgs e)
{
//validating cell
//should only validate certain cells i.e. not the description
if (e.ColumnIndex == 3 || e.ColumnIndex == 7)
{
ValidateTheEntry(e.Formatt edValue.To String());
}
}
public int ValidateTheEntry(string number)
{
int temp;
string result;
if (number != null && !string.IsNullOrEmpty(numb er))
{
if (int.TryParse(number, out temp))
{
result = temp.ToString();
}
else
{
result = "0";
}
}
else
{
result = "";
}
number = result;
return Convert.ToInt32(result);
}
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_CellValidati
{
//validating cell
//should only validate certain cells i.e. not the description
if (e.ColumnIndex == 3 || e.ColumnIndex == 7)
{
ValidateTheEntry(e.Formatt
}
}
public int ValidateTheEntry(string number)
{
int temp;
string result;
if (number != null && !string.IsNullOrEmpty(numb
{
if (int.TryParse(number, out temp))
{
result = temp.ToString();
}
else
{
result = "0";
}
}
else
{
result = "";
}
number = result;
return Convert.ToInt32(result);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.