|
-
Jul 22nd, 2010, 08:11 AM
#1
Thread Starter
New Member
Two bindings in the collection to bind to the same property: Exception
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..
Code:
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)
{
}
}
}
Last edited by Hack; Jul 22nd, 2010 at 08:20 AM.
Reason: Added Code Tags
-
Jul 22nd, 2010, 08:21 AM
#2
Re: Two bindings in the collection to bind to the same property: Exception
Welcome to the forums 
 Originally Posted by sudhanshu
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.
What is the exception?
-
Jul 22nd, 2010, 08:35 AM
#3
Re: Two bindings in the collection to bind to the same property: Exception
Not sure what the exception is ... or where... but I did spot an error in the code:
Code:
textBox2.DataBindings.Clear();
textBox2.DataBindings.Add("Text", dsView, "Role_Desc.Rol_SkillSet");
textBox2.DataBindings.Clear(); // Did you REALLY mean to do this? Look at the name CAREFULLY!
textBox4.DataBindings.Add("Text", dsView, "Role_Desc.Rol_qlfctn");
This is why it's generally a good idea to give your controls a meaningful name as soon as possible.
-tg
EDIT - ah... yes.... that line I noted is the source of the problem. It's not clearing out the bindings on Textbox4, so when you try to add a new binding on the next line, the exception is thrown.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|