ASP.NET Get future 6 months - asp.net

So what I am looking to do is to get future six months in a dropdown box and I was trying something like
public List<String> GetTrainingDates()
{
var monthList = new List<String>();
var currentMonth = DateTime.Now.Month;
for(var i = currentMonth; i <= currentMonth + 6; i++)
{
monthList.Add(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i));
}
return monthList;
}
But ofcourse this goes greater than 12, so would have to restart at 12 and then start from 1 again.
Just wondering if anybody has a good idea how to do this ?

Just use the % modulus operator:
monthList.Add(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(
((i - 1) % 12) + 1));

public List<String> GetTrainingDates()
{
var monthList = new List<String>();
var currentDate = DateTime.Now();
for(var i = 0; i <= 6; i++)
{
monthList.Add(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(currentDate.AddMonths(i).Month));
}
return monthList;
}

Use the .AddMonths function of the DateTime class...
public List<String> GetTrainingDates()
{
var monthList = new List<String>();
var month;
for(var i = 1; i <= 6; i++)
{
month = DateTime.Now.AddMonths(i).Month;
monthList.Add(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month));
}
return monthList;
}

Simply, you can use LINQ expression with Range.....
List<string> listMonth = Enumerable.Range(1, 6).ToList()
.Select(i => CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.AddMonths(i).Month))
.ToList();

Related

Xamarin forms: Issue with multiple events click on same day in XamForms.Controls.Calendar

I am using XamForms.Controls.Calendar for showing events on the calendar. I have give color for special dates on the calendar using the following code:
private void AddSpecialDateWithList(List<events> list)
{
List<SpecialDate> newList = new List<SpecialDate>();
foreach (events model in list)
{
if (string.IsNullOrWhiteSpace(model.end))
{
string date = model.start;
int index = date.IndexOf('T');
if (index > 0)
{
date = date.Substring(0, index);
}
var newDate = AddDate(DateTime.Parse(date), model);
newList.Add(newDate);
newEventsList.Add(model);
}
else
{
string startDate = model.start;
int startIndex = startDate.IndexOf('T');
if (startIndex > 0)
{
startDate = startDate.Substring(0, startIndex);
}
string endDate = model.end;
int endIndex = endDate.IndexOf('T');
if (endIndex > 0)
{
endDate = endDate.Substring(0, endIndex);
}
List<DateTime> dates = GetDatesBetween(DateTime.Parse(startDate), DateTime.Parse(endDate));
for (int i = 0; i < dates.Count; i++)
{
var newDate = AddDate(dates[i], model);
newList.Add(newDate);
newEventsList.Add(model);
}
}
}
calendar.SpecialDates = newList;
}
private SpecialDate AddDate(DateTime dateTime, events model)
{
SpecialDate newDate = new SpecialDate(dateTime)
{
Selectable = true,
BackgroundColor = Color.FromHex("#fec208"),
TextColor = Color.White
};
return newDate;
}
public List<DateTime> GetDatesBetween(DateTime startDate, DateTime endDate)
{
List<DateTime> allDates = new List<DateTime>();
for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
allDates.Add(date);
return allDates;
}
I have also enabled clicked event for special dates:
private async void calendar_DateClicked(object sender, DateTimeEventArgs e)
{
int num = 0;
var specialList = calendar.SpecialDates;
var date = e.DateTime;
selectedEventsList.Clear();
foreach (SpecialDate specialDate in specialList)
{
if (specialDate.Date.Year == date.Year && specialDate.Date.Month == date.Month && specialDate.Date.Day == date.Day)
{
events model = new events();
model = newEventsList[num];
selectedEventsList.Add(model);
}
else
{
num++;
}
}
if (selectedEventsList.Count == 1)
{
await DisplayAlert("Alert", "successs.", "Ok");
}
else
{
eventTitleList.Clear();
for (int j = 0; j < selectedEventsList.Count; j++)
{
eventTitleList.Add(selectedEventsList[j].title);
}
string action = await DisplayActionSheet(null, "Cancel", null, eventTitleList.ToArray());
for (int k = 0; k < eventTitleList.Count; k++)
{
if (action == eventTitleList[k])
{
//next page
}
}
}
}
When multiple events coming on the same day I need to show the events as a pop-up window. Then the user is able to select the event from the pop-up and go to the details page. I have implemented this on the above code, but sometimes duplicates events are showing on the pop-up window.
I have created a sample project for reproducing the issue. In the sample project, on 7May, the events are Chemistry Assignment and English Project. But Chemistry Assignment is showing duplicate. 10 may events are Chemistry Assignment and Physics, but showing Chemistry Assignment and English Project. I checked this lot of times, but didn't find the reason behind this.
The error caused by that the newEventsList do not get the correct index.
Change the code in calendar_DateClicked event from:
foreach (SpecialDate specialDate in specialList)
{
if (specialDate.Date.Year == date.Year && specialDate.Date.Month == date.Month && specialDate.Date.Day == date.Day)
{
events model = new events();
model = newEventsList[num];
selectedEventsList.Add(model);
}
else
{
num++;
}
}
To:
foreach (SpecialDate specialDate in specialList)
{
if (specialDate.Date.Year == date.Year && specialDate.Date.Month == date.Month && specialDate.Date.Day == date.Day)
{
events model = new events();
model = newEventsList[num];
selectedEventsList.Add(model);
}
num++;
}
Screenshot:

Alternative to System.Web.Security.Membership.GeneratePassword in aspnetcore (netcoreapp1.0)

Is there any alternative to System.Web.Security.Membership.GeneratePassword in AspNetCore (netcoreapp1.0).
The easiest way would be to just use a Guid.NewGuid().ToString("n") which is long enough to be worthy of a password but it's not fully random.
Here's a class/method, based on the source of Membership.GeneratePassword of that works on .NET Core:
public static class Password
{
private static readonly char[] Punctuations = "!##$%^&*()_-+=[{]};:>|./?".ToCharArray();
public static string Generate(int length, int numberOfNonAlphanumericCharacters)
{
if (length < 1 || length > 128)
{
throw new ArgumentException(nameof(length));
}
if (numberOfNonAlphanumericCharacters > length || numberOfNonAlphanumericCharacters < 0)
{
throw new ArgumentException(nameof(numberOfNonAlphanumericCharacters));
}
using (var rng = RandomNumberGenerator.Create())
{
var byteBuffer = new byte[length];
rng.GetBytes(byteBuffer);
var count = 0;
var characterBuffer = new char[length];
for (var iter = 0; iter < length; iter++)
{
var i = byteBuffer[iter] % 87;
if (i < 10)
{
characterBuffer[iter] = (char)('0' + i);
}
else if (i < 36)
{
characterBuffer[iter] = (char)('A' + i - 10);
}
else if (i < 62)
{
characterBuffer[iter] = (char)('a' + i - 36);
}
else
{
characterBuffer[iter] = Punctuations[i - 62];
count++;
}
}
if (count >= numberOfNonAlphanumericCharacters)
{
return new string(characterBuffer);
}
int j;
var rand = new Random();
for (j = 0; j < numberOfNonAlphanumericCharacters - count; j++)
{
int k;
do
{
k = rand.Next(0, length);
}
while (!char.IsLetterOrDigit(characterBuffer[k]));
characterBuffer[k] = Punctuations[rand.Next(0, Punctuations.Length)];
}
return new string(characterBuffer);
}
}
}
I've omitted the do...while loop over the CrossSiteScriptingValidation.IsDangerousString. You can add that back in yourself if you need it.
You use it like this:
var password = Password.Generate(32, 12);
Also, make sure you reference System.Security.Cryptography.Algorithms.
System.Random doesn't provide enough entropy when used for security reasons.
https://cwe.mitre.org/data/definitions/331.html
Why use the C# class System.Random at all instead of System.Security.Cryptography.RandomNumberGenerator?
Please see the example below for a more secure version of #khellang version
public static class Password
{
private static readonly char[] Punctuations = "!##$%^&*()_-+[{]}:>|/?".ToCharArray();
public static string Generate(int length, int numberOfNonAlphanumericCharacters)
{
if (length < 1 || length > 128)
{
throw new ArgumentException("length");
}
if (numberOfNonAlphanumericCharacters > length || numberOfNonAlphanumericCharacters < 0)
{
throw new ArgumentException("numberOfNonAlphanumericCharacters");
}
using (var rng = RandomNumberGenerator.Create())
{
var byteBuffer = new byte[length];
rng.GetBytes(byteBuffer);
var count = 0;
var characterBuffer = new char[length];
for (var iter = 0; iter < length; iter++)
{
var i = byteBuffer[iter] % 87;
if (i < 10)
{
characterBuffer[iter] = (char)('0' + i);
}
else if (i < 36)
{
characterBuffer[iter] = (char)('A' + i - 10);
}
else if (i < 62)
{
characterBuffer[iter] = (char)('a' + i - 36);
}
else
{
characterBuffer[iter] = Punctuations[GetRandomInt(rng, Punctuations.Length)];
count++;
}
}
if (count >= numberOfNonAlphanumericCharacters)
{
return new string(characterBuffer);
}
int j;
for (j = 0; j < numberOfNonAlphanumericCharacters - count; j++)
{
int k;
do
{
k = GetRandomInt(rng, length);
}
while (!char.IsLetterOrDigit(characterBuffer[k]));
characterBuffer[k] = Punctuations[GetRandomInt(rng, Punctuations.Length)];
}
return new string(characterBuffer);
}
}
private static int GetRandomInt(RandomNumberGenerator randomGenerator)
{
var buffer = new byte[4];
randomGenerator.GetBytes(buffer);
return BitConverter.ToInt32(buffer);
}
private static int GetRandomInt(RandomNumberGenerator randomGenerator, int maxInput)
{
return Math.Abs(GetRandomInt(randomGenerator) % maxInput);
}
}

Returning 2 Dimensional array in asp.net c#

I'm using .asmx web service that will return 2 dimensional array where I want to return like row1={'id1','name1'}, row2={'id2','name2'} but I'm not being able to do so.
Code Section
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Array getAlbumsAndPhotos()
{
DataTable resultText = new DataTable();
resultText = AdminUserDataAccess.getAlbumsAndPhotosforAsmx();
string[,] ResultResponse = new string[resultText.Rows.Count , resultText.Columns.Count];
for (int i = 0; i < resultText.Rows.Count; i++)
{
for (int j = 0; j < resultText.Columns.Count; j++)
{
ResultResponse[i, j] = resultText.Rows[i][j].ToString();
}
}
return ResultResponse;
}
This gives me output as:
[0,0]:"id1"
[0,1]:"name1"
[1,0]:"id2"
[1,1]:"name2"
I want two rows to be returned not four with single 0th index(with id1 and name1) and 1st index(with id2 and name2). Some type of nested array output is expected. But I'm not being able to do so. Any help is appreciated. :)
Edit: Solution
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<string[]> getAlbumsAndPhotos()
{
DataTable resultText = new DataTable();
resultText = AdminUserDataAccess.getAlbumsAndPhotosforAsmx();
string[] rowsValues = new string[resultText.Rows.Count];
var list = new List<string[]>();
for (int i = 0; i < resultText.Rows.Count; i++)
{
string col1 = resultText.Rows[i][0].ToString();
string col2 = resultText.Rows[i][1].ToString();
list.Add(new[] { col1, col2});
}
return list;
I can use List or Array list to implement this. Thanks. :)

j script runtime error: object requiered

I am working on the platform confirmit, which creates online surveys. This is a script node that results in the runtime error "object required", I would be grateful if you could help me fix it.. It is supposed to check whether certain codes hold 1 or 2 in the questions q2b and q3a (questions are referenced with the function f() - f(question id)[code]) - the
'//skip ?' part. Then it recodes a maximum of four of the codes into another question (h_q4) for further use.
var flag1 : boolean = false;
var flag2 : boolean = false;
//null
for(var i: int=0; i <9; i++)
{
var code = i+1;
f("h_q4")[code].set(null);
}
f("h_q4")['95'].set(null);
//skip ?
for(var k: int=1; k <16; k+=2)
{
var code = k;
if(f("q2b")[code].none("1", "2"))
flag1;
else
{
flag1 = 0;
break;
}
}
if(f("q3a")['1'].none("1", "2"))
flag2;
if(flag1 && flag2)
f("h_q4")['95'].set("1");
//recode
else
{
var fromForm = f("q2b");
var toForm = f("h_q4");
const numberOfItems : int = 4;
var available = new Set();
if(!flag1)
{
for( i = 1; i < 16; i+=2)
{
var code = i;
if(f("q2b")[i].any("1", "2"))
available.add(i);
}
}
if(!flag2)
{
available.add("9");
}
var selected = new Set();
if(available.size() <= numberOfItems)
{
selected = available;
}
else
{
while(selected.size() < numberOfItems)
{
var codes = available.members();
var randomNumber : float = Math.random()*codes.length;
var randomIndex : int = Math.floor(randomNumber);
var selectedCode = codes[randomIndex];
available.remove(selectedCode);
selected.add(selectedCode);
}
}
var codes = fromForm.domainValues();
for(var i = 0;i<codes.length;i++)
{
var code = codes[i];
if(selected.inc(code))
{
toForm[code].set("1");
}
else
{
toForm[code].set("0");
}
}
}
the first part of the code (//null) empties the 'recepient' question to ease testing
.set(), .get(), .any(), .none() are all valid

Populating an array collection by parsing a string

In ActionScript I have string as
str="subject,r1,r2:a,b:1,2:3,4";
dynamically i have split this string and build array collection as given below
arraycoll.addItem({subject:a ,r1:1,r2:3});
this example of one set
the arraycollection should be built dynamic i have tried but not successful
var str:String ="subject,r1,r2:a,b:1,2:3,4";
var parts:Array = str.split(":");
var props:Array = parts[0].split(",");
var count:Number = parts[1].split(",").length;
var items:Array = [];
var values:Array = [];
var i:Number, j:Number;
for(i = 0; i < props.length; i++)
values.push(parts[i + 1].split(","));
for(i = 0; i < count; i++)
{
items.push({});
for(var j = 0; j < props.length; j++)
{
items[i][props[j]] = values[j][i];
}
}
var arCol:ArrayCollection = new ArrayCollection(items);

Resources