0

I'm currently working on a project which needs to check for the day and time, then compare these with values in my database. This needs to be done for an Live Chat, while we have opened. I'm new to ASP.Net and this is giving me an headache.

I Have a table which contains these Rows: - weekday - starttime - endtime

In my Controller for the Chat I have tried this so far

private Model1Container db = new Model1Container();

    public ActionResult Index(Openinghours openinghours)
    {
        ViewBag.Opening = db.OpeninghoursSatz.ToList();

        string CurrentDay = DateTime.Now.DayOfWeek.ToString();
        string CurrentTime = DateTime.Now.TimeOfDay.ToString();

        bool IsWeekDay= db.OpeninghoursSatz.Where(i => i.weekday == CurrentDay).Any();
        var Starttime = db.OpeninghoursSatz.Find(CurrentDay);
        Debug.Write("!!++" + Starttime);

My exact Problem is to compare the time with the two rows from my table. I thought about putting the values in a variable and then compare them, but there must be a cleaner way for this.

I hope I was able to point out what my problem is, if not please ask.

With regards,

Nico

EDIT: Maybe I should say that the database holds the hour it opens and the hour it closes (e.g. 9 and 17). I need to check if the current time is between these two.

2
  • you want to compare the start days of two row ? , and after comparision something need to be done , or just u want to calculate ? Commented Jun 11, 2015 at 8:33
  • I have the row starttime and endtime. in starttime is the int value 9 and in endtime the value 17. The current time is 10:43 AM. so it is betwen 9 AM and 5 PM. Thats what I want to check. If the time is between these values I activate the chat and if not I deactivate the chat. Commented Jun 11, 2015 at 8:45

2 Answers 2

1
      int getStartHour = DateTime.Now.Hour;
        int weekDay= (int)DateTime.Now.DayOfWeek;
 bool value =    db.OpeninghoursSatz.Where(m => getStartHour >= m.Starttime  && getStartHour <= m.EndTime && m.weekday == weekDay).Any()

    /// if everything is in int. or if in string 
     string getStartHour = DateTime.Now.Hour.ToString();
        string weekDay= DateTime.Now.DayOfWeek.ToString();
 bool value =    db.OpeninghoursSatz.Where(m => getStartHour >= m.Starttime  && getStartHour <= m.EndTime && m.weekday == weekDay).Any();

But in this case for Thursday , we will get Thursday, but in Int we will get 4.
Sign up to request clarification or add additional context in comments.

5 Comments

I think there is an Problem with this Code. db.OpeninghoursSatz.Any is not possible. I get the message that Any is not defined in DBset<Models.Openinghours>. And should .Any not be at the and? so I should replace .any with find/equals or something like that.
Try now with the edited one, but it doesnt make sense, I used intelligence to build the query only difference was the class name.. :(
This is weird. after rebuilding the project he isn't saying not defined anymore. but now he wantso something different in .Any(. After m => getStartHour > = he says = is not legit and after that he wants an ")". Maybe I'm just stupid.
Now its working :) Thank you a lot. it was my bad that I overlooked the whitespace and I also had some trouble with my database. Now everything is working. Thank you sir.
0

The line looks odd to me:

var Starttime = db.OpeninghoursSatz.Find(CurrentDay);

I guess you should change it to something like:

if (IsWeekDay){
    var Starttime = db.OpeninghoursSatz.Where(i => i.weekday == CurrentDay).First().StartTime;
    Debug.Write("!!++" + Starttime);
}

1 Comment

It seems like my variable CurrentDay is the Problem. I get the Error "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException`. But this gives me an Idea for a new way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.