0

I have this methode to loop through if the data in my gridview is checked or not, if they are checked they should be inserted into the database. As you can see the only data I want to insert from the gridview is the text from cell[2] in the gridview and it's in the name of @HeaderName

        using (SqlConnection con = new SqlConnection("my con string"))
        {
            con.Open();
            foreach (GridViewRow row in GridViewConsNames.Rows)
            {
                CheckBox myCheckBox = row.FindControl("myCheckBox") as CheckBox;
                if (myCheckBox.Checked)
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Accounting_Consolidation_ServiceOutputs(Cusomer_Name, Service_Name, Header_Name, Sort_Postion, Rename_to, Value) Values(@CustName,@ServiceName,@HeaderName,@Sort,@Rename,@Value)", con))
                    {
                        //cmd.Parameters.AddWithValue("PersonId", Convert.ToInt32(GridViewConsNames.DataKeys[row.RowIndex].Value));
                        cmd.Parameters.AddWithValue("@CustName", TextBoxCustName.Text);
                        cmd.Parameters.AddWithValue("@ServiceName", DropDownListServicesAvailable2.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@HeaderName", "Name of the checked cells from my gridview");
                        cmd.Parameters.AddWithValue("@Sort", null);
                        cmd.Parameters.AddWithValue("@Rename", "");
                        cmd.Parameters.AddWithValue("@Value", null);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }

I have tried with this

 GridViewRow row = GridViewConsNames.SelectedRow; 
 string headername = row.Cells[2].Text;

But this seems not working

3
  • Explode? you can look at the AddWithValue = @HeaderName I need to put it there, it does not work because I just realized the gridview.slected row does not work since there are no rows selected and they are all selected through the checkbox instead Commented Dec 5, 2014 at 8:46
  • What is the "Name of the checked cells from my gridview"? Do you really want to insert the Text property of those checkboxes? If so, why haven't you used myCheckBox.Text? Commented Dec 5, 2014 at 8:50
  • Because the check box and text next to it are in 2 different cells but on the same row. So Basically, just check the names I want and them insert them to the table, just missing the headerName Commented Dec 5, 2014 at 9:45

1 Answer 1

1

Why do you use GridViewRow row = GridViewConsNames.SelectedRow when you actually want to use all rows where the checkbox is selected?

So i guess that this is what you want:

foreach (GridViewRow row in GridViewConsNames.Rows)
{
    CheckBox myCheckBox = row.FindControl("myCheckBox") as CheckBox;
    if (myCheckBox.Checked)
    {
        // ...
            string headerName = row.Cells[2].Text;
            cmd.Parameters.AddWithValue("@HeaderName", headerName);
        // ...
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi man sorry for my late answer, I solved it by looking at your method with small changes, but it was right! Thank u so much for the help

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.