locked
dbconcurrency exception RRS feed

  • General discussion

  • I wrote this code to insert delete view and edit a database...
    but i m having problems with the dataAdapter.Update();
    can anyone please suggest something....
    thanks













    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace EmployeeMaster
    {
        public partial class frmEmployeeMaster : Form
        {
            private OleDbConnection conn;
            private OleDbDataAdapter dataAdapter;
            private DataTable dataTable;
            private DataSet ds;
            private int currRecord;
            private int totalRecord;
            bool insertSelected;

            public frmEmployeeMaster()
            {
                InitializeComponent();
            }

            private void btnLoad_Click(object sender, EventArgs e)
            {
               // this.Cursor = Cursor.WaitCursor;
                string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=C:\\EMPLOYEE.mdb;User ID=admin";
                conn = new OleDbConnection(connectionString);
                string commandString = "Select * from empmaster1";
                dataAdapter = new OleDbDataAdapter(commandString,conn);
                ds = new DataSet();
                dataAdapter.Fill(ds, "empmaster1");
                dataTable = ds.Tables["empmaster1"];
                currRecord = 0;
                totalRecord = dataTable.Rows.Count;
                FillControls();
                InitialiseCommands();
                ToggleControls(true);
                //this.Cursor = Cursor.Default;
            }
            private void FillControls()
            {
                tbxNo.Text = dataTable.Rows[currRecord]["empno"].ToString();
                tbxName.Text = dataTable.Rows[currRecord]["empname"].ToString();
                tbxDesig.Text = dataTable.Rows[currRecord]["desig"].ToString();
                tbxDOB.Text = dataTable.Rows[currRecord]["dob"].ToString();
                tbxPNo.Text = dataTable.Rows[currRecord]["opno"].ToString();
                tbxPTupe.Text = dataTable.Rows[currRecord]["ptype"].ToString();
                tbxDOI.Text = dataTable.Rows[currRecord]["doissue"].ToString();
                tbxDOE.Text = dataTable.Rows[currRecord]["doexpiry"].ToString();
                tbxPrivatePNo.Text = dataTable.Rows[currRecord]["privatepno"].ToString();
            }
            private void InitialiseCommands()
            {

                // Preparing Insert Command
                dataAdapter.InsertCommand = conn.CreateCommand();
                dataAdapter.InsertCommand.CommandText =
                    " INSERT INTO empmaster1 " +
                    "(empno, empname, desig, dob, opno, ptype, doissue, doexpiry, privatepno)" +
                   " VALUES(@empno, @empname, @desig, @dob, @opno, @ptype, @doissue, @doexpiry, @privatepno)";
                AddParamas(dataAdapter.InsertCommand, "empno", "empname", "desig", "dob", "opno", "ptype",
                    "doissue", "doexpiry", "privatepno");

                // Preparing Update Command
                dataAdapter.UpdateCommand = conn.CreateCommand();
                dataAdapter.UpdateCommand.CommandText =
                    "UPDATE empmaster1 SET " +
                    "empname=@empname, desig=@desig, dob=@dob, opno=@opno, ptype=@ptype, " +
                    " doissue=@doissue, doexpiry=@doexpiry, privatepno=@privatepno" +
                    " WHERE empno = @empno ";
                AddParamas(dataAdapter.UpdateCommand, "empno", "empname", "desig", "dob", "opno", "ptype",
                    "doissue", "doexpiry", "privatepno");

                // Preparing Delete Command
                dataAdapter.DeleteCommand = conn.CreateCommand();
                dataAdapter.DeleteCommand.CommandText = "DELETE FROM empmaster1 WHERE empno = @empno";
                AddParamas(dataAdapter.DeleteCommand, "empno");
            }

            private void AddParamas(OleDbCommand cmd, params string[] cols)
            {
                foreach (string col in cols)
                {
                    cmd.Parameters.Add("@" + col, OleDbType.Char, 0, col);
                }
            }

            private void ToggleControls(bool val)
            {
                tbxDesig.ReadOnly = val;
                tbxDOB.ReadOnly = val;
                tbxDOE.ReadOnly = val;
                tbxDOI.ReadOnly = val;
                tbxName.ReadOnly = val;
                tbxPNo.ReadOnly = val;
                tbxPrivatePNo.ReadOnly = val;
                tbxPTupe.ReadOnly = val;
                //tbxNo.ReadOnly = val;

                btnSave.Enabled = !val;
                btnCancel.Enabled = !val;

                btnDelete.Enabled = val;
                btnEdit.Enabled = val;
                btnInsert.Enabled = val;
                btnLoad.Enabled = val;
                btnNext.Enabled = val;
                btnPrevious.Enabled = val;
            }

            private void btnEdit_Click(object sender, EventArgs e)
            {
                ToggleControls(false);
            }

            private void btnSave_Click(object sender, EventArgs e)  //this is where i am getting the exception
            {
                //lblLabel.Text = "Saving changes...";
               // btnSave.Cursor = Cursor.WaitCursor;
                DataRow row = dataTable.Rows[currRecord];
                row.BeginEdit();
                row["empno"] = tbxNo.Text;
                row["empname"] = tbxName.Text;
                row["desig"] = tbxDesig.Text;
                row["dob"] = tbxDOB.Text;
                row["opno"] = tbxPNo.Text;
                row["ptype"] = tbxPTupe.Text;
                row["doissue"] = tbxDOI.Text;
                row["doexpiry"] = tbxDOE.Text;
                row["privatepno"] = tbxPrivatePNo.Text;
                row.EndEdit();
                dataAdapter.Update(ds, "empmaster1");
                ds.AcceptChanges();
                ToggleControls(true);
                tbxNo.ReadOnly = true;
                insertSelected = false;
                //this.Cursor = Cursor.Default;
                //lblLabel.Text = " Changes Saved ";

            }

            private void btnCancel_Click(object sender, EventArgs e)
            {
                if (insertSelected)
                {
                    btnDelete_Click(null, null);
                    insertSelected = false;
                }
                FillControls();
                ToggleControls(true);
            }

            private void btnInsert_Click(object sender, EventArgs e)
            {
                insertSelected = true;
                DataRow row = dataTable.NewRow();
                dataTable.Rows.Add(row);
                totalRecord = dataTable.Rows.Count;
                currRecord = totalRecord - 1;

                tbxDesig.Text = "";
                tbxDOB.Text = "";
                tbxDOE.Text = "";
                tbxDOI.Text = "";
                tbxName.Text = "";
                tbxNo.Text = "";
                tbxPNo.Text = "";
                tbxPrivatePNo.Text = "0";
                tbxPTupe.Text = "";

                ToggleControls(false);
                tbxNo.ReadOnly = false;
               
            }

            private void btnDelete_Click(object sender, EventArgs e)
            {
                DialogResult res = MessageBox.Show(
                    "Are you sure you want to delete the current record?",
                    "Conform record deletion", MessageBoxButtons.YesNo);
                if (res == DialogResult.Yes)
                {
                    DataRow row = dataTable.Rows[currRecord];
                    row.Delete();
                    dataAdapter.Update(ds, "empmaster1");
                    ds.AcceptChanges();
                    //lblLabel.Text = "Record Deleted";
                    totalRecord--;
                    currRecord = totalRecord - 1;
                    FillControls();
                }
            }

            private void btnNext_Click(object sender, EventArgs e)
            {
                currRecord++;
                if (currRecord >= totalRecord)
                {
                    currRecord = 0;
                }
                FillControls();
            }

            private void btnPrevious_Click(object sender, EventArgs e)
            {
                currRecord--;
                if (currRecord < 0)
                {
                    currRecord = totalRecord - 1;
                }
                FillControls();
            }

                 
              



        }
    }
     
    • Changed type Figo Fei Thursday, July 24, 2008 3:23 AM no follow up
    Friday, July 18, 2008 12:10 PM

All replies

  • Please show the exception detail message as it was, and elabrate what problem it was you met with, so that others can be more helpful.

    Thanks.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, July 21, 2008 8:45 AM