简体   繁体   中英

combobox.selectedvalue on comboBox_TextChanged isn't working properly

I have this C# code, and I'm using Sqlite

private void Form1_Load(object sender, EventArgs e)
    {
        using (var db = new mainEntities())
        {
            comboBox1.DataSource = db.Employers.ToList();
            comboBox1.DisplayMember = "Surname";
            comboBox1.ValueMember = "ID";

            //convert to long
            long id = Convert.ToInt64(comboBox1.SelectedValue);
            //get employer's ID
            var employer = (from s in db.Employers
                            where s.ID == id
                            select s).FirstOrDefault();
            //set 2 textboxes to Name and Surname values
            TextBox1.Text = employer.Name;
            TextBox2.Text = employer.Surname;
            db.SaveChanges();

        }
    }

when I try to set my 2 textboxes changing the values of my combobox with this code:

private void comboBox1_TextChanged(object sender, EventArgs e)
    {

        long id = Convert.ToInt64(comboBox1.SelectedValue);
            using (var db = new mainEntities())
        {
            var employer = (from s in db.Employers
                            where s.ID == id
                            select s).FirstOrDefault();
            //set 2 textboxes to Name and Surname values
            TextBox1.Text = employer.Name;
            TextBox2.Text = employer.Surname;       
        }

I get: System.InvalidCastException: 'System.Data.Entity.DynamicProxies.Employers_4603A50E3C8B2711C02C0DAE379C7EC403103DB450E8CCC08E7874FDC1318E90' on type 'System.IConvertible'.'

If I disable Proxy Creation with this:

this.Configuration.ProxyCreationEnabled = false;

in my Db.context.cs I get: System.InvalidCastException: 'Unable to execute cast on objects of type 'Db.Employers' on type 'System.IConvertible'.'

If I try to convert to String instead of long and put the string in a messagebox I get 2 times 'Db.Employers' and from the third the numerical values of the ID as it would be in my combobox. Where am I wrong?

private void Form1_Load(object sender, EventArgs e)
{
    using (var db = new mainEntities())
    {
        comboBox1.DataSource = db.Employers.ToList();
        comboBox1.ValueMember = "ID";
        comboBox1.DisplayMember = "Surname";
        comboBox1.SelectedIndex =0;
        //convert to long
        long id = Convert.ToInt64(comboBox1.SelectedValue);
        //get employer's ID
        var employer = (from s in db.Employers
                        where s.ID == id
                        select s).FirstOrDefault();
        //set 2 textboxes to Name and Surname values
        TextBox1.Text = employer.Name;
        TextBox2.Text = employer.Surname;
        //db.SaveChanges();

    }
}

if you want change the text value to relate with selected value from comboBox you can do it by SelectedIndexChanged Event

private void ComboBox1_SelectedIndexChanged(object sender,System.EventArgs e)
  {
    long id = Convert.ToInt64(comboBox1.SelectedValue);
        using (var db = new mainEntities())
    {
        var employer = (from s in db.Employers
                        where s.ID == id
                        select s).FirstOrDefault();
        //set 2 textboxes to Name and Surname values
        TextBox1.Text = employer.Name;
        TextBox2.Text = employer.Surname;       
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM