In order to populate ComboBox2 with the tasks of the selected Employee in ComboBox1, I need to retrieve the the EmployeeID from the ComboBox1. Could not get the following code to work:
private void Form1_Load(object sender, EventArgs e)
{
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities()) ;
ObjectQuery
comboBox1.DataSource = from u in Emp select new { u.ID, u.LastName };
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "LastName";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ObjectQuery
comboBox2.DataSource = from u in Tsk where u.EmloyeeID = comboBox1.SelectedValue select new { u.EmloyeeID, u.TaskName };
comboBox2.ValueMember = "EmployeeID";
comboBox2.DisplayMember = "TaskName";
}

Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 26, 2012, 4:54 AM EDT
Try this...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Combobox
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oda;
DataSet ds;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Choose CourseID");
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from Courses";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["CourseID"]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from Students where CourseID='" + comboBox1.Text.Trim() + "'";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox2.Items.Add(reader["FirstName"].ToString());
}
con.Close();
reader.Close();
}
}
}
Thanks
If this post helps you mark it as answer
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2012, 5:35 AM EDT
Samio170 reputationPosted Mar 26, 2012, 6:12 AM EDT
Got the next :
- The name 'MyEntities' does not exist in the current context
- Delegate 'System.Func
Samio170 reputationPosted Mar 26, 2012, 6:16 AM EDT
Sorry, but I am forced to use LINQ to Entities for this case.
Thank you anyway.
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2012, 6:24 AM EDT
Samio170 reputationPosted Mar 26, 2012, 8:52 AM EDT
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
ObjectQuery
Error: Embedded statement cannot be a declaration or labeled statement
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2012, 10:52 AM EDT
using (.....)
{
// statements in block
}
Check how I've done it in my first post to this thread.
Samio170 reputationPosted Mar 26, 2012, 2:26 PM EDT
Sorry for this inconvenience,
the used code is as next:
using System;
using System.Windows.Forms;
using System.Data.Objects;
using System.Linq;
namespace MyNameSpace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
ObjectQuery
comboBox1.DataSource = (from u in Emp select new { u.ID, u.LastName }).ToList();
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "LastName";
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1) return;
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
ObjectQuery
comboBox2.DataSource = (from t in Tsk where t.EmloyeeID.ToString() = comboBox1.SelectedValue.ToString() select new { t.EmloyeeID, t.TaskName }).ToList();
comboBox2.ValueMember = "EmployeeID";
comboBox2.DisplayMember = "TaskName";
}
}
}
}
The above code returned:
-Delegate 'System.Func
-Cannot convert lambda expression to type 'string' because it is not a delegate type
-The left-hand side of an assignment must be a variable, property or indexer
-Cannot implicitly convert type 'string' to 'bool'
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2012, 2:50 PM EDT
This line:
comboBox2.DataSource = (from t in Tsk where t.EmloyeeID.ToString() = comboBox1.SelectedValue.ToString() select new { t.EmloyeeID, t.TaskName }).ToList();
should be:
comboBox2.DataSource = (from t in Tsk where t.EmloyeeID.ToString() == comboBox1.SelectedValue.ToString() select new { t.EmloyeeID, t.TaskName }).ToList();
Samio170 reputationPosted Mar 27, 2012, 1:31 AM EDT
comboBox1.SelectedValue return 2 args: "ID" and "LastName", we need to retrieve only "ID" to make comparison with EmployeeID. So I think we have to use something other than .SelectedValue
VulpesRank #55 · 29.2K reputationPosted Mar 27, 2012, 1:45 AM EDT
comboBox1.ValueMember = "ID";
If it is a combination of ID and LastName, then how are they being combined - in other words what does comboBox1.SelectedValue.ToString() typically look like?
Samio170 reputationPosted Mar 27, 2012, 1:53 AM EDT
ComboEmployee (ComboBox1) should have ID and LastName, and should display only LastName
ComboTasks (ComboBox2) should have EmployeeID and TaskName, and should display only TaskName
When we change Employee in ComboEmployee its related tasks should change in ComboTasks
VulpesRank #55 · 29.2K reputationPosted Mar 27, 2012, 1:58 AM EDT
Samio170 reputationPosted Mar 27, 2012, 2:21 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 27, 2012, 2:38 AM EDT
As the property is of type object, that makes it difficult to deal with an anonymous type as casting is not possible.
We could apply the ToString() method and then parse out the value of the ID field but that's error prone.
It's probably better to use either a Tuple or a named type instead.
Are you using .NET 4.0 (VS 2010)?
Samio170 reputationPosted Mar 27, 2012, 2:40 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 27, 2012, 7:51 AM EDT
Samio170 reputationPosted Mar 27, 2012, 8:45 AM EDT
At first use, Combo1 remained empty despite the table contains multiple records.
VulpesRank #55 · 29.2K reputationPosted Mar 27, 2012, 11:25 AM EDT
Samio170 reputationPosted Mar 27, 2012, 12:39 PM EDT
private void Form1_Load(object sender, EventArgs e)
{
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
{
ObjectQuery
comboBox1.DataSource = (from u in Emp select new { u.ID, u.LastName }).ToList();
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "LastName";
}
}
also we have to change this:
if (comboBox1.SelectedIndex == -1) return;
to this:
if (comboBox1.SelectedIndex.ToString() == "0") return;
Because comboBox1.SelectedIndex gets the value 0 each time there is a new ligne, and this a change Event for ComboBox2.
What we really need is to retrieve the current "u.ID" from the first query (ComboBox1) to use it in the second query "EmployeeID = u.ID" as condition to display it in comboBox2
VulpesRank #55 · 29.2K reputationPosted Mar 27, 2012, 1:53 PM EDT
Samio170 reputationPosted Mar 28, 2012, 10:43 AM EDT
Got this: LINQ to Entities does not recognize the method 'System.String ToString () ", and it can not be translated into a store expression.
VulpesRank #55 · 29.2K reputationPosted Mar 28, 2012, 11:11 AM EDT
Is it a string already or is it an int?
Samio170 reputationPosted Mar 28, 2012, 12:18 PM EDT
although I use this:
comboBox2.DataSource = (from t in Tsk select new { t.EmloyeeID, t.TaskName }).ToList();
I get the following error:
An error occurred while executing the command definition. For details, see the inner exception.
a first idea on the issue:
I think I made a mistake as I used EmployeeID as ValueMember of ComboBox2, ValueMember should be unique, I had to put ID (of Tasks table) instead. Code works if used as next (without link to ComboBox1.SelectedValue):
comboBox2.DataSource = (from t in Tsk select new { t.ID, t.TaskName }).ToList();
label2.Text = comboBox2.DataSource.ToString();
comboBox2.ValueMember = "ID";
comboBox2.DisplayMember = "TaskName";
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2012, 6:52 AM EDT
int val = Convert.ToInt32(comboBox1.SelectedValue.ToString().Split(' ')[3].Replace(",", ""));
comboBox2.DataSource = (from t in Tsk where t.ID == val select new { t.ID, t.TaskName }).ToList();
Samio170 reputationPosted Mar 29, 2012, 8:53 AM EDT
the index is outside the limits of Table
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2012, 10:59 AM EDT
Samio170 reputationPosted Mar 29, 2012, 11:23 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2012, 11:40 AM EDT
In other words does the SelectedValue look something like this:
{ ID = 0, LastName = "FONDA" }
Even if it is, I don't really understand the error because 'while' is just a filter - if it's false for all rows, the query should just return values for all the rows.
Samio170 reputationPosted Mar 29, 2012, 12:16 PM EDT
Table Employee Fields are: ID, LastName, FirstName, BirthDate.
Records: IDs from 1 to 3
First record ID = 1 LastName = "FONDA"
Table Tasks Fields are: ID, TaskName, EmployeeID
Records: IDs from 1 to 5
Retated records: ID = 4, TaskName = Task8, EmployeeID = 1
ID = 5, TaskName = Task3, EmployeeID = 1
Employee.ID is related to Tasks.EmployeeID (1 Employee to many Tasks)
VulpesRank #55 · 29.2K reputationPosted Mar 29, 2012, 3:11 PM EDT
If we're getting 0 when the range of values is 1 to 5, then there seems to be something wrong somewhere.
Samio170 reputationPosted Mar 30, 2012, 6:35 AM EDT
MessageBox.Show("Convert.ToInt32(comboBox1.SelectedValue).ToString() = " + Convert.ToInt32(comboBox1.SelectedValue).ToString());
comboBox2.DataSource = (from t in Tsk where t.ID == Convert.ToInt32(comboBox1.SelectedValue) select new { t.ID, t.TaskName }).ToList();
The messageBox returns the right value, but at comboBox2.Datasource level get the following error:
LINQ to Entities does not recognize the method 'Int32 ToInt32 (System.Object) ", and it can not be translated into a store expression.
Conversion could not be done inside the LINQ query
Finally this code works:
int val = Convert.ToInt32(comboBox1.SelectedValue.ToString());
comboBox2.DataSource = (from t in Tsk where t.ID == val select new { t.ID, t.TaskName }).ToList();
comboBox2.ValueMember = "ID";
comboBox2.DisplayMember = "TaskName";
Now the final code is as the next:
Samio170 reputationPosted Mar 30, 2012, 6:40 AM EDT
To be continued here: http://www.c-sharpcorner.com/Forums/Thread/166792/initialize-combo-box-with-a-blank-item-linq-C-Sharp.aspx