Home > Software engineering >  Cannot convert string to int
Cannot convert string to int

Time:10-03

var result = 0;
try
{
    var MaxID = GridView1.Rows.Cast<GridViewRow>()
                .Max(r => Convert.ToInt32(r.Cells["Id"].Value));
    result = (MaxID   1);
}
catch (Exception ex)
{
    Label6.Text ="Err "   ex ;
}

The Error

How can i fix that error? I am new to ASP.NET

CodePudding user response:

Something like this would help you:

var result = gridView1.Rows.Cast<GridViewRow>()
            .Select(r =>
            {
                _ = int.TryParse(r.Cells["Id"].Value?.ToString(), out var numeric);
                return numeric;
            }).Max();

CodePudding user response:

You can try with a Parse() method

var result = 0;
        try
        {
            var MaxID = GridView1.Rows.Cast<GridViewRow>()
                        .Max(r => Int32.Parse(r.Cells["Id"].Value));
            result = (MaxID   1);
        }
        catch (Exception ex)
        {
            Label6.Text ="Err "   ex ;
        }
  • Related