I'm designing a database to hold a list of employees which is reading from a text file.
I have two forms, the first one (frmManager) acts as a view to go through the list, which i have next and previous buttons which i scroll through the employees in the list. The other form(frmAdd), can add new employees to the List. My problem is, when i update the List<>, how can i update it in frmManager? when i add a new employee, theiir attributes get written to the text file but I have to rebuild the project to show the updated list.
file that adds employees:
public class EmployeeDB
{
public List<Employee> employees;
public static EmployeeDB instance;
public static EmployeeDB Instance
{
get
{
if (instance == null)
{
instance = new EmployeeDB();
instance.populate();
}
return instance;
}
}
public EmployeeDB()
{
employees = new List<Employee>();
}
public void populate()
{
string[] parts;
foreach (string line in File.ReadAllLines("StaffList.txt"))
{
parts = line.Split(',');
employees.Add(new Employee(parts[0], parts[1], parts[2], parts[3], int.Parse(parts[4]), int.Parse(parts[5])));
}
}
}
The employee class contains just a constructor to add their details.
the form to add new employees
public partial class frmAdd : Form
{
EmployeeDB employee;
int grade;
public frmAddEmployee()
{
employee = EmployeeDB.Instance;
InitializeComponent();
}
private void btnCreate_Click(object sender, EventArgs e)
{
System.IO.StreamWriter file = new System.IO.StreamWriter("StaffList.txt", true);
foreach (Employee em in employee.employees) //To avoid username clashes
{
if (em.username == txtUsername.Text)
{
file.WriteLine(txtFName.Text + "," + txtLName.Text + "," + txtUsername.Text + employee.employees.Count()
+ "," + txtPassword.Text + "," + checkedButton().ToString() + ","
+ 0.ToString(), Environment.NewLine);
}
else
{
file.WriteLine(txtFName.Text + "," + txtLName.Text + "," + txtUsername.Text
+ "," + txtPassword.Text + "," + checkedButton().ToString() + ","
+ 0.ToString(), Environment.NewLine);
}
file.Close();
MessageBox.Show("Employee successfully added");
return;
}
I have tried recalling the EmployeeDB file hoping it would repopulate to no avail. Any help will be much appreciated.
You are not adding the new Employee to the list anywhere. From the code you included, you are only populating the list via populate().
I would add the new Employee to the EmployeeDB on button click. This will cause the list to be up-to-date. Then I would add a method to your EmployeeDB class that writes the file.
private void btnCreate_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
int i = employee.employees.Count();
while (employee.AsEnumerable().Select(r => r.username == username).Count() > 0)
{
username = txtUserName.Text + i++; //Makes sure you have no common usernames
}
employee.employees.Add(new Employee(){...});
employee.SaveFile(); //New method
}
I am assuming you have a string in the class Employee called username hence the r => r.username above
Related
I have small exec(old one) that treat adding members to a table in the DB. if the member not exist in the DB, it will insert new member in AllMember table. If the member already exists in the DB, it will update the values that are different. What exists already in the code is not updating all the members as I want. I want to code it efficiently now. For every update, I am taking all of the members from the DB(6000) and if I have excel with 4000 members it will make the comparison 24000000 and will increase with time.
Getting all the members:
public static IEnumerable<AllMember> GetAllMembersList()
{
string connection = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
using (var dataAccess = new DataAccessDataContext(connection))
{
var v = (from row in dataAccess.AllMembers
//where row.PremiumType.HasValue && row.PremiumType.Value == 100
select row);
return v.ToList();
}
//#TODO fun
}
Handle the file of new\update members
internal override void ProcessFile()
{
StringBuilder CheckMembersList = new StringBuilder();
CheckMembersList.Clear();
ErrorFounds = false;
UpdateQuery = new StringBuilder();
if (!System.IO.File.Exists(InputFile))
{
Mail.InsertNewMail("שגיאה בתהליך קליטת פרטי משתמשים ", "הקובץ " + InputFile + " לא נמצא ");
return;
}
CsvReader fileReader = new CsvReader(InputFile, FileEncoding, false, false);
DataTable fileContentTable = fileReader.ReadFile();
FileInfo fileInfo = new FileInfo(InputFile);
UpdateDB(fileContentTable, CheckMembersList);
WriteResponseFile(fileContentTable);
}
Updating the DB:
private void UpdateDB(DataTable inputTable, StringBuilder CheckMembersList)
{
IEnumerable<AllMember> allMembersList = Utilities.GetAllMembersList();
DBUpdateStatus updateStatus = DBUpdateStatus.NO_CHANGE;
bool x;
bool newMember;
int rowIndex=0 ;
for (int i = 1; i < inputTable.Rows.Count; i++)
{
rowIndex = i;
DataRow fileRow = inputTable.Rows[i];
newMember = true;
foreach (AllMember membersRow in allMembersList)
{
if (!(String.IsNullOrEmpty(membersRow.TZ))) /*&& (fileRow[ConstDBRow.TZ].ToString().Trim().PadLeft(9, '0') == membersRow.TZ.ToString().Trim().PadLeft(9, '0')))*/
{
newMember = false;
updateStatus = UpdateMemberDetails(fileRow, membersRow);
break;
}
}
if (newMember == true)
updateStatus = InsertNewMember(fileRow);
var memberId = GetMemberId(fileRow[ConstDBRow.TZ].ToString().Trim().PadLeft(9, '0'));
if (updateStatus != DBUpdateStatus.NO_CHANGE)
QueryBuilder.InsertRequest(memberId, updateStatus);
fileRow["UPDATE_STATUS"] = Utilities.GetStatusString(updateStatus);
//append to CheckMembersList for sending members list through email
CheckMembersList.AppendLine("Row Index: " + Convert.ToString(rowIndex + 1) +", Identification number: " + (fileRow[ConstDBRow.TZ].ToString().Trim().PadLeft(9, '0')) + ", First Name: " + fileRow[ConstDBRow.FIRST_NAME].ToString().Replace("'","''") + ", Last Name: " + fileRow[ConstDBRow.LAST_NAME].ToString().Replace("'","''") + ", Update Status: " + fileRow["UPDATE_STATUS"].ToString().Replace("'", "''") + "<br/>");
}
}
How can I do this effectively? Is EntityFramework a good option or taking the list of All-Members differently?
I would leave it on DB to compare the records and insert/update using Merge SQL statement.
There is Merge in SQL Server, hope it is available on other DB servers too https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-2017
As a note: Are you doing insert/update request for each of your record? Try to perform one DB call
I'm currently working on finding a way to Log UserActions/Requests. I'm inclined towards logging the details to a text file. The LOG details are organized in a tree-like (hierarchical) structure so that it is readable and shows method names in a step-by-step manner. (if a request went through several methods)
I have a sample app which works fine but it is not the way how it should be. Consider the following classes.
Node class which is the template to make a tree-like (hierarchical) structure. It has attributes such as name (method name), Time and a List type para named Children.
public class Node
{
public string Name; // method name
public DateTime Time; // time when accessed
public List<Node> Children;
public static void PrintTree(Node tree)
{
string temp = "";
List<Node> firstStack = new List<Node>();
firstStack.Add(tree);
List<List<Node>> childListStack = new List<List<Node>>();
childListStack.Add(firstStack);
while (childListStack.Count > 0)
{
List<Node> childStack = childListStack[childListStack.Count - 1];
if (childStack.Count == 0)
{
childListStack.RemoveAt(childListStack.Count - 1);
}
else
{
tree = childStack[0];
childStack.RemoveAt(0);
string indent = "";
for (int i = 0; i < childListStack.Count - 1; i++)
{
indent += (childListStack[i].Count > 0) ? "| " : " ";
}
temp = indent + "+- " + tree.Name + " (" + tree.Time + ")";
Console.WriteLine(indent + "+- " + tree.Name + " (" + tree.Time + ")");
File.AppendAllText(#"C:\Users\aimalkhan\Desktop\Log Work\Log.txt", temp + Environment.NewLine);
if (tree.Children != null)
{
if (tree.Children.Count > 0)
{
childListStack.Add(new List<Node>(tree.Children));
}
}
}
}
File.AppendAllText(#"C:\Users\aimalkhan\Desktop\Log Work\Log.txt", Environment.NewLine + "*************************************************************" + Environment.NewLine);
}
}
My sample Employee class with a sample method SetEmployeeName() having AN EXTRA parameter of NODE type for Logging purposes.
public class Employee
{
private string FirstName { get; set; }
private string LastName { get; set; }
public string SetEmployeeName(string firstName, string lastName, Node node)
{
node.Name = "Class Name: " +this.GetType().Name + ", Calling method Name: setEmployeeName()";
node.Time = DateTime.Now;
this.FirstName = firstName;
this.LastName = lastName;
return this.FirstName + " " + this.LastName;
}
public void CompleteTask(string empName, string taskName)
{
Console.WriteLine("Employee: " + empName + " is completing the task: " + taskName);
}
}
and finally this is how i'm using the aforementioned sample of Codes.
Node root = new Node();
root.Name = "ClassName: Main";
root.Time = DateTime.Now;
root.Children = new List<Node>();
Node child = new Node();
Employee emp = new Employee();
emp.SetEmployeeName("John", "D", child);
root.Children.Add(child);
Node.PrintTree(root);
This is how the output looks
Now my question is that it would really be a headache for me to pass a NODE type para every time i need a child info log. Could this be some how made centralized in any possible way? Is there a better way than this one? A little guidance would really be appreciated.
I'm trying to write a custom query generator for a small database that I'm making, but the comma that should appear in between all the entries to the string aren't appearing only the one at the end is.
private void BTN_advancedSearch_Click(object sender, EventArgs e)
{
// Creates the variable part of the custom query
string customwhereclause = "";
if (CHK_enableGameName.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Game.GameName LIKE '%" + TXT_addGame.Text + "%'";
}
if (CHK_enableGenreName.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Genre.GenreID =" + genreID + "";
}
if (CHK_enableConsoleName.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Console.ConsoleID =" + consoleID + "";
}
if (CHK_enablePlayers.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Game.Players >=" + NUD_players.Value + "";
}
if (CHK_enableDisc.Checked == true)
{
if (CHK_discOwned.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Game.Disc ='" + "yes" + "'";
}
else
{
Connectqry(customwhereclause);
customwhereclause += "Game.Disc ='" + "no" + "'";
}
}
if (CHK_enableCompleted.Checked == true)
{
if (CHK_completed.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause += "Game.Completed ='" + "yes" + "'";
}
else
{
Connectqry(customwhereclause);
customwhereclause += "Game.Completed ='" + "no" + "'";
}
}
//varible query code being passed back to search form.
frm_search.Cstmqry = customwhereclause;
//close the form and reopen the other one.
this.Close();
frm_search.Show();
}
private void Connectqry(string s)
{
if (s == "")
{
Console.WriteLine("the query is blank");
}
else
{
s = s + " , ";
Console.WriteLine(s);
}
}
the output is currently this:
the query is blank
Game.GameName LIKE '%name%' ,
Game.GameName LIKE '%name%'Genre.GenreID =0 ,
Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0 ,
Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0Game.Players >=1 ,
Game.GameName LIKE '%name%'Genre.GenreID =0Console.ConsoleID =0Game.Players >=1Game.Disc ='no' ,
I'm not sure why it's removing the commas that be in between the string.
You should add the code:
if (!string.IsNullOrEmpty(customwhereclause))
{
customwhereclause += " AND ";
}
customwhereclause += // Your condition
in all your conditions. It'll add an AND operator everywhere it's necessary.
Even better:
private static string computeCondition(string current, string newCondition)
{
if (!string.IsNullOrEmpty(current))
{
current += " AND ";
}
return current + newCondition;
}
private void BTN_advancedSearch_Click(object sender, EventArgs e)
{
// Creates the variable part of the custom query
string customwhereclause = "";
if (CHK_enableGameName.Checked == true)
{
Connectqry(customwhereclause);
customwhereclause = computeCondition(customwhereclause, "Game.GameName LIKE '%" + TXT_addGame.Text + "%'");
}
...
To avoid too big code dup
Or even better:
private void BTN_advancedSearch_Click(object sender, EventArgs e)
{
// Creates the variable part of the custom query
List<string> whereClausesList = new List<string>();
if (CHK_enableGameName.Checked == true)
{
Connectqry(customwhereclause);
whereClausesList.Add("Game.GameName LIKE '%" + TXT_addGame.Text + "%'");
}
...
string.Join(" AND ", whereClausesList);
as suggested by Rob
Your code is not working because string is immuteable. When you do string concatenation like s = s + " , "; this is not updating the object that s references. It's creating a new string and assigning the reference to s. And because you don't pass s as a ref you are only updating a local copy of the reference and not the original. The correct way to fix that is to return the new string and assign it.
private string Connectqry(string s)
{
if (s == "")
{
Console.WriteLine("the query is blank");
}
else
{
s = s + " , ";
Console.WriteLine(s);
}
return s;
}
and use it like
customwhereclause = Connectqry(customwhereclause);
As other's have mentioned you probably want to use "AND" instead of commas, and using string.Join or StringBuilder would likely be more efficient, but string being immutable and string concatenation creating a new string is why your current code doesn't do what you expect.
I am working on a Data Structures project and have made a telephone directory. It is a windows form app. There are 2 parts of it; one is the home directory and other is the organization directory.
The home directory uses queues to take first and last name and number of the contact and saves it to a .txt file.
The organization directory uses double linked list to perform same operation and save organization name and number to a new node and then to a .txt file.
Now the problem is that though it saves to the .txt file (linked list) i am unable to search and delete that element from file as well as the linked list. I have tried everything and searched tons of websites but to no use...please help i just have two days to submit my project ;(
this is my code to insert data to doubly linked list and save to file behind the insert buttone
linked_list l;
string data;
bool check = true;
private void button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(textBox1.Text);
string name = textBox2.Text;
string phone = textBox3.Text;
try
{
data = "";
if (check)
{
if (comboBox1.Text == "First")
{
l.InsertFirst(id, name, phone);
data = label1.Text + ":" + textBox1.Text + Environment.NewLine + label2.Text + ":" + textBox2.Text + Environment.NewLine + label3.Text + ":" + textBox3.Text + Environment.NewLine+Environment.NewLine;
File.AppendAllText("Organization Directory.txt", data);
MessageBox.Show("DATA SAVED");
}
else if (comboBox1.Text == "Last")
{
l.InsertLast(id, name, phone);
data = label1.Text + ":" + textBox1.Text + Environment.NewLine + label2.Text + ":" + textBox2.Text + Environment.NewLine + label3.Text + ":" + textBox3.Text + Environment.NewLine+Environment.NewLine;
File.AppendAllText("Organization Directory.txt", data);
MessageBox.Show("DATA SAVED");
}
} display();
}
catch (Exception ex)
{
MessageBox.Show(ex.Source);
}
}
public void display()
{
listBox1.Items.Clear();
Nodes n = l.head;
while (n != null)
{
listBox1.Items.Add(label1.Text + ":" + n.id).ToString();
listBox1.Items.Add(n.name).ToString();
listBox1.Items.Add(label3.Text + ":" + n.phone).ToString();
listBox1.Items.Add(Environment.NewLine).ToString();
n = n.next;
}
}
I would like to click on an item in a listbox and display the attributes that were passed into that listbox to a multiline textbox.
Below is the code I have written on form initialisation
public Form1()
{
InitializeComponent();
ReadFromFile.Read("sample.GED");
foreach (KeyValuePair<int, Individual> kvp in ReadFromFile.individuals)
{
listBox2.Items.Add("ID = " + kvp.Value.id + " Name = " + kvp.Value.name.givenName + " " + kvp.Value.name.surname + " DoB = " + kvp.Value.birth.date);
}
int testIndividual = 94;
string genderOut = "";
if (ReadFromFile.individuals[testIndividual].gender == "M")
{
genderOut = "MALE";
}
else if (ReadFromFile.individuals[testIndividual].gender == "F")
{
genderOut = "FEMALE";
}
try
{
textBox1.AppendText(
"Name = " + ReadFromFile.individuals[testIndividual].name.givenName + " "
+ ReadFromFile.individuals[testIndividual].name.surname
+ Environment.NewLine + "Gender = " + genderOut
+ Environment.NewLine + "Birth date = " + ReadFromFile.individuals[testIndividual].birth.date
+ Environment.NewLine + "Birth place = " + ReadFromFile.individuals[testIndividual].birth.place
+ Environment.NewLine + "Death date = " + ReadFromFile.individuals[testIndividual].death.date
+ Environment.NewLine + "Death place = " + ReadFromFile.individuals[testIndividual].death.place);
}
catch
{
MessageBox.Show("This individual doesnt exist");
}
}
}
I would like to add more so I can click on a listbox item and the details for that item will be shown in the textbox
I get the feeling I may have to override the ToString() method or regex it. Im still quite a novice programmer so go easy on me :) THANK YOU
You need to handle the SelectedIndexChanged event for your listbox.
One way to do this is to bring up Form1.cs[Design] and select the listbox. In the property grid (Alt+Enter) click the icon that looks like this:
Find the event SelectedIndexChanged and double click it. That will hook up an event handler for you in the auto generated Form1.cs.designer file.
Next, replace the code for your Form1 class with the following:
public partial class Form1 : Form
{
private Dictionary<int, Individual> _individuals;
public Form1()
{
InitializeComponent();
ReadFromFile.Read("sample.GED");
_individuals = ReadFromFile.individuals;
listBox1.DataSource = _individuals.Select(individual => individual.Value).ToList();
listBox1.DisplayMember = "name";
listBox1.ValueMember = "id";
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Clear();
var individual = listBox1.SelectedItem as Individual;
string genderOut = (individual.Gender == "M") ? "MALE" : "FEMALE";
var displayText
= String.Format("Name = {0} {1}\r\n" +
"Gender = {2}\r\n" +
"Birth date = {3}\r\n" +
"Birth place = {4}\r\n" +
"Death date = {5}\r\n" +
"Death place = {6}"
, individual.name.givenName
, individual.name.surname
, genderOut
, individual.birth.date
, individual.birth.place
, individual.death.date
, individual.death.place);
textBox1.AppendText(displayText);
}
}
A few notes about some of the things i've changed.
I've moved the code that was setting the textbox value into the SelectedIndexChanged event handler
I've refactored that code so that it's more readable by using the static String.Format method (all those Environment.NewLine repeats you had were messy).
I've setup the data for the list box using the DataSource property instead of your foreach loop.
Also, one thing you'll notice with this is that the list items in the listbox will not show the correct text. This is because you appear to be using some custom classes or structs for the name, birth and death of an Individual? To fix this, you need to add a new property to the Individual class like this:
public class Individual
{
// ... your code
public string DisplayName
{
get { return String.Format("{0} {1}), name.givenName, name.surname; }
}
// ... the rest of your code
}
Then you will need to change the line in my code above that looks like this:
listBox1.DisplayMember = "name";
to this:
listBox1.DisplayMember = "DisplayName";
Final note: You should probably be using "Upper Camel Case" for your property names. That means that they start with an upper case letter and then the first letter of each word is also upper case. For example, name.givenName should be Name.GivenName. This is a widely used convention.