locked
Cannot perform '=' operation on System.TimeSpan and System.String. RRS feed

  • Question

  • User1487175000 posted

    Hi,

    I have following datatable table structure. Its have two timespan columns. Later i try to filter datatbale based on these timespan column. Then i get the following error.

    dt_checkout = new DataTable();
    dt_checkout.Columns.Add(new DataColumn("buildingID", typeof(Int32)));
    dt_checkout.Columns.Add(new DataColumn("bookingDate", typeof(DateTime)));
    dt_checkout.Columns.Add(new DataColumn("bookingStartTime", typeof(TimeSpan)));
    dt_checkout.Columns.Add(new DataColumn("bookingEndTime", typeof(TimeSpan)));
    
    // filter
    dt_checkout.Select("buildingID=" + bID + " AND bookingDate='" + bDate.ToString("yyyy-MM-dd") + "' AND bookingStartTime='" + sTime.ToString() + "' AND bookingEndTime='" + eTime.ToString() + "'");
    Cannot perform '=' operation on System.TimeSpan and System.String.

    Monday, February 18, 2019 11:16 PM

All replies

  • User-1716253493 posted

    Are sTime and eTime time span?

    Check what the sTime.ToString() result

    TimeSpan is duration

    Tuesday, February 19, 2019 12:40 AM
  • User1487175000 posted

    Yes they are timespain duration.

    Tuesday, February 19, 2019 8:20 AM
  • User-1174608757 posted

    Hi,Shahid Majeed

    As far as I known ,timespan has methods for comparisons but it  does not  have the use of the operators which means you could n't compare the type of timespan.

    So, in this case , you could just use long type to represent the duration. You could write as below:

     protected void Page_Load(object sender, EventArgs e)
            {
               DataTable dt_checkout = new DataTable();        
               dt_checkout.Columns.Add(new DataColumn("bookingStartTime", typeof(long)));
                DataRow row = dt_checkout.NewRow();
                
                row["bookingStartTime"] = "3600";
                dt_checkout.Rows.Add(row);
        
               DataRow[] rows= dt_checkout.Select("bookingStartTime= '"+ long.Parse(a1.Text)+"'");
    
            }

    Best Regards

    Wei Zhang

    Tuesday, February 19, 2019 9:51 AM
  • Sunday, February 24, 2019 10:21 PM