locked
Condition to find string to number RRS feed

  • Question

  • User-543160537 posted

    Hi,

    I am using LINQ to fetch data from excel and convert to XML, but i need to find one of the column value is string or number and then change the object value.

      DataTable EventDetails = (from DataRow dr in SelectedRows.Rows
                                                      where dr["Event Id"].ToString() == item.ToString()
                                                      select dr).CopyToDataTable();
    
    
      if (number)
      {
       Int64 evtId = Int64.Parse(EventDetails.Rows[0]["Event Id"].ToString(), NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);
     }
      else
      {
        string evtId = EventDetails.Rows[0]["Event Id"].ToString(); 
    }

    Number will only number ex; 1234456777 and string may consist ex: abc, abc123, abc_123

    Please advice

    Thanks
     

    Saturday, October 10, 2015 9:46 PM

Answers

  • User2103319870 posted

    but i need to find one of the column value is string or number and then change the object value.

    You can try using the TryParse method to check if you have valid number like  given below

    Int64 number;
                //Convert the number to integer using Tryparse if its a valid number then result will be true and converted value is assigned to number
                bool result = Int64.TryParse(EventDetails.Rows[0]["Event Id"].ToString(), out number);
                //Check the conversion result
                if (result)
                {
                    Int64 evtId = number
                }
                else
                {
                    string evtId = EventDetails.Rows[0]["Event Id"].ToString();
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 10, 2015 10:18 PM