tags:

views:

164

answers:

1

I am struggling to get a subsonic select query to work, I am writing a paging method and tried the following

Select ns = new Select(maincolumns.ToArray());
ns.PageSize = 10;   ** Error Here **
ns.PageIndex = 1;   ** And Error Here **
ns.Where("IsLive").IsEqualTo(true);
ns.And("Title").Like("%" + SearchTerm + "%");
ns.OrderAsc("RentalExVat");
return ns.ExecuteDataSet().Tables[0];

Now it doesn't recognise ns.PageSize Or ns.PageIndex, the rest of the query works fine?? I see I need to use the new 'Query' tool to be able to use these two, but I can't figure out the Query syntax??

any syntax help appreciated

+4  A: 

SubSonic's query syntax is 'fluent', so in your code sample, the Where clause is not be applied to your query. This snippet may work better:

Select ns = new Select(maincolumns.ToArray());
ns = ns.Where("IsLive").IsEqualTo(true)
       .And("Title").Like("%" + SearchTerm + "%")
       .OrderAsc("RentalExVat")
       .Paged(1, 10); // paging is set here
return ns.ExecuteDataSet().Tables[0];

Also, make sure your 'SearchTerm' has been SQL escaped (or use an alternate calling pattern) or you are vulnerable to SQL injection.

Jason
Thanks for replying but this presents the same problem? ns.PageSize is saying its readonly?? And its saying it does not contain a definition for ns.PageIndex?? Its the syntax for those that I cannot figure out, the where section is working fine :(
leen3o
Sorry for the confusion. The Query() object uses those properties and has setters. I've edited my answer to reflect how to use it with the Select() ctor (SqlQuery objects).
Jason