1
ObjectContext context = ((IObjectContextAdapter)db).ObjectContext; 
string queryString = @"SELECT VALUE c FROM Product AS c WHERE c.ProductType = " + comboBox1.SelectedValue; 
ObjectQuery<Product> productQuery = context.CreateQuery<Product>( queryString, comboBox1.SelectedValue ); 

// I am having trouble passing combobox.selectedvalue as a parameter –

Dai
  • 141,631
  • 28
  • 261
  • 374
Soundview
  • 43
  • 7

2 Answers2

1

I will tell you the reason in a bit. Let's go through this code first so I can explain what the issue could be.

Here is a Person class:

public class Person 
{
   public int Age { get; set; }
   public string Name { get; set; }
}

Here is a list we will bind the combobox to:

var persons = new List<Person>() { new Person { Age = 35, Name = "George" } };

Here are different ways to set the binding:

comboBox1.DataSource = persons;

comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";

combobox1.SelectedValue will return "George".

Here is another way to set the binding:

comboBox1.DataSource = persons;

comboBox1.DisplayMember = "Name";

combobox1.SelectedValue will return a Person object.

Your issue is most likely because SelectedValue is not returning what you expect it to return.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • @Soundview Set a breakpoint and see what querystring has. See if it has the description selected from `combobox1`. – CodingYoshi Dec 16 '16 at 00:17
  • i have my comboBox1 bound to a database `comboBox1.DataSource = db.Product_Type.ToList(); comboBox1.ValueMember = "Description"; comboBox1.DisplayMember = "Product_Type";` – Soundview Dec 16 '16 at 00:21
  • the return type is of `db.Product_type`and when i select for example `Hats` the `dataGridView1` should filter ProductType 'Hats' but its not doing anything – Soundview Dec 16 '16 at 00:47
  • I guess `db.Product_Type` is a `DbSet` what is the definition for this `T`? I guess you have a `Product_Type` class? What are its properties? – Andrés Robinet Dec 16 '16 at 01:11
  • `public virtual DbSet Product_Type { get; set; }` this was generated by entity framework database first. – Soundview Dec 16 '16 at 02:03
  • 1
    You are making this harder than it needs to be. Did you set the breakpoint like I asked you to? – CodingYoshi Dec 16 '16 at 03:15
  • I added the breakpoint like you said. On the watch it says: ProductType The name 'ProductType' does not exist in the current context. I know this is something simple. I am very new to c# and ef6. My apologies. – Soundview Dec 16 '16 at 10:23
  • @Soundview Edit your question and add Product_Type and Product classes to your code. Make sure you put the whole class code, for both. Without seeing that code, it is hard to tell what the issue could be. – CodingYoshi Dec 16 '16 at 14:24
  • i have no idea what happened but just lost the whole project. The power went out in my house. I gotta start all over. I didn't back up my project. I still have the database – Soundview Dec 16 '16 at 17:05
  • @Soundview sorry to hear that :( On the other hand, you should put your code in source control. This is not advertising and it's a bit off-topic but, off the top of my head, Bitbucket and Gitlab are just two companies that can give you free private Git repositories, if you just don't want other people to see your code. Or... at least upload to GoogleDrive, OneDrive, Box, Dropbox... whatever (again, off-topic and not advertising, there are many other options). Anyway... any chance you will write your code back or you prefer to delete the question and ask it again whenever you are ready? – Andrés Robinet Dec 17 '16 at 10:27
  • Thanks a lot I will look into that. In my project folder is all my files. I don't have to rewrite any code. The project file is the only thing that was damaged. I'm gonna create a new project and import everything and it should be all good. – Soundview Dec 18 '16 at 02:41
  • I will start getting into the habit of backing up. – Soundview Dec 18 '16 at 02:42
  • Hey guys guess what i rewrote all my code and i got my filtered data to work! here's what i did: `var objectContext = ((IObjectContextAdapter)db).ObjectContext;` `System.Data.Objects.ObjectQuery filteredProducts = new ObjectQuery( "SELECT VALUE c FROM Products AS c WHERE c.ProductType = " + comboBox1.SelectedValue, objectContext);` and finally something i did not do before is to update the `dataGridView1.DataSource` with this `dataGridView1.DataSource = filteredProducts;` thank you guys for your help! – Soundview Dec 19 '16 at 22:46
0

You have to add a parameter to your query, that is:

var context = ((IObjectContextAdapter)db).ObjectContext;

var queryString = @"SELECT VALUE c FROM Product AS c WHERE c.ProductType = @productType";

var productQuery = context.CreateQuery<Product>(queryString, new ObjectParameter("productType", comboBox1.SelectedValue);
Andrés Robinet
  • 1,527
  • 12
  • 18
  • Thank you for your help. I really appreciate it. I understand now how to create the parameter. i typed up the code and when i select the the value of the `comboBox1.SelectedValue` it doesn't do anything. i added a `ToList();` and i got an exception. – Soundview Dec 16 '16 at 00:07
  • See @CodingYoshi answer, what does your ´comboBox1.SelectedValue` return?? And by the way. if his answer leads you to the resolution of your problem, by all means mark that as an answer and not this one – Andrés Robinet Dec 16 '16 at 00:10
  • Got it working now! I rewrote the whole thing from scratch this morning. – Soundview Dec 19 '16 at 22:47