Hi all,
i have a table employee;[MSACESS database] it has an ID which is auto generated.
in datagrid view, the id field is hidden. name and department values user should enter.
so for current row , if there is no ID value, then it should be insert.
else , if there is an id value is present it should be update. my code below not looking into that.
can you please help me
- private void DgvWizard_CellValueChanged(object sender, DataGridViewCellEventArgs e)
- {
- if (dgvWizard.CurrentRow != null)
- {
- using (OleDbConnection Connection = new OleDbConnection(connectionString))
- {
- Connection.Open();
- DataGridViewRow dgvRow = dgvWizard.CurrentRow;
- try
- {
- if(dgvRow.Cells["txtEmpID"].Value==DBNull.Value)
- cmd = new OleDbCommand(cmdText: "insert into employees(name,contact) values (@name,@contact)", Connection);
-
- else
- cmd = new OleDbCommand(cmdText: "update employees set name =@name, contact=@contact where empid=@empid", Connection);
-
- cmd.Parameters.AddWithValue("@name", dgvRow.Cells["txtEmployeeName"].Value == DBNull.Value ? "" : dgvRow.Cells["txtEmployeeName "].Value.ToString());
- cmd.Parameters.AddWithValue("@empid ", Convert.ToInt32(dgvRow.Cells["txtEmpID "].Value == DBNull.Value ? "" : dgvRow.Cells["txtEmpID "].Value));
- cmd.Parameters.AddWithValue("@contact", dgvRow.Cells["txtContactName"].Value == DBNull.Value ? "" : dgvRow.Cells["txtContactName"].Value.ToString());
- cmd.ExecuteNonQuery();
- MessageBox.Show("Success!");
- }
- catch (Exception ee) { }
- }
- }
- }