Hi i have a problem Error 1 Operator '<' cannot be applied to operands of type 'object' and 'System.TimeSpan'. to use timespan with < is there any type of code?

SqlCommand cmd = new SqlCommand("SELECT * FROM tblBooking", conn);
            SqlDataReader rdr = cmd.ExecuteReader();         
            DateTime ts = new DateTime();
            TimeSpan asd = ts.TimeOfDay;
            while (rdr.Read())
            {
                int x = Booking.noOfpersons;
                
                for (int i = 0; i < rdr.FieldCount; i++)
                {
                     if (rdr[7] < asd)
                    {
                        iftrue = true;
                    }
             }

The problem is getting an object from your data reader rather than a time span. You need to cast before doing the relational test.

hi i tried casting the object but it doesn't seem to help. it casted the timespan asd but it gives me more errors, can anyone please help me?

hi i tried casting the object but it doesn't seem to help. it casted the timespan asd but it gives me more errors, can anyone please help me?

Cast the object from the reader. Something like

if ((TimeSpan)rdr[blah] < asd)
{
    
}

>it casted the timespan asd but it gives me more errors, can anyone please help me?
Maybe you should look at your monitor through a mirror, because you seem to be naturally backwards.

Hi i have a problem Error 1 Operator '<' cannot be applied to operands of type 'object' and 'System.TimeSpan'. to use timespan with < is there any type of code?

SqlCommand cmd = new SqlCommand("SELECT * FROM tblBooking", conn);
            SqlDataReader rdr = cmd.ExecuteReader();         
            DateTime ts = new DateTime();
            TimeSpan asd = ts.TimeOfDay;
            while (rdr.Read())
            {
                int x = Booking.noOfpersons;
                
                for (int i = 0; i < rdr.FieldCount; i++)
                {
                     if (rdr[7] < asd)
                    {
                        iftrue = true;
                    }
             }

What are you trying to do?
1. If you are intentionally trying to set "asd" to zero, why don't you set it to zero. (ts is midnight of day 0)
Did you really intend "DateTime ts = DateTime.Now();" to get the current time?
2. "if (rdr[7] < asd)" does not belong inside of "for (int i = 0; i < rdr.FieldCount; i++)" because it is a constant comparison which only needs to be executed once.
3. Is the eighth field of the table a time span or a date type?

How about?

SqlCommand cmd = new SqlCommand("SELECT * FROM tblBooking", conn);
            SqlDataReader rdr = cmd.ExecuteReader();         
            DateTime ts = DateTime.Now();
            TimeSpan asd = ts.TimeOfDay;
            while (rdr.Read())
            {
                int x = Booking.noOfpersons;
                ts = (DateTime) rdr[7];//I'm assuming you don't need the current time anymore in DateTime format
                TimeSpan asd2 = ts.TimeOfDay;
                if (asd2 < asd) iftrue = true;
                
                for (int i = 0; i < rdr.FieldCount; i++)
                {
                    //Do whatever valid reason you have for looping here.
                }
             }

PS Might you want to bypass the loop completely?

for (int i = 0; i < rdr.FieldCount && iftrue; i++)

>it casted the timespan asd but it gives me more errors, can anyone please help me?
Maybe you should look at your monitor through a mirror, because you seem to be naturally backwards.

Well, at times I can't help being sarcastic, but I certainly have to take a back seat to you. You would think a code goddess would know mere mortals can't know everything and give them a break. (Yes, I know its funny watching puny creatures fall down and go boom. Still, isn't it better to get them standing, so they'll fall from a better height?)

commented: Oh my god, go get yourself a sense of humor. -4

Sry i haven't explained myself well. I am having an error Specified cast is not valid. it think because rdr[6] is set to varchar but as far as i know it is not possible to pass date only in datetime in sql am i right? i am just asking because at the moment i am abroad lol.

//this is the code in the booking form
// time is public static
time = TimeSpan.Parse(dtpTime.Value.ToShortTimeString());

//this is the code in another form
while (rdr.Read())
            {
                for (int i = 0; i < rdr.FieldCount; i++)
                {

if ((TimeSpan)rdr[6] > Booking.time) 
                    {
                        // etc etc
                    }
                 }
}

Did you even read my comments?

Interesting. You've switched from rdr[7] to rdr[6]. Make up your mind.

Go back and CAREFULLY read what I said about casting. (At least now you aren't starting from Midnight.)

It doesn't really matter, but why did you make "time" static?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.