0

I want to update a row which I have checked and reload the gridview again.

enter image description here

I update it using a button and a checkbox for selecting the row.

protected void btnProceed_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
        if (chkRow.Checked)
        {
            con.Open();
            cmd = new SqlCommand(@"UPDATE JobQuotations1
                                    SET TransactionStatus = @Done
                                    WHERE TransactionID = @Tid
                                    AND TransactionNum = @num", con);

            cmd.Parameters.AddWithValue("@Done", "Done");
            cmd.Parameters.AddWithValue("@Tid", GridView1.SelectedRow.Cells[1].ToString());
            cmd.Parameters.AddWithValue("@num", GridView1.SelectedRow.Cells[2].ToString());

            cmd.ExecuteNonQuery();
            con.Close();
            LoadDataGrid();
        }
    }
}

When I clicked the button nothing is happening.

1
  • Have you try using debug? Is there any request to your server? Commented Jun 1, 2016 at 10:57

3 Answers 3

2

Replace this

GridView1.SelectedRow.Cells[1].ToString()
GridView1.SelectedRow.Cells[2].ToString()

with this

GridView1.SelectedRow.Cells[1].Text
GridView1.SelectedRow.Cells[2].Text

You want the Text of row cell, while ToString will return the type info of cell type.

Also, the correct way should be to get the cell value of your current row like this inside the loop since you already have the current row

row.Cells[1].Text
row.Cells[2].Text
Sign up to request clarification or add additional context in comments.

2 Comments

tried your code now I'm getting this An exception of type 'System.NullReferenceException' occurred in MejShop.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.
For that you have to post your grid view markup. Edit your question and put grid view markup then we can find something
0
cmd.Parameters.AddWithValue("@Tid", row.Cells[1].Text);
cmd.Parameters.AddWithValue("@num", row.Cells[2].Text);

Modify your code where you are adding parameters. Also check the query which is sent to database server through debugging. Try running the same query on database server directly and see if any rows are affected.

Comments

0

Use

   foreach (DataGridViewRow item in GridView.Rows)
           {    
             if (Convert.ToBoolean(item.Cells["ColumnCheckBoxName"].Value) == true)
                  {
                     long DocId = (long)item.Cells[0].Value;
                  }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.