Click here to Skip to main content
14,385,144 members
Rate this:
Please Sign up or sign in to vote.
See more:
public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"data source=.\SQLEXPRESS;database= Registration; trusted_connection=yes ");
        SqlDataAdapter sd = new SqlDataAdapter();
        DataSet ds = new DataSet();
        BindingSource bs = new BindingSource();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnADD_Click(object sender, EventArgs e)
        {
            
            sd.InsertCommand = new SqlCommand("INSERT INTO dbtest VALUES(@firstname, @lastname, @mobile, @emailid)", con);
            sd.InsertCommand.Parameters.Add("@firstname", SqlDbType.VarChar).Value = tbfname.Text; //****ERROR HERE
            sd.InsertCommand.Parameters.Add("@lastname", SqlDbType.VarChar).Value = tblname.Text;
           sd.InsertCommand.Parameters.Add("@mobile", SqlDbType.Int).Value =Convert.ToInt32(tbmobile.Text);
            sd.InsertCommand.Parameters.Add("@emailid", SqlDbType.VarChar).Value = tbemail.Text;
            con.Open();
            sd.InsertCommand.ExecuteNonQuery();
            con.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sd.SelectCommand = new SqlCommand("SELECT * from dbtest",con);
            ds.Clear();
            sd.Fill(ds);
            DG.DataSource = ds.Tables[0];
            bs.DataSource = ds.Tables[0];
            tbfname.DataBindings.Add(new Binding("Text",bs,"firstname")); //****ERROR HERE
            tblname.DataBindings.Add(new Binding("Text",bs,"lastname"));
            tbmobile.DataBindings.Add(new Binding("Text",bs,"mobile"));
            tbemail.DataBindings.Add(new Binding("Text",bs,"emailid"));
            selction();
        }

        private void DG_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            bs.MoveNext();
            selction();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bs.MovePrevious();
            selction();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bs.MoveFirst();
            selction();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            bs.MoveLast();
            selction();
        }
        private void selction()
        {
            DG.ClearSelection();
            DG.Rows[bs.Position].Selected = true;
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            sd.UpdateCommand = new SqlCommand("UPDATE dbtest SET firstname=@firstname, lastname=@lastname, mobile=@mobile, emailid=@emailid WHERE ID=@ID",con);
            sd.UpdateCommand.Parameters.Add("@firstname", SqlDbType.VarChar).Value = tbfname.Text;  //****ERROR HERE
            sd.UpdateCommand.Parameters.Add("@lastname", SqlDbType.VarChar).Value = tblname.Text;
            sd.UpdateCommand.Parameters.Add("@mobile", SqlDbType.Int).Value = Convert.ToInt32(tbmobile.Text);
            sd.UpdateCommand.Parameters.Add("@emailid", SqlDbType.VarChar).Value = tbemail.Text;
            sd.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value=ds.Tables[0].Rows[bs.Position][0];
            con.Open();
            sd.UpdateCommand.ExecuteNonQuery();
            con.Close();

        }
    }


When i am trying to update or delete it I am getting exception error as "This causes two bindings in the collection to bind to the same property." please help me


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 25-May-12 10:34am
v4

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Every Control can have only one binding at a time. It looks like you already have a binding to the textboxes before and now when you try to rebind it, it throws an error. You need to clear the old binding before adding a new one.

Try:
tbfname.DataBindings.Clear();
tbfname.DataBindings.Add(new Binding("Text",bs,"firstname"));

tblname.DataBindings.Clear();
tblname.DataBindings.Add(new Binding("Text",bs,"lastname"));

tbmobile.DataBindings.Clear();
tbmobile.DataBindings.Add(new Binding("Text",bs,"mobile"));

tbemail.DataBindings.Clear();
tbemail.DataBindings.Add(new Binding("Text",bs,"emailid"));
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100