0

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();
        }

Edited by __avd: Added [code] tags.

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by __avd
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.