1

When ComboBox SelectedIndexChanged event occurs, I need to populate the respective controls with its values. I am using entity framework 6 for the approach. I am using c# windows forms and .net framework 4.5. My code is :

private void comboBoxCustomer_SelectedIndexChanged(object sender, EventArgs e)
{
    using (ReliableTradersEntities entities = new ReliableTradersEntities())
    {
        m_blouse = new BLOUSE_MEASUREMENT();
        m_pardi = new PARDI_MEASUREMENT();
        m_lengha = new LHENGA_MEASUREMENT();

        var res = from c in entities.CUSTOMERs
                        join p in entities.PARDI_MEASUREMENT on c.CUSTOMERID equals p.CUSTOMERID
                        join l in entities.LHENGA_MEASUREMENT on c.CUSTOMERID equals l.CUSTOMERID
                        join b in entities.BLOUSE_MEASUREMENT on c.CUSTOMERID equals b.CUSTOMERID
                        select new 
                        {

                            p.PARDILENGTH,
                            p.PARDILIMIT,
                            p.SHOULDER,
                            p.SHOULDERTOHEAD,
                            p.HEADTOBACK,
                            p.HALFHEADROUND,
                            p.NIQABLENGTH,
                            p.KAS,
                            l.LHENGALENGTH,
                            l.LHENGALIMIT,
                            l.BELTOPTION,
                            l.ISPOCKET,
                            l.ISZIP,
                            l.STYLE,
                            l.STYLESIZE,
                            l.ALINE,
                            b.BLOUSELENGTH,
                            b.CHEST,
                            b.WAIST,
                            b.SLEEVES,
                            b.NECK,
                            b.POINT,
                            b.BLOUSEOPENING
                        };
        textBoxPL.Text = m_pardi.PARDILENGTH;
    }

}
Abid Ali
  • 1,731
  • 8
  • 26
  • 62

1 Answers1

1

You have created an anonymous type that is returned in the variable res with all the info extracted by your query. You have not filled the initial three class instances.

You need to initialize your instances using the res variable with

// The query returns an IEnumerable, you need to extract the first element
var x = res.FirstOrDefault();
if(x != null)
{
    m_pardi = new PARDI_MEASUREMENT();
    m_pardi.PARDILENGTH = x.PARDILENGTH;
    .... 
}

But you could use directly the res variable

 var x = res.FirstOrDefault();
 if(x != null)
 {
     textBoxPL.Text = x.PARDILENGTH;
     .... and so on....
 }
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I don't get res.PARDILENGTH in my intellisence. – Abid Ali Sep 02 '18 at 20:06
  • Right, too fast. The return from your query is an IEnumerable, you need to get the first element from that query even if it returns a single record. – Steve Sep 02 '18 at 20:29