将多个选定的复选框行值插入数据库

时间:2015-02-02 13:58:33

标签: c# mysql checkbox datagridview

我有2个datagridview功能和操作,两者分别在程序上和设计者都有一个checbox列。现在,当在功能网格上选中一个复选框时,它会在Actiongridview上加载相应的操作。现在我想通过检查复选框列在Actiongridview中插入ID,当单击一个按钮时,通过actiongridview循环检查已经检查过的行,并通过存储过程将它们插入到数据库中。下面是我到目前为止尝试的并且得到异常错误"指定的强制转换是无效的"

 private void radbtnSave_Click(object sender, EventArgs e)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;
        using (MySqlConnection cnn = new MySqlConnection(connectionString))
        using (MySqlCommand cmd = cnn.CreateCommand())
            try
            {
                cnn.Open();
                cmd.CommandText = "sp_perm_insert";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pprofileID", Convert.ToInt64(kryptonDataGridProf.SelectedRows[0].Cells["profileID"].Value.ToString()));
                cmd.Parameters.AddWithValue("@pfeatureID", Convert.ToInt64(kryptonDataGridViewAction.Rows[0].Cells["featureID"].Value.ToString()));

            //    int iRow = 0;

                string strfeatureID;
                strfeatureID = kryptonDataGridViewAction.Rows[iRow].Cells["featureID"].Value.ToString();
                //string strActionID;
                //strActionID = kryptonDataGridViewAction.Rows[iRow].Cells["ActionID"].Value.ToString();



                foreach (DataGridViewRow row in kryptonDataGridViewAction.Rows)
                {
                    if (kryptonDataGridViewAction.Rows[iRow].Cells["featureID"].Value.ToString() == strfeatureID)
                    {

                          DataGridViewCheckBoxCell check = kryptonDataGridViewAction.Rows[0].Cells["chk"] as DataGridViewCheckBoxCell; 
                              if ((bool)check.Value)
                    {


                        cmd.Parameters.AddWithValue("@pActionID", Convert.ToInt64(kryptonDataGridViewAction.Rows[iRow].Cells["ActionID"].Value.ToString()));
                        cmd.Parameters.AddWithValue("@pfeatureID", strfeatureID);
                    }


                        //DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                        //ch1 = (DataGridViewCheckBoxCell)kryptonDataGridViewAction.Rows[0].Cells["chk"];

                        //if (ch1.Value == null)
                        //    ch1.Value = true;
                        //MessageBox.Show(ch1.Value.ToString(), "");

                        //switch (ch1.Value.ToString())
                        //{
                        //    case "false":
                        //        ch1.Value = true;

                        //        break;

                        }


                    }



                        DialogResult result = MessageBox.Show("Do you want to apply these changes?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (result == DialogResult.Yes)
                        {
                            MessageBox.Show("Changes has successfully been applied", "Applying Changes", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else if (result == DialogResult.No)
                        {
                            return;
                        }




                int result1 = cmd.ExecuteNonQuery();

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

    }

和存储过程

DROP PROCEDURE IF EXISTS `sp_perm_insert`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_perm_insert`(pprofileID INT(11),
    pfeatureID INT(11),
pActionID INT(11))
BEGIN
    INSERT INTO t_perm(profileID, featureID, ActionID)
    VALUES(pprofileID, pfeatureID, pActionID);
    END$$

0 个答案:

没有答案