locked
TryParse DataKeys RRS feed

  • Question

  • User-1664485818 posted

    Hi folks, looking for some help with the syntax, trying to TryParse a datakey, not really sure how to do this.

    Why validate a datakey if you know that the field is a primary key and that the field will always be an int????

         if (int.TryParse(GridView2.DataKeys[e.RowIndex].Value, out num2)) ;
                {
                    linkVehicleTyresId = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);
                }
    



    Thursday, October 20, 2016 7:43 PM

Answers

  • User753101303 posted

    Hi,

    Correct. If you expect the string to be always a valid integer just use Convert.ToInt32 (and it will just throw if it is not).

    Else if it ever happens, the value will be likely left to 0 and your code will fail later or even worse your app will show some strange behavior (for example no deletion at all will happen or whatever).

    IMO your code should work for the EXACT situation that you expect. It should FAIL (rather than failing later or not doing what you want)  if the situation is not what you expect.

    Similarly I saw once someone searching for a control in an ItemDataBound event. His code failed because he didn't find the control in the grid header. He fixed this by skipping this statement if the control is not found. As a result his code will still run if the control is renamed but then the app won't do anymore this change on the control.

    If instead, the row type is tested, then renaming the control will throw an exception which is good as the app doesn't do any more what is EXPECTED.

    In short wonder what happens next. Does it make something you would expect if you keep running despite the value being not a numeric value?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 20, 2016 8:18 PM
  • User-183374066 posted

    Either you do TryParse like following

    if (int.TryParse(GridView2.DataKeys[e.RowIndex].Value, out num2)) ;
    {
        linkVehicleTyresId = num2;
    }

    and as you said you have int Id then don't use to TryParse. Simply cast it to int

    linkVehicleTyresId = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);

    Hope it helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 20, 2016 9:22 PM

All replies

  • User753101303 posted

    Hi,

    Correct. If you expect the string to be always a valid integer just use Convert.ToInt32 (and it will just throw if it is not).

    Else if it ever happens, the value will be likely left to 0 and your code will fail later or even worse your app will show some strange behavior (for example no deletion at all will happen or whatever).

    IMO your code should work for the EXACT situation that you expect. It should FAIL (rather than failing later or not doing what you want)  if the situation is not what you expect.

    Similarly I saw once someone searching for a control in an ItemDataBound event. His code failed because he didn't find the control in the grid header. He fixed this by skipping this statement if the control is not found. As a result his code will still run if the control is renamed but then the app won't do anymore this change on the control.

    If instead, the row type is tested, then renaming the control will throw an exception which is good as the app doesn't do any more what is EXPECTED.

    In short wonder what happens next. Does it make something you would expect if you keep running despite the value being not a numeric value?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 20, 2016 8:18 PM
  • User-183374066 posted

    Either you do TryParse like following

    if (int.TryParse(GridView2.DataKeys[e.RowIndex].Value, out num2)) ;
    {
        linkVehicleTyresId = num2;
    }

    and as you said you have int Id then don't use to TryParse. Simply cast it to int

    linkVehicleTyresId = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);

    Hope it helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 20, 2016 9:22 PM