How can i compare the checked day and current day? - c#

Now iam stuck with a problem.
I have a checked listbox having weekdays sunday,monday etc..
This is my code for populating checked items from database.
string[] setday = dr.GetValue(2).ToString().Split(',');
for (int i = 0; i < setday.Length - 1; i++)
{
days.SetItemChecked(Convert.ToInt32(setday[i]), true);
}
And i have a save button when i click the save button i insert the index of checked days to my database table.then i have a compare button to compare when i click this i want to know today is checked or not.
How can i do this ?
Thanks in advance.

Your question doesn't provide much information but still I guess this what you want
I assume that you will fetch a string of days from database like this
"sun,mon,tue,thu,fri,sat"
and in front end want to check days according to that string. If this is the situation than try this out
Front end
<asp:CheckBoxList runat="server" ID="chks">
<asp:ListItem>sun</asp:ListItem>
<asp:ListItem>mon</asp:ListItem>
<asp:ListItem>tue</asp:ListItem>
<asp:ListItem>thu</asp:ListItem>
<asp:ListItem>fri</asp:ListItem>
<asp:ListItem>sat</asp:ListItem>
</asp:CheckBoxList>
Back end
string[] days = "sun,mon,tue,thu,fri,sat".Split(',');
ListItem[] chkdays = new ListItem[chks.Items.Count];
chks.Items.CopyTo(chkdays, 0);
chkdays.ToList().ForEach(delegate(ListItem item)
{
item.Selected = days.Contains(item.Text);
});

I believe what you want is to check current date's day of week and compare it against the checked listbox value. Refer to this MSDN link on how to get DayOfWeek for a date.

Related

Change text color of old date using javascript

I am using javascript to display a dashboard with various charts and graphs and when you click a chart you can drill down and see information about it.
At the bottom I also have a date of when the charts were last updated.
How can change the text color of the text when the 'last updated' date has passed?
I could do it in C# with the following code (something like it, didn't test it), but not sure on how to go about it using javascript.
if(text.Date < DateTime.Now)
{
text.Date.backcolor == Color.Red;
}
You can do something like this:
var dt = document.getElementById("yourDate"); //get your date
var today = new Date(); //get date today
if(dt.value < today )
{
dt.style.color="red";
}
It would be easier to answer if you disclosed how you are putting the date there and what format it is in.
Otherwise check out the Date object in javascript. It could be as simple as:
var oldDate = new Date(unix_timestamp); // you'd provide this from your db?
if(oldDate.getTime() - new Date().getTime() < 0)
document.getElementById('your_date_element?').style.color = 'red';
If you're putting the date there via php or something, you could simply change the output color at that time using a php solution ( strtotime and time(), for example)
EDIT: but yeah that'll always be the case lol.

C# WinForms - Adding text to each line in a textbox

I am very new to C#. I learn best by experimentation, but of course, I will get completely stumped sometimes. I will try to explain my problem the best I can with what knowledge of the programming language I currently have.
I have been trying to create a simple tool to edit/add lines of text into a text file. I have done much researching, especially on this site, and all the information has been extremely helpful. My problem though, is adding text to both sides to a single line of text within a multi-line textbox.
So lets say I have a textbox with 2 existing lines; I want to add some text next to both sides of to one of one lines, and do the same to the next one. Here is an example of what the text would look like before and after a button is hit:
Before
is not the same as is different than
After
A is not the same as B A is different than B
The two lines in "Before" would be in textBox1 (multiline), and would be inserted to richTextBox1 as "After".
Hopefully I have explained it clearly enough, I do not know where to begin with this.
Thank you.
If you know which index you have to update the text, then you should be able to inset the value directly using insert function exposed by string class
Example:
//Get the text box value
var formatedTextboxString = this.textbox1.Text;
formatedTextboxString = formatedTextboxString.Insert(0, "A ");
formatedTextboxString = formatedTextboxString.Insert(21, "B");
//Place the formated text back to the richTextBox
this.richTextBox1.Text = formatedTextboxString;
Try
"{0} is not the same as {1} {2} is different than {3}"
In textbox1. Then use:
textbox2.Text = String.Format(textbox1.Text, A, B, A, B);
In case if you have multiline textbox:
var list = new List<string>(textBox1.Lines);
for (int i = 0; i < list.Count; ++i)
{
list[i] = "A" + list[i] + "B";
}
textBox1.Lines = list.ToArray();
string[] arr = textBox1.Text.Split(Environment.Newline);
//then loop over each line and add whatever you want :-
foreach (string s in arr)
{
//add here
}
I guess this is good enough hint to start with :)

C# If statement where date is this month

string month = DateTime.Now.Month.ToString();
string CommandText = "SELECT slocref.slocname, partstuff.quantity, partstuff.currency, partstuff.postingdate" + " FROM partstuff INNER JOIN slocref ON partstuff.sloc = slocref.slocvalue";
This is the code to get date and month.
if (Regex.IsMatch(rdr["postingdate"].ToString(), "0" + month + "*"))
{
if (rdr["slocname"].ToString() == "Answer/Slatwall")
{
string quantity = rdr["quantity"].ToString();
string currency = rdr["currency"].ToString();
answertotalmonth += float.Parse(currency);
label100.Text = "$" + answertotalmonth.ToString("#,##0.00");
}
}
This is just an example of the code that is run by the if statement.
But the if statement needs to get all of the July information and add it up. So from 07/01/2012 to 07/31/2012. I cannot change the SQL because it messes up the rest of the program. I tried to get the current date, add the 0 in front of it, then use a wildcard.
Need a little direction on this if statement.
Let me clarify this post. The "postingdate" value comes from FileHelpers. Importing a text file by fixed width into these values. The postingdate value looks like "07/16/12"
So, I want to get this current month, and then if a postingdate value has that month's value, run the if statement code.
Why would you do this?
if (Regex.IsMatch(rdr["postingdate"].ToString(), "0" + month + "*"))
If rdr["postingdate"] is supposed to be a date, convert it to a date time and do a sensible comparison.
if(DateTime.TryParse(rdr["postingdate"].ToString(), rdrDateTime))
{
// proceed as planned
if (rdrDate.Month == DateTime.Now.Month)
{
// and so on.
}
}
Not sure I understand you problem correctly but why don't you just match the DataTime's month property:
if( date.Month == month)
{
//your code...
}
Also like mentioned above, you can parse the text as a date. But if you are just interested in just the month that may not be necessary.
Edit: As mentioned in the comments, you would have to check the year as well. So it would be better to pares as a full DateTime object.

Javascript to validate start date and end date in asp.net

I have written a JS that should check that start date is less than end date. If not, alert should be thrown
The JS is written as;
function DateValidation(startDate, EndDate) {
debugger;
var stdate = startDate;
var enddate = EndDate;
if (stdate!= '' && enddate!='') {
if (stdate > enddate) {
alert('Start date cannot be greater than end date');
return false;
}
else {
return true;
}
}
}
This JS gets fired when i am clicking a button as "Show Report".
Problems that i am facing
JS doesn't validate the date correctly. What am i missing? i am passing date from the textbox
The JS doesn't fired up when clicking button for the first time. it fires when clicking the button second time
Plus, i have registered the JS as below;
btnShowReport.Attributes.Add("onclick", "return DateValidation('" + txtStartDate.Text + "', '" + txtEndDate.Text + "');");
Is the above code correct? What is the correct place to register the JS?
Please guide.. thanks!
You need to parse the string values to dates
if (startDate!= '' && EndDate!='') {
var stdate = Date.parse(startDate);
var enddate = Date.parse(EndDate);
if (stdate > enddate) {
alert('Start date cannot be greater than end date');
return false;
}
else
{
return true;
}
}
Without further code it's hard to tell why your button only fires the event on the second click. Is your button disabled to start with?
Use Date.parse. What you are doing is checking whether a string is greater than another string.
Also the script will take only whatever is there at the first time in txtStartDate.Text, txtEndDate.Text EVERY time the script runs.
Why? You have not correctly understood server side and client side execution.
This line in your code,
btnShowReport.Attributes.Add("onclick", "return DateValidation('" + txtStartDate.Text + "', '" + txtEndDate.Text + "');");
registers the script to the page passing the text in those text boxes.
You have assumed that each time the text changes in the text box, the method will take the new values and do the date calculation.
However your script would look something like this, assuming the two text boxes are empty when the page is loaded. You can verify this by checking the page source.
<inputid="btnShowReport" ... onclick="return DateValidation('','')>
Because JavaScript is run at client side, the server is not contacted each time to get the current values of those text boxes.
What you can do is pass the text boxes it self to the method. Something like
return DateValidation(txtStartDate.ClientID, txtEndDate.ClientID);
and from the method you can access it like shown below
function DateValidation(txtStartDate, txtEndDate) {
debugger;
var stdate = Date.parse(txtStartDate.value);
I think the problem is that you're not comparing dates - you have just declared them as vars without a type so they're essentially Strings.
Check out the Date.parse() method.
Adding to what the previous 2 guys have answered with, you have to parse the dates. You also need to validate that they are even dates. I use this library often when working with dates on the client side:
http://www.datejs.com/
The main problem is how you register the event. You are creating a string with code that contains string literals with the values, which means that you get the values from the textboxes at the time that you create the string, not at the time that the event is activated. You have to make a postback before the code is updated with the current values, that is why it doesn't work on the first click.
Create code that gets the values at the time of the click:
btnShowReport.Attributes.Add("onclick", "return DateValidation(document.getElementById('" + txtStartDate.ClientID + "').value, document.getElementById('" + txtEndDate.ClientID + "').value);");
Another possible problem is that the code doesn't compare dates, it compares strings. Some date formats are comparable as strings, e.g. ISO 8601 based formats: "2010-12-31" < "2011-01-01", but other date formats has to be parsed into dates to be compared, e.g. "31/12/2010" > "01/01/2011".
Parse the dates after checking that they are not empty:
...
if (startDate != '' && EndDate != '') {
var stdate = Date.parse(startDate);
var enddate = Date.parse(EndDate);
...

Jquery Datepicker Issues with adding events

I am trying to make a date picker with a bunch of events that I pull from an rss feed. To make the datepicker I pretty much copy this post: jQuery UI Datepicker : how to add clickable events on particular dates?
The issue I am having is that I keep getting the error event.Date is undefined. I think this may be because of how I am passing in the dates. The dates come from a collection of strings on page load, that are converted like this:
//Convert objects
currentEventInformationString = JsonConvert.SerializeObject(currentStoreEventInformation);
eventDatesString = JsonConvert.SerializeObject(storeEventDates);
Where currentEventInformationString is a collection of strings containing a title, description, and link and eventDateString is a collection of strings that are dates (I get it from a method that returns date.ToShortDateString();
I then add all of my dates to an event array like so (in js):
//Adds each event to the date picker
for (var x = 0; x < eventDates.length; x++) {
//Adds event
events[x] = [{ Title: currentEvents[x].title.toString(), Date: new Date(eventDates[x].toString()) }];
}
I have then tried running a console.debug(events[x].Title + " " + events[x].Date); but every time I undefined undefined
When I run a debug like this:
console.debug(currentEvents[x].title.toString() + " " + eventDates[x].toString());
I get the correct values so I know that that is not the issue.
Any suggestions?
Also: I know that the question seems vague so I tried to include as much sample code as I thought was relevant. If you need more let me know. To see how the date picker is made look at the link.
Edit Here is how I declare events:
//Current event
var events = new Array(eventDates.length);
I think you have a stray set of brackets. This:
events[x] = [{
Title: currentEvents[x].title.toString(),
Date: new Date(eventDates[x].toString())
}];
Is assigning an array that contains one object literal to events[x] but I think you just want to assign an object to events[x]:
events[x] = {
Title: currentEvents[x].title.toString(),
Date: new Date(eventDates[x].toString())
};

Categories