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";
}
SamioPosted Mar 30, 2012, 10:40 AM
To be continued here: http://www.c-sharpcorner.com/Forums/Thread/166792/initialize-combo-box-with-a-blank-item-linq-C-Sharp.aspx
SamioPosted Mar 30, 2012, 10:35 AM
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:
VulpesPosted Mar 29, 2012, 7:11 PM
If we're getting 0 when the range of values is 1 to 5, then there seems to be something wrong somewhere.
SamioPosted Mar 29, 2012, 4:16 PM
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)
VulpesPosted Mar 29, 2012, 3:40 PM
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.
SamioPosted Mar 29, 2012, 3:23 PM
VulpesPosted Mar 29, 2012, 2:59 PM
SamioPosted Mar 29, 2012, 12:53 PM
the index is outside the limits of Table
VulpesPosted Mar 29, 2012, 10:52 AM
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();
SamioPosted Mar 28, 2012, 4:18 PM
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";
VulpesPosted Mar 28, 2012, 3:11 PM
Is it a string already or is it an int?
SamioPosted Mar 28, 2012, 2:43 PM
Got this: LINQ to Entities does not recognize the method 'System.String ToString () ", and it can not be translated into a store expression.
VulpesPosted Mar 27, 2012, 5:53 PM
SamioPosted Mar 27, 2012, 4:39 PM
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
VulpesPosted Mar 27, 2012, 3:25 PM
SamioPosted Mar 27, 2012, 12:45 PM
At first use, Combo1 remained empty despite the table contains multiple records.
VulpesPosted Mar 27, 2012, 11:51 AM
SamioPosted Mar 27, 2012, 6:40 AM
VulpesPosted Mar 27, 2012, 6:38 AM
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)?
SamioPosted Mar 27, 2012, 6:21 AM
VulpesPosted Mar 27, 2012, 5:58 AM
SamioPosted Mar 27, 2012, 5:53 AM
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
VulpesPosted Mar 27, 2012, 5:45 AM
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?
SamioPosted Mar 27, 2012, 5:31 AM
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
VulpesPosted Mar 26, 2012, 6:50 PM
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();
SamioPosted Mar 26, 2012, 6:26 PM
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'
VulpesPosted Mar 26, 2012, 2:52 PM
using (.....)
{
// statements in block
}
Check how I've done it in my first post to this thread.
SamioPosted Mar 26, 2012, 12:52 PM
using (LINQtoEntitiesEntities MyEntities = new LINQtoEntitiesEntities())
ObjectQuery
Error: Embedded statement cannot be a declaration or labeled statement
VulpesPosted Mar 26, 2012, 10:24 AM
SamioPosted Mar 26, 2012, 10:16 AM
Sorry, but I am forced to use LINQ to Entities for this case.
Thank you anyway.
SamioPosted Mar 26, 2012, 10:12 AM
Got the next :
- The name 'MyEntities' does not exist in the current context
- Delegate 'System.Func
VulpesPosted Mar 26, 2012, 9:35 AM
Satyapriya NayakPosted Mar 26, 2012, 8:54 AM
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