本文介绍了遇到异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();

        }
    }



当我尝试更新或删除它时,我收到异常错误,因为这导致集合中的两个绑定绑定到相同的属性."请帮助我


[edit]已添加代码块-OriginalGriff [/edit]



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]

推荐答案

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"));


这篇关于遇到异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 22:25