Click here to Skip to main content
15,913,159 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to filter a gridview to show all the records within a date range i.e. greater than equal to from date -and less than equal to todate, using whereParameter on the datasource. As whereParameters by default search with equality and doesnot support >= or <=, I am at a loss how to achieve this. I have tried doing so in the following manner Fig 1. But ended up with the given error Fig2 when the variable dates has more than one value.

Fig 1.
C#
var dates=from t1 in db.tablename
                   where (t1.datecol) >= Convert.ToDateTime(txtDate1.Text)
                   &&  (t1.datecol) >= Convert.ToDateTime(txtDate2.Text)
                   select t1.datecol;

         foreach (var items in dates)
         {
             DateTime EmailDt = Convert.ToDateTime(items);
             string EmailDate = EmailDt.ToString("MM/dd/yyyy");


             string date = Convert.ToString(EmailDate);
	     String value2 = date;
             String name2 = "Approved";
             var param2 = new Parameter();
             param2.Name = name2;
             var fkColumn = table.GetColumn(name2);
             param2.Type = Type.GetTypeCode(fkColumn.ColumnType);
             param2.DefaultValue = value2;
             GridDataSource.WhereParameters.Add(param2);
             GridDataSource.AutoGenerateWhereClause = true;
         }

Fig 2.
C#
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: No property or field 'datecol1' exists in type 'tablename'.


Please advise if multivalue is possible or only one value can passed to this whereparameter. Thanx in advance.
Posted
v2

1 solution

try using lambda expression

C#
dbContext.tablename.Where(x => x.dateColumn >= startDate && x.dateColumn <= endDate);
 
Share this answer
 
Comments
Bornita Das 21-Mar-13 2:33am    
I can get the list of dates very well with linq. But how to pass multiple values to the whereparameter? If the linq returns a single value, the whereparameter filters the dataset but not when more than one value is passed. Any idea??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900