lundi 2 février 2015

inserting multiple selected checkbox rows value into database

i have 2 datagridview features and actions, upon which both has a checbox column programtically and by designer respectively. now when a checkbox is checked on features grid it loads up its corresponding actions on Actiongridview. now i would like to insert the IDs in Actiongridview by checking a checkbox column and when a button is clicked loops through actiongridview for rows that have been checked and inserts them in database through stored procedure. below is what i tried so far and gets exception error "specified cast is not valid"



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

}


and stored procedure



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$$




Aucun commentaire:

Enregistrer un commentaire