Home > Mobile >  Submitting multiple rows in gridview with Checkboxes (C#)
Submitting multiple rows in gridview with Checkboxes (C#)

Time:12-10

I am aiming to get data:

  • From an sql database
  • To a gridview
  • To a local table

So far, I am able to use a stored procedure to display the sql data to a gridvew and I am also able to use checkboxes to send the data to a local table.

Issue:
Only one row's data is being submitted to the table, despite the number of checkboxes checked.

ex. I click 3 checkboxes, wanting to get 3 different rows of data to the table. I hit the submit button and when I check the table, only one of the "checked" rows is submitted to the table 3 times.

Code:

        protected void Checker_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < Gridview1.Rows.Count; i  )
        {
            int rowind = ((GridViewRow)(sender as Control).NamingContainer).RowIndex;
            CheckBox cb = (CheckBox)Gridview1.Rows[rowind].FindControl("Checker");
            if (cb.Checked)
            {
                data1 = Gridview1.Rows[rowind].Cells[1].Text;
                data2 = Gridview1.Rows[rowind].Cells[2].Text;
                data3 = Gridview1.Rows[rowind].Cells[3].Text;
            }
            else
            {
                data1 = "";
                data2 = "";
                data3 = "";
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
                var connectionString = ConfigurationManager.ConnectionStrings["localDataB"].ConnectionString;
                var insertStatement = "INSERT into LocalDB (Item1, Item2, Item3) values (@Item1, @Item2, @Item3)";
                using (var sqlConnection = new SqlConnection(connectionString))
                {
                    sqlConnection.Open();
                    using (var sqlCommand = new SqlCommand(insertStatement, sqlConnection))
                    {
                        sqlCommand.Parameters.AddWithValue("Item1", data1);
                        sqlCommand.Parameters.AddWithValue("Item2", data2);
                        sqlCommand.Parameters.AddWithValue("Item3", data3);
                        sqlCommand.ExecuteNonQuery();
                    }
                    sqlConnection.Close();
                }
      }
        GVbind();
    }

CodePudding user response:

It should be like this;

sqlCommand.Parameters.AddWithValue("@Item1", data1);
sqlCommand.Parameters.AddWithValue("@Item2", data2);
sqlCommand.Parameters.AddWithValue("@Item3", data3);

CodePudding user response:

Ok, it not at all clear what your routine checker_CheckedChanged() does?

You don't need a post-back or anything for the check boxes - but ONLY the one submit button and code stub

. Those data1, data2 etc. will NOT persist in memory anyway. So, you can't use that routine - but you don't need it either.

Unless the grid has multiple pages etc., then dump that routine. You are free to check box any row in the grid. You then have one submit button code, and that routine just needs a bit of change to check all GV rows, and save the check box values.

That submit button code thus can/will look like this:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["localDataB"].ConnectionString;

        using (var sqlConnection = new SqlConnection(connectionString))
        {
            sqlConnection.Open();

            // insert any row with check boxes into temp db

            string insertStatement = "INSERT into LocalDB (Item1, Item2, Item3) "   
                                     "values (@Item1, @Item2, @Item3)";

            bool Data1, Data2, Data3;

            foreach (GridViewRow gRow in GridView1.Rows)
            {
                Data1 = Convert.ToBoolean(gRow.Cells[0].ToString());
                Data2 = Convert.ToBoolean(gRow.Cells[1].ToString());
                Data3 = Convert.ToBoolean(gRow.Cells[2].ToString());

                // save data if ANY is checked
                if (Data1 | Data2 | Data3)
                {
                    using (var sqlCommand = new SqlCommand(insertStatement, sqlConnection))
                    {
                        sqlCommand.Parameters.Add("@Item1", SqlDbType.Bit).Value = Data1;
                        sqlCommand.Parameters.Add("@Item2", SqlDbType.Bit).Value = Data2;
                        sqlCommand.Parameters.Add("@Item3", SqlDbType.Bit).Value = Data3;
                        sqlCommand.ExecuteNonQuery();
                    }
                }
            }
        }
    GVbind();
    }

I don't see the need for your first routine. The submit button can loop the GV, get the check box values, and if any one of the 3 is checked, then you do the insert.

However, keep in mind that the cells[] collection ONLY works for datafields if you using a templated control and REAL check box, then you need to use findcontrol, and NOT the cells[] collection

  • Related