I have an asp.net griedview that includes a radio button. My application can sucessfully retrieve data and display it in the gridview. But when I select the radio button that corresponds to the row I want to delete and click the delete button, I get the following error:
"Input string was not in a correct format."
And the applications highlights the following code (that I think is the one that originated the error):
Id = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[2].Text);

The gridview contains items templates: the first with label control that displays the row id, the second with textbox control that displays the file name and the last contains radio button control.

The code "behind" the delete button looks like this:

int Id = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            RadioButton rb = (RadioButton)row.FindControl("RadioButton1");
            if (rb.Checked)
            {
                Id = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[2].Text);
                cmd = new SqlCommand("deleterow", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@id", SqlDbType.Int);
                cmd.Parameters["@id"].Value = Id;
                con.Open();
                cmd.ExecuteNonQuery();
                Label1.Visible = true;
                Label1.Text = "Successfully Deleted";
                BindGrid();
}
}

The bind method looks like this:

 private void BindGrid()
    {
        string strQuery = "select id, Name from tblFiles";
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd = new SqlCommand(strQuery);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }

The page_load event contains the following code:

if (!IsPostBack)
        {
            BindGrid();
        }

Please check the column/cell value. I think the 2nd cell has non-numeric.

If the ID is at 1st cell then try,

Id = Convert.ToInt32(row.Cells[0].Text);

or change index of Cells to 1 if ID is at second cell and so on

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.