Sudhanshu

Sudhanshu

  • NA
  • 4
  • 7.6k

Two bindings in the collection to bind to the same property: Exception

Jul 22 2010 7:04 AM
Hi there,

I have created a windows form through which user can update delete or add records in the table. I have made in such a way that if user chooses update or delete option in the form all the textboxes and comboboxes get populated with the data automatically from the database I am using databinding properties. It works fine but if I choose delete after choosing update application crashes with the exception: This causes two bindings in the collection to binf\d to the same property.

I am even clearing the databindings before using them.

Here is the code...Since I m a newbie in C# please answer in simple terms..

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

 

namespace RMC

{

    public partial class frmdatabaserole : Form

    {

       public string rolename,exp,qualification,skillset;

       public bool Add = false,update=false,Delete=false;

       private OleDbConnection connection;

       private OleDbCommand cmd = new OleDbCommand();

       cmd.Connection = connection;

       cmd.CommandType = CommandType.Text;

       cmd.CommandText = "select * from Role_Desc;";

       private OleDbDataAdapter da1= new OleDbDataAdapter(cmd);

          

       private DataSet ds=new DataSet();

       private DataViewManager dsView;

       MDIParent1 parentrl;

 

        public frmdatabaserole(MDIParent1 parent)

        {

            parentrl = parent;

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

           

            if (Add == true)

            {

                parentrl.updateroledb('a',textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text);  

               

            }

            else if (update == true)

            {

                MessageBox.Show(comboBox1.Text + textBox4.Text + maskedTextBox1.Text);

                parentrl.updateroledb('c', comboBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text);

            }

            else if (Delete == true)

            {

                if(MessageBox.Show(this, "Are you sure you want to delete" + comboBox1.Text + "record", "Warning",MessageBoxButtons.YesNo)==DialogResult.Yes)

                    parentrl.updateroledb('d',comboBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text);

                else

                    return;

               

            }

         

            this.Close();

        }

 

        private void radioButton1_CheckedChanged(object sender, EventArgs e)

        {

            button1.Text = "Add";

            comboBox1.Hide();

            textBox1.Show();

            groupBox2.Enabled = true;

            Add = true;

        }

        private void binddata()

        {

            try

            {

                connection = new OleDbConnection("Provider=Microsoft.JET.OleDb.4.0;Data Source=C:\\Documents and Settings\\pavan\\Desktop\\sudhanshu\\MSaccess\\Database1.mdb;");

                connection.Open();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

                return;

            }

            MessageBox.Show("Connected");

          

            da1.TableMappings.Clear();

            da1.TableMappings.Add("Table", "Role_Desc");

            da1.Fill(ds, "Role_Desc");

            //da1.TableMappings.Add("Table","Role_Desc");

            dsView = ds.DefaultViewManager;

            comboBox1.DataBindings.Clear();

            comboBox1.DataSource = dsView;

            comboBox1.DisplayMember = "Role_Desc.Rol_name";

            comboBox1.ValueMember = "Role_Desc.Rol_ID";

            comboBox1.Show();

            textBox2.DataBindings.Clear();

            textBox2.DataBindings.Add("Text", dsView, "Role_Desc.Rol_SkillSet");

            textBox2.DataBindings.Clear();

            textBox4.DataBindings.Add("Text", dsView, "Role_Desc.Rol_qlfctn");

            maskedTextBox1.DataBindings.Clear();

            maskedTextBox1.DataBindings.Add("Text", dsView, "Role_Desc.Rol_Exp");

            connection.Close();

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)

        {

            textBox1.Hide();

            button1.Text = "Update";

            binddata();

            update = true;

            groupBox2.Enabled = true;

          

 

        }

 

        private void radioButton3_CheckedChanged(object sender, EventArgs e)

        {

            textBox1.Hide();

            comboBox1.Show();

            binddata();

            button1.Text = "Delete";

            Delete = true;

            groupBox2.Enabled = true;

         }

 

        private void button2_Click(object sender, EventArgs e)

        {

            this.Close();

        }

 

 

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

 

        }

 

       

    }

}


Thanks in advance guyz....