yatta
yatta
CC#
Created by yatta on 6/20/2024 in #help
UI modification
No description
3 replies
CC#
Created by yatta on 3/6/2024 in #help
Frontend not working
No description
32 replies
CC#
Created by yatta on 1/23/2024 in #help
Seeding data from csv file
I have a model like this:
public class Record
{
public int Id { get; set; }
public long caller_id { get; set; }
public long recipient { get; set; }
public DateTime call_date { get; set; }
public DateTime endtime { get; set; }
public int duration { get; set; }
public double cost { get; set; }
public string reference { get; set; }
public string currency { get; set; }
}
public class Record
{
public int Id { get; set; }
public long caller_id { get; set; }
public long recipient { get; set; }
public DateTime call_date { get; set; }
public DateTime endtime { get; set; }
public int duration { get; set; }
public double cost { get; set; }
public string reference { get; set; }
public string currency { get; set; }
}
I try to seeding data from my csv file like this:
public void SeedDataContext()
{
if (!_ctx.Record.Any())
{
var filePath = "C:/techtest_cdr.csv";

var csvData = System.IO.File.ReadAllLines(filePath).Skip(1);

var datas = new List<Record>();

foreach (var line in csvData)
{
var values = line.Split(',');

var record = new Record
{
caller_id = long.Parse(values[0]),
recipient = long.Parse(values[1]),
call_date = DateTime.ParseExact(values[2], "dd/MM/yyyy",CultureInfo.InvariantCulture),
endtime = DateTime.ParseExact(values[3], "HH/mm/ss", CultureInfo.InvariantCulture),
duration = int.Parse(values[4]),
cost = double.Parse(values[5]),
reference = values[6],
currency = values[7].ToUpper()
};

datas.Add(record);
}

_ctx.Record.AddRange(datas);
_ctx.SaveChanges();
}
}
public void SeedDataContext()
{
if (!_ctx.Record.Any())
{
var filePath = "C:/techtest_cdr.csv";

var csvData = System.IO.File.ReadAllLines(filePath).Skip(1);

var datas = new List<Record>();

foreach (var line in csvData)
{
var values = line.Split(',');

var record = new Record
{
caller_id = long.Parse(values[0]),
recipient = long.Parse(values[1]),
call_date = DateTime.ParseExact(values[2], "dd/MM/yyyy",CultureInfo.InvariantCulture),
endtime = DateTime.ParseExact(values[3], "HH/mm/ss", CultureInfo.InvariantCulture),
duration = int.Parse(values[4]),
cost = double.Parse(values[5]),
reference = values[6],
currency = values[7].ToUpper()
};

datas.Add(record);
}

_ctx.Record.AddRange(datas);
_ctx.SaveChanges();
}
}
When I run the program, I got the error: Unhandled exception. System.FormatException: String '' was not recognized as a valid DateTime. The format for date time for call_date in my csv file is dd/mm/yyyy. The format for endtime in my csv file is HH/mm/ss.
22 replies
CC#
Created by yatta on 1/22/2024 in #help
HTTPPut url
No description
5 replies
CC#
Created by yatta on 1/11/2024 in #help
The value '' is invalid. for primary key in MVC
No description
24 replies
CC#
Created by yatta on 12/25/2023 in #help
Add new field in ASP.Net
So im learning ASP.Net from Microsoft doc, and when I com to this documentation: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/new-field?view=aspnetcore-8.0&tabs=visual-studio I've done everything the doc said, but the Rating doesn't show anything. When I think it suppose to display the Rating "R" when I hardcoded it in the program. Also, the doc says I should able to perform other CRUD command with the Rating attribute, but I can't at all. Is it like how it suppose to work, or I actually have done something wrong ?
4 replies
CC#
Created by yatta on 10/9/2023 in #help
❔ Interview preparation
No description
15 replies
CC#
Created by yatta on 8/26/2023 in #help
❔ Cannot create a .net core application
22 replies
CC#
Created by yatta on 6/6/2023 in #help
❔ What could possibly be the reason when I run database -update but no tabled added ?
10 replies
CC#
Created by yatta on 2/4/2023 in #help
Fibonacci in recursive ?
127 replies
CC#
Created by yatta on 11/14/2022 in #help
❔ How to get an attribute from xml file
7 replies
CC#
Created by yatta on 11/10/2022 in #help
❔ How to write Process name into XML file ?
I try to write my Process name into xml file like this:
foreach(var process in Process.GetProcessesByName(ProcessName))
{
XmlNode startTimeNode = processedData.CreateElement("StartTime");
startTimeNode.AppendChild(processedData.CreateTextNode(process.StartTime.ToString()));
processNode.AppendChild(startTimeNode);
}
foreach(var process in Process.GetProcessesByName(ProcessName))
{
XmlNode startTimeNode = processedData.CreateElement("StartTime");
startTimeNode.AppendChild(processedData.CreateTextNode(process.StartTime.ToString()));
processNode.AppendChild(startTimeNode);
}
but when I try to run the code, it skip the process name for first 2 records, and only from 3rd record onwards, it write down enough information of it.
3 replies
CC#
Created by yatta on 11/10/2022 in #help
❔ How to set a time interval in a timer ?
This is the class:
public string ProcessName { get; set; }
public int MaximumLifeTime { get; set; }
public int Frequency { get; set; }
public ProcessKiller(string name, int maximumLifeTime, int frequency)
{
this.ProcessName = name;
this.MaximumLifeTime = maximumLifeTime;
this.Frequency = frequency;
}
public bool KillProcess()
{
TimeSpan lifeTime = default;

foreach (var process in Process.GetProcessesByName(ProcessName))
{
lifeTime = DateTime.Now - process.StartTime;
if (lifeTime.TotalMinutes >= MaximumLifeTime)
{
process.Kill();
WriteLog();
}
}

return true;
}
public string ProcessName { get; set; }
public int MaximumLifeTime { get; set; }
public int Frequency { get; set; }
public ProcessKiller(string name, int maximumLifeTime, int frequency)
{
this.ProcessName = name;
this.MaximumLifeTime = maximumLifeTime;
this.Frequency = frequency;
}
public bool KillProcess()
{
TimeSpan lifeTime = default;

foreach (var process in Process.GetProcessesByName(ProcessName))
{
lifeTime = DateTime.Now - process.StartTime;
if (lifeTime.TotalMinutes >= MaximumLifeTime)
{
process.Kill();
WriteLog();
}
}

return true;
}
And this is the Main class that calls the class
public class Program
{
public static int MinuteCount { get; set; }
public static void Main(string[] args)
{
string name = args[0];
int maxLifeTime = Convert.ToInt32(args[1]);
int freq = Convert.ToInt32(args[2]);
var killer = new ProcessKiller(name, maxLifeTime, freq);

//Rerun the function after every "intervalTime" minute
var timer = new System.Timers.Timer(freq * 60000);
timer.Elapsed += (s, e) =>
{
killer.KillProcess();
MinuteCount++;
if (MinuteCount == 1)
{
Console.WriteLine("One minute passed !");
MinuteCount = 0;
}
};
//timer.Elapsed += Timer_Elapsed;
timer.Enabled = true;
timer.AutoReset = true;
timer.Start();
Console.WriteLine("Press any key to stop the program");
Console.ReadKey();
}

}
public class Program
{
public static int MinuteCount { get; set; }
public static void Main(string[] args)
{
string name = args[0];
int maxLifeTime = Convert.ToInt32(args[1]);
int freq = Convert.ToInt32(args[2]);
var killer = new ProcessKiller(name, maxLifeTime, freq);

//Rerun the function after every "intervalTime" minute
var timer = new System.Timers.Timer(freq * 60000);
timer.Elapsed += (s, e) =>
{
killer.KillProcess();
MinuteCount++;
if (MinuteCount == 1)
{
Console.WriteLine("One minute passed !");
MinuteCount = 0;
}
};
//timer.Elapsed += Timer_Elapsed;
timer.Enabled = true;
timer.AutoReset = true;
timer.Start();
Console.WriteLine("Press any key to stop the program");
Console.ReadKey();
}

}
I have my program above to check after an interval time, if a process have running longer than the allowed duration or not, and if it is the program will kill the process. The problem I got now is the time take pretty long than I expected. This is the first time I use the timer so Im sure how to adjust the time with it.
48 replies
CC#
Created by yatta on 11/8/2022 in #help
How to kill a process ?
I just know about process killing and try to test it, here is my code:
using System.Diagnostics;

var processes = Process.GetProcessesByName("Sublime Text ");
foreach(var process in processes)
{
process.Kill();
}
Console.WriteLine("Process is killed");
using System.Diagnostics;

var processes = Process.GetProcessesByName("Sublime Text ");
foreach(var process in processes)
{
process.Kill();
}
Console.WriteLine("Process is killed");
When I run the program, there's no error occurred but the Sublime Text process doesn't killed, isn't killing a process means we have it vanished in Task Manager ?. Since there is no error so I have no idea where i'm wrong
12 replies
CC#
Created by yatta on 11/4/2022 in #help
How to create a linked list ?
13 replies
CC#
Created by yatta on 10/12/2022 in #help
How to assert HttpResponseMessage content in unit test [Answered]
Let's say I have 2 variable like this:
var result = await sender.SendAsync(request);
var testResMess = new HttpResponseMessage();
testResMess.Content = new StringContent("\"test message\"");
var result = await sender.SendAsync(request);
var testResMess = new HttpResponseMessage();
testResMess.Content = new StringContent("\"test message\"");
I want to test them in unit test to see if they're equal (which I know they are but I still need to do it in the unit test), so I do something like this:
Assert.AreEqual(testResMess.Content, result.Value);
Assert.AreEqual(testResMess.Content, result.Value);
csharp but the IDE yields The EqualTo constraint always fails as the actual and the expected value cannot be equal So i tried to change it to
Assert.AreEqual(testResMess.Content.ToString(), result.Value);
Assert.AreEqual(testResMess.Content.ToString(), result.Value);
And this time, I got the error: Expected string length 29 but was 12. Strings differ at index 0. Expected: "System.Net.Http.StringContent" But was: "test message" -----------^ At this rate, I have no idea is the problem I got and how I can solve them. I'm also new to C# Unit Testing so any help from expert would be really appriciate.
6 replies
CC#
Created by yatta on 8/18/2022 in #help
How to write function in .net6? [Answered]
9 replies