2

I have a simple problem,

dtERP value = 2885,78,89,78A;

for (int i = 0; i < dtERP.Rows.Count; i++)
{
    for (int j = 0; j < dtERP.Columns.Count; j++)
    {
        object field = dtERP.Rows[j]["QTY"];
        if (field != int)
        {

        }
    }
}

How can I validate my field if the value is string?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
iMemew
  • 59
  • 7
  • 1
    Please only add relevant tags to your question. Also, your current code doesn't make sense (given the initial value of dtERP, it can't be a `DataTable`). What does "how can I validate my field if the value have a string character" mean? – ProgrammingLlama Feb 13 '20 at 03:01
  • https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is – TheGeneral Feb 13 '20 at 03:05
  • uhm. something like ``if (Convert.ToInt32(field) != true) {error}`` – iMemew Feb 13 '20 at 03:07

2 Answers2

3

You can use the is operator. Something like this:

if(field is string)
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • Do note that if the original value is `"7"`, it is still a string - a database column cannot store both strings and numbers. But this particular value may be *converted* to an int – Hans Kesting Feb 13 '20 at 10:57
0

You can do a .GetType() on the variable/instance.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Djs75
  • 5
  • 2