0

I have two datetimeControllers :

DateTimeControl dtFrom = new DateTimeControl(); dtFrom.ID = "startDate";

DateTimeControl dtTo = new DateTimeControl(); dtTo.ID = "endDate";

I want to get range between dTfrom.SelectedDate and dtTo.SelectedDate using CAML Query ...

SPList list = web.Lists["WayBill"];
SPQuery myquery = new SPQuery();

myquery.Query = "<OrderBy>" + 
                  "<FieldRef Name='wayNum' />" + 
                  "<FieldRef Name='Company' />" +
                  "<FieldRef Name='Project' />" +
                "</OrderBy>";

My query must be with Lq and Gt.

SPListItemCollection myitemcol = list.GetItems(myquery);

DataTable table = new DataTable();
table.Columns.Add("wayNum");
table.Columns.Add("Title");
table.Columns.Add("Project");
table.Columns.Add("Company");
table.Columns.Add("CarNumber");
table.Columns.Add("NumWayBill");

foreach (SPListItem item in myitemcol)
{
    DataRow row = table.NewRow();
    row["wayNum"] = item["wayNum"].ToString();
    row["Title"] = item["Title"].ToString();
    row["Project"] = item["Project"].ToString();
    row["Company"] = item["Company"].ToString();
    row["CarNumber"] = item["CarNumber"].ToString();
    row["NumWayBill"] = item["NumWayBill"].ToString();
    table.Rows.Add(row);
}

DataView mydataview = new DataView(table);
table = mydataview.ToTable(true, "wayNum", "Title", "Project", "Company", "CarNumber", "NumWayBill");
tableRef.DataSource = table;
tableRef.DataBind();
1

try this:

<Where><And><Gt><FieldRef Name='Date'/><Value Type='DateTime'>" + dTfrom.SelectedDate.ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Value></Gt><Lt><FieldRef Name='Date'/><Value Type='DateTime'>" + dtTo.SelectedDate.ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Value></Lt></And></Where>
0

Just another input:

When working with caml querys, you need to format your date time to the right format.

SPUtility gives you 2 important methods:

SPUtility.CreateISO8601DateTimeFromSystemDateTime

And

SPUtility.CreateDateTimeFromISO8601DateTimeString

They are really helpful when working with Caml and DateTimes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.