Teo Laferla
Teo Laferla

Reputation: 41

Labels doesn't display what i need from SQL database (Linq to SQL)

I have a database which holds data i need and i want to display these data in several different labels. I am choosing a Primary Key that represents rows in datatable from combobox and every selected index change i want to retrieve other related details to that specific Primary Key (MID) into labels.

I tried using below code;

private void CBMeasDBMID_SelectedIndexChanged(object sender, EventArgs e)
    {
using (LinqDataClassesDataContext dataContext = new 
LinqDataClassesDataContext())
    {
    var query = from x in dataContext.MeasResults
                where x.MoldID == cBMeasDBMID.SelectedValue.ToString()
                        group x by x.MeasId into grp
                        select grp.OrderByDescending(x => x.Date).First();
    var result = query.OrderByDescending(x => x.Date).Take(5);

    daGridLastMeas.AutoGenerateColumns = false;
    daGridLastMeas.Columns["MeasId"].DataPropertyName = "MeasId";
    daGridLastMeas.Columns["Date"].DataPropertyName = "Date";               
    daGridLastMeas.Columns["Plane"].DataPropertyName = "Plane";
    daGridLastMeas.Columns["Position"].DataPropertyName = "Postn";

    daGridLastMeas.DataSource = result;

    var manuf = from y in dataContext.Moulds
                where y.MID == cBMeasDBMID.SelectedValue.ToString()
                select y.manuf;

    lblManufac.Text = manuf.ToString();

    var size = from a in dataContext.Moulds
               where a.MID == cBMeasDBMID.SelectedValue.ToString()
               select a.Size;

    lblSize.Text = size.ToString();

    var lastmeas = from c in dataContext.MeasResults
                   where c.MoldID == cBMeasDBMID.SelectedValue.ToString()
                   select c.Date;

    lblLastMeasDate.Text = lastmeas.ToString();

    var wi = from d in dataContext.Moulds
                 where d.MID == cBMeasDBMID.SelectedValue.ToString()
                 select d.AWI;

    lblWi.Text = wi.ToString();
        }
    }

My problem is code doesn't retrieve the data, instead when i first click to combobox to choose an ID, all labels show SELECT text which is Primary Key selection ComboBox's default text and afterwards there is no change or data retrieve even though i change the selected index. How can i fix this issue?

Upvotes: 0

Views: 49

Answers (1)

matt
matt

Reputation: 133

    var manuf = (from y in dataContext.Moulds
            where y.MID == cBMeasDBMID.SelectedValue.ToString()
            select y.manuf).FirstOrDefault();

    lblManufac.Text = manuf.ToString();

    var size = (from a in dataContext.Moulds
           where a.MID == cBMeasDBMID.SelectedValue.ToString()
           select a.Size).FirstOrDefault();

    lblSize.Text = size.ToString();

    var lastmeas = (from c in dataContext.MeasResults
               where c.MoldID == cBMeasDBMID.SelectedValue.ToString()
               select c.Date).FirstOrDefault();

    lblLastMeasDate.Text = lastmeas.ToString();

    var wi = (from d in dataContext.Moulds
             where d.MID == cBMeasDBMID.SelectedValue.ToString()
             select d.AWI).FirstOrDefault();

    lblWi.Text = wi.ToString();

Think this will solve your problem

Upvotes: 1

Related Questions