Limit number of rows returned from data connection? - InfoPath Dev Sign in | Join | Help in Data Connections InfoPath (Entire Site) InfoPath Dev InfoPath Dev is dedicated to bringing you the information and tools you need to be successful in your Microsoft Office InfoPath development projects. Home Blogs Forums Photos Downloads InfoPath Dev » InfoPath » Data Connections » Limit number of rows returned from data connection? Limit number of rows returned from data connection? Last post 01-05-2009 04:24 AM by geoffc. 15 replies. Page 1 of 2 (16 items) 1 2 Next > Sort Posts: Oldest to newest Newest to oldest Previous Next 12-27-2008 06:52 PM geoffc Joined on 10-24-2008 Posts 41 Limit number of rows returned from data connection? Reply Contact I am currently querying SQL tables/views and storing the values into numerous repeating tables on my InfoPath form. I would like to store only the first or last 5 rows for all these queries. I was able to do this with the TOP command in the SELECT statement, but it looks like that only works with SQL tables but not views. Does anyone know how to limit the repeating table itself to only display 5 rows? Or does anyone know of any SQL code I can use to limit the returned rows? Any help is appreciated. Thank you!!! 12-28-2008 10:45 PM In reply to Shiva Joined on 04-15-2005 India Posts 694 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Hello, We can do filter those using InfoPath filter condition and using code we can remove extra nodes. If we are doing in InfoPath filter condition or code also it will impact on performance. After getting the all rows we are filtering or removing means it takes unnecessarily time being this kind of operation. My suggestion while getting the number rows limitation is should be in database. Here is the link to create the view using top command, http://sqlug.be/blogs/resource_blog/archive/2008/07/09/sql-server-2005-vs-2000-views-with-top-percent-order-by.aspx Shiva / http://www.ggktech.com 12-29-2008 04:58 AM In reply to geoffc Joined on 10-24-2008 Posts 41 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Thank you for your reply Shiva. Would I be entering in this code through InfoPath or is this something I would have to set up in SQL first? I am currently constructing these queries in my InfoPath form since the queries rely on user input in two fields. The results are then stored in a repeating table. Here is an example query string that I am using: const string queryString = "select * from [TABLE] where [FIELD1]='{0}' and [FIELD2]='{1}' order by [YEAR] DESC, [MONTH] DESC"; 12-29-2008 10:41 PM In reply to Shiva Joined on 04-15-2005 India Posts 694 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Hello, Here is the change in your SQL statement, select TOP 5 * from [TABLE] where [FIELD1]='{0}' and [FIELD2]='{1}' order by [YEAR] DESC, [MONTH] DESC"; Shiva / http://www.ggktech.com 12-30-2008 04:17 AM In reply to geoffc Joined on 10-24-2008 Posts 41 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Yes, but for some reason this does not work when I query against a view and no results are returned. However, when I use this same statement for a table, it works wonderfully. Any ideas? 12-30-2008 05:05 AM In reply to Shiva Joined on 04-15-2005 India Posts 694 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Hello, Can you try once below statement for creating the view, Create view newView as select TOP 5 * from [TABLE] where [FIELD1]='{0}' and [FIELD2]='{1}' order by [YEAR] DESC, [MONTH] DESC"; Shiva / http://www.ggktech.com 12-30-2008 05:47 AM In reply to geoffc Joined on 10-24-2008 Posts 41 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Here is the query string. However, i'm not sure how to integrate this new query string with my code to store the retrieved values into the nodes. const string queryString6 = "Create view newView as select TOP 5 * from [TABLE] where [FIELD1]='{0}' order by [YEAR] DESC, [MONTH] DESC"; if (!string.IsNullOrEmpty(field1)){ AdoQueryConnection dc = (AdoQueryConnection)DataConnections["TABLE"];dc.Command = string.Format(queryString6, uninum); dc.Execute();string[] columnNames = {"COL1","COL2","COL3","COL4"}; string myNamespace = NamespaceManager.LookupNamespace("my");string dNamespace = NamespaceManager.LookupNamespace("d"); string blankNamespace = NamespaceManager.LookupNamespace("");XPathNavigator dataFields = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/my:queryResults1", NamespaceManager); XPathNavigator queryDataFields = DataSources["TABLE"].CreateNavigator();queryDataFields = queryDataFields.SelectSingleNode("/dfs:myFields/dfs:dataFields", NamespaceManager);while (dataFields.MoveToChild("resultsRow1", myNamespace)) { dataFields.DeleteSelf(); }foreach (XPathNavigator resultsRow1 in queryDataFields.SelectChildren("TABLE", dNamespace)) {using (XmlWriter writer = dataFields.AppendChild()) {writer.WriteStartElement("resultsRow1", myNamespace);foreach (string colName in columnNames) { writer.WriteStartElement(colName, myNamespace); writer.WriteValue(resultsRow1.GetAttribute(colName, blankNamespace)); writer.WriteEndElement(); } writer.WriteEndElement(); } } } 12-31-2008 03:55 AM In reply to Shiva Joined on 04-15-2005 India Posts 694 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Hello, First you have to execute that view (newView) in your database. Then you have to write the statement to get the data from view as below, const string queryString6 = "select * from newView"; Rest of the code no change. Shiva / http://www.ggktech.com 12-31-2008 04:31 AM In reply to geoffc Joined on 10-24-2008 Posts 41 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Hello Shiva, can you think of any other work arounds to just return the first five results? Unfortunately, I am not the dba so I do not have permissions to create new views. Thank you for your help. I really appreciate it! 12-31-2008 04:47 AM In reply to Shiva Joined on 04-15-2005 India Posts 694 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Hello, If you do not have permission to create any view you can write the below statement to give top 5 rows, const string queryString6 = "select TOP 5 * from [TABLE] where [FIELD1]='{0}' order by [YEAR] DESC, [MONTH] DESC"; Shiva / http://www.ggktech.com 12-31-2008 08:00 AM In reply to geoffc Joined on 10-24-2008 Posts 41 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Shiva, unfortunately I am restricted to using these pre-built views and the TOP command does not work with the views (no results are returned). Is there anything else you can think of? Thank you! 12-31-2008 08:11 AM In reply to Hilary Stoupa Joined on 06-20-2008 Posts 13,297 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Actually, I got curious and just tested this -- I tried using TOP to query a view in SQL Server Management Studio and it returned the 5 rows, as specified. Then I tried this as a SQL receive data connection in an IP 2007 form (editing the SQL command) and again had 5 rows returned. Could something else be preventing you from seeing the data you expect? Hilary Stoupa 12-31-2008 08:59 AM In reply to geoffc Joined on 10-24-2008 Posts 41 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact That certainly throws a kink into things. When I insert the TOP expression, nothing is returned from the view. When I remove the TOP expression. values are returned. All I am doing is modifying the SQL statement. From this const string queryString2 = "select * from [VIEW] where [FIELD1]='{0}' order by [YEAR] DESC ,[MONTH] DESC"; to this: const string queryString2 = "select TOP 5 * from [VIEW] where [FIELD1]='{0}' order by [YEAR] DESC ,[MONTH] DESC"; 12-31-2008 10:01 AM In reply to Hilary Stoupa Joined on 06-20-2008 Posts 13,297 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact Well, mind you, I was just trying this by editing the SQL in a data connection in the data connection wizard... and then again in a direct query. Can't imagine why it would work differently in code, though. I'm assuming when you test the two different query strings, you are using the same parameters for {0}, year & month.... :) Hilary Stoupa 01-01-2009 08:45 AM In reply to Jimmy Joined on 07-03-2008 Posts 2,595 Re: Limit number of rows returned from data connection? Mark as Not AnswerMark as Answer... Reply Contact I think I've resolved this issue over private message with Geoffrey. The issue was that results were coming back from the database, but the code to copy them to the main data source wasn't finding them. It looks like this line was the problem: foreach (XPathNavigator resultsRow1 in queryDataFields.SelectChildren("TABLE", dNamespace)) Oddly enough, if you query an SQL view from InfoPath, the nodes of the result set will have the name of the view, but if you use "TOP X" in the query, the nodes are named after one of the tables in the view. Changing that line to the following should work for just about any situation: foreach (XPathNavigator resultsRow1 in queryDataFields.Select("*")) Jimmy Rishe / Software Developer / Microsoft MVPQdabra Software Page 1 of 2 (16 items) 1 2 Next > Copyright © 2003-2019 Qdabra Software. All rights reserved.View our Terms of Use.
I am currently querying SQL tables/views and storing the values into numerous repeating tables on my InfoPath form. I would like to store only the first or last 5 rows for all these queries. I was able to do this with the TOP command in the SELECT statement, but it looks like that only works with SQL tables but not views. Does anyone know how to limit the repeating table itself to only display 5 rows? Or does anyone know of any SQL code I can use to limit the returned rows? Any help is appreciated. Thank you!!!
Hello,
We can do filter those using InfoPath filter condition and using code we can remove extra nodes. If we are doing in InfoPath filter condition or code also it will impact on performance. After getting the all rows we are filtering or removing means it takes unnecessarily time being this kind of operation.
My suggestion while getting the number rows limitation is should be in database. Here is the link to create the view using top command,
http://sqlug.be/blogs/resource_blog/archive/2008/07/09/sql-server-2005-vs-2000-views-with-top-percent-order-by.aspx
const
Here is the change in your SQL statement,
select TOP 5 * from [TABLE] where [FIELD1]='{0}' and [FIELD2]='{1}' order by [YEAR] DESC, [MONTH] DESC";
Yes, but for some reason this does not work when I query against a view and no results are returned. However, when I use this same statement for a table, it works wonderfully. Any ideas?
Can you try once below statement for creating the view,
Create view newView as
Here is the query string. However, i'm not sure how to integrate this new query string with my code to store the retrieved values into the nodes.
const string queryString6 = "Create view newView as select TOP 5 * from [TABLE] where [FIELD1]='{0}' order by [YEAR] DESC, [MONTH] DESC";
if (!string.IsNullOrEmpty(field1))
dc.Execute();
{
dataFields.DeleteSelf();
}
writer.WriteStartElement(colName, myNamespace);
writer.WriteValue(resultsRow1.GetAttribute(colName, blankNamespace));
writer.WriteEndElement();
First you have to execute that view (newView) in your database. Then you have to write the statement to get the data from view as below,
const string queryString6 = "select * from newView";
Rest of the code no change.
If you do not have permission to create any view you can write the below statement to give top 5 rows,
const string queryString6 = "select TOP 5 * from [TABLE] where [FIELD1]='{0}' order by [YEAR] DESC, [MONTH] DESC";
Shiva, unfortunately I am restricted to using these pre-built views and the TOP command does not work with the views (no results are returned). Is there anything else you can think of? Thank you!
Actually, I got curious and just tested this -- I tried using TOP to query a view in SQL Server Management Studio and it returned the 5 rows, as specified. Then I tried this as a SQL receive data connection in an IP 2007 form (editing the SQL command) and again had 5 rows returned. Could something else be preventing you from seeing the data you expect?
That certainly throws a kink into things. When I insert the TOP expression, nothing is returned from the view. When I remove the TOP expression. values are returned. All I am doing is modifying the SQL statement. From this
const string queryString2 = "select * from [VIEW] where [FIELD1]='{0}' order by [YEAR] DESC ,[MONTH] DESC";
to this:
const string queryString2 = "select TOP 5 * from [VIEW] where [FIELD1]='{0}' order by [YEAR] DESC ,[MONTH] DESC";
Well, mind you, I was just trying this by editing the SQL in a data connection in the data connection wizard... and then again in a direct query. Can't imagine why it would work differently in code, though. I'm assuming when you test the two different query strings, you are using the same parameters for {0}, year & month.... :)
I think I've resolved this issue over private message with Geoffrey. The issue was that results were coming back from the database, but the code to copy them to the main data source wasn't finding them. It looks like this line was the problem:
foreach (XPathNavigator resultsRow1 in queryDataFields.SelectChildren("TABLE", dNamespace))
Oddly enough, if you query an SQL view from InfoPath, the nodes of the result set will have the name of the view, but if you use "TOP X" in the query, the nodes are named after one of the tables in the view. Changing that line to the following should work for just about any situation:
foreach (XPathNavigator resultsRow1 in queryDataFields.Select("*"))