I have a group box with a few radioboxes. My SQL query will be based on radiobox checked status. I have found that the collection will send last query to SQL server once before new query execute it.
For example, in form load event and radiobox checked event
private void Form1_Load(object sender, EventArgs e) { CustomerCollection col = new CustomerCollection(); col.LoadAll(); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { CustomerCollection col = new CustomerCollection(); col.Query.Where(col.Query.LastName == "A"); col.LoadAll(); } private void radioButton2_CheckedChanged(object sender, EventArgs e) { CustomerCollection col = new CustomerCollection(); col.Query.Where(col.Query.LastName == "B"); col.LoadAll(); } private void radioButton3_CheckedChanged(object sender, EventArgs e) { CustomerCollection col = new CustomerCollection(); col.Query.Where(col.Query.LastName == "C"); col.LoadAll(); }
When form executed, it will run the following SQL at SQL Profiler
Code:SELECT * FROM Customer
SELECT * FROM Customer
When I checked radiobox2, it will execute 2 query at SQL Profiler
Code:Query 1 SELECT * FROM Customer Query 2 SELECT * FROM Customer Where LastName='B'
Query 1 SELECT * FROM Customer Query 2 SELECT * FROM Customer Where LastName='B'
When I try to checked radiobox3, it will also send last query to SQL Profiler
Code:Query 1 SELECT * FROM Customer Where LastName='B' Query 2 SELECT * FROM Customer Where LastName='C'
Query 1 SELECT * FROM Customer Where LastName='B' Query 2 SELECT * FROM Customer Where LastName='C'
I feel very curious on it. Althrough last query will not populate data into the collection, andit will populate data using new query result into collection, but is it this situation is normalor any issue to the ES?
Version: 2009.2.1012.0
The code is incorrect, you're not even executing the queries ...
This
private void radioButton1_CheckedChanged(object sender, EventArgs e) { CustomerCollection col = new CustomerCollection(); col.Query.Where(col.Query.LastName == "A"); col.LoadAll(); }
Should be (notice the way we load it ...)
private void radioButton1_CheckedChanged(object sender, EventArgs e) { CustomerCollection col = new CustomerCollection(); col.Query.Where(col.Query.LastName == "A"); col.Query.Load(); }
EntitySpaces | Twitter | BLOG | Please honor our Software License
Regards, Scott Schecter EntitySpaces | Blog | Twitter
In addition to using coll.Query.Load rather than coll.LoadAll as Mike pointed out, I think you need to test the checked/unchecked status of a radio button inside your CheckChanged events.
private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) { CustomerCollection col = new CustomerCollection(); col.Query.Where(col.Query.LastName == "A"); col.Query.Load(); } }
I have an additional question, though. You have already loaded all columns for all rows in your Form1_Load event. Why hit the database again as the user filters data with the RadioButtons? Can't you filter the already in-memory collection instead?
private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) { col.filter = "[LastName] = 'A'"; } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) { col.filter = "[LastName] = 'B'"; } } etc.
David Neal Parsonswww.entityspaces.net
Thanks for your help. It works as expected.
What I want accomplish to hit for the database again when the criteria for conditions was met, that's why I re-query the database under certain circumstances. If in standalone application, it was not a problem. When it involves concurrent users using the application, this would be an issue for us since some users may see other user changes result and some didn't since it was loaded before user make changes or adding new records. Althrough it was an expensive call to hit the database, but it reduces the issues for incident.