Else, If statement breaking

The first if statement works but when you try and execute the else statement it breaks on: int SearchUser = Convert.ToInt32(SearchResult.SelectedRow.Cells[1].Text);

protected void SearchResult_SelectedIndexChanged(object sender, EventArgs e)
{

    // open new connection
    SqlConnection connection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    connection1.Open();

    int SearchUser = Convert.ToInt32(SearchResult.SelectedRow.Cells[1].Text);
    int Student = Convert.ToInt32(Session["UserID"]);


    if (SearchResult.SelectedRow.Cells[1].Text != null)
    {

    String PT = "INSERT into PTutors(PersonalTutorID, StudentID) values(@SearchUser, @Student)";
    SqlCommand myCommand = new SqlCommand(PT, connection1);
    myCommand.Parameters.AddWithValue("@SearchUser", SearchUser);
    myCommand.Parameters.AddWithValue("@Student", Student);
    myCommand.ExecuteNonQuery();


    Response.Write("<script type='text/javascript'>");
    Response.Write("alert('Student Assigned to Personal Tutor');");
    Response.Write("document.location.href='ViewAssignedTutors.aspx';");
    Response.Write("</script>");
    }

    else 
    { 

    int SearchAPM = Convert.ToInt32(SearchResult.SelectedRow.Cells[9].Text);

        String PT2 = "INSERT into PTutors(PersonalTutorID, StudentID) values(@SearchAPM, @Student)";
        SqlCommand myCommand = new SqlCommand(PT2, connection1);
        myCommand.Parameters.AddWithValue("@SearchAPM", SearchAPM);
        myCommand.Parameters.AddWithValue("@Student", Student);
        myCommand.ExecuteNonQuery();


        Response.Write("<script type='text/javascript'>");
        Response.Write("alert('Student Assigned to Personal Tutor');");
        Response.Write("document.location.href='ViewAssignedTutors.aspx';");
        Response.Write("</script>");
    }
}

Answers


Try instead of terse

  int SearchAPM = Convert.ToInt32(SearchResult.SelectedRow.Cells[9].Text);

put talkative

  int SearchAPM = 0;

  if (!int.TryParse(SearchResult.SelectedRow.Cells[9].Text, out SearchAPM)) {
    Message.Show(String.Format("\"{0}\" is not an integer value.", 
      SearchResult.SelectedRow.Cells[9].Text));
  }

then, please, have a look at message popped up and debug the routine;


It seems that data you are trying to convert is not in a proper format. As described on Msdn

Try placing a breakpoint on the converts, and see which values you are giving from the cells. Also, how did you determine that you should use field 9 ? Are you sure you are not just selecting the wrong field?

Alternatively you can also encase your converts in a try catch

try{
    int SearchAPM = Convert.ToInt32(SearchResult.SelectedRow.Cells[9].Text);
} catch(FormatException ex)
{
   // Do something to debug/handle
   SearchApm = -1;
   Trace.WriteLine("Oh no, {0} is not correctly formated",SearchResult.SelectedRow.Cells[9].Text);
}

Or to even simplify version of such a try catch could be the try parse which will handle the error internally, giving you a 0 if it fails.


Need Your Help

TYPO3 6.2 beta3 installation complaining about links

php apache installation typo3

I am following the TYPO3 6.2 beta 3 install instructions at https://github.com/TYPO3/TYPO3.CMS/blob/master/INSTALL.md however in the first step of the installation the install tool complains (all are