Reputation: 49
I have a way to Save files using a button that grabs the text in textboxes and saves them with a number specified in one of those text boxes. example: "enter employee id:" (I would enter) "12345" and click the button, and it would save the file as 12345 with some info in it from other text boxes.
Now I also have implemented a way to check if that file exists in two separate folders so that is doesn't create a file with multiple of the same name.
Now I am trying to check if the file exists (in both directories) again when someone clicks a button to login on a separate page but I run into a problem where it always tells me the file doesn't exist. I'm new to programming so I'm a little lost. Here is my code:
Login Page:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rent_a_Car
{
public partial class Employee_Login_Page : Form
{
public Employee_Login_Page()
{
InitializeComponent();
}
string ManagerPath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Managers\Manager_Ids.txt"; //Path To Manager Logins
string EmployeePath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Staff\Staff_Ids.txt"; //Path to Employees Logins
string FileName; //Declares FileName
bool FileExists;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Employee_Id_TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && //Checks Characters entered are Numbers Only and allows them
(e.KeyChar != '0'))
{
e.Handled = true;
}
else if (e.KeyChar == (char)13) //Checks if The "Enter" Key is pressed
{
Login_Btn_Click(this, new EventArgs()); //If so activate the Login Button
}
}
private void Employee_Login_Page_Load(object sender, EventArgs e)
{
}
private void Login_Btn_Click(object sender, EventArgs e)
{
FileName = Employee_Id_TextBox.Text=".txt"; //sets the string FileName to Employee Id Entered
string pathManagerString = System.IO.Path.Combine(ManagerPath, FileName); //Combine Manager Path With New Id and sets to pathManagerString
string pathEmployeeString = System.IO.Path.Combine(EmployeePath, FileName); //Combine Manager Path With New Id and sets to pathEmployeeString
if (FileExists = File.Exists(pathEmployeeString)) //Check If Employee ID File Exists in Employee folder
{
MessageBox.Show("Employee ID Entered as Employee");
EmployeeHome_Page myForm = new EmployeeHome_Page();
this.Hide();
myForm.ShowDialog();
}
else if(FileExists = File.Exists(pathManagerString))
{
MessageBox.Show("Employee ID Entered as Manager");
EmployeeHome_Page myForm = new EmployeeHome_Page();
this.Hide();
myForm.ShowDialog();
}
else
{
MessageBox.Show("Employee ID Not Found");
}
//need to check logins and wether they are manager or just standard users/and if so activate manager button
}
}
}
Second Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Rent_a_Car
{
public partial class AddEmployee_Page : Form
{
public AddEmployee_Page()
{
InitializeComponent();
}
string ManagerPath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Managers";
string EmployeePath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Staff";
private void AddEmployee_Page_Load(object sender, EventArgs e)
{
}
string FileName; //Declares FileName
bool FileExists;
private void CancelAddEmployee_Btn_Click(object sender, EventArgs e)
{
EmployeeHome_Page myForm = new EmployeeHome_Page ();
this.Hide();
myForm.ShowDialog();
}
private void AddEmployee_Btn_Click(object sender, EventArgs e)
{
FileName = EmployeeID_AddEmployee_TextBox.Text; //sets the string FileName to Employee Id Entered
string pathManagerString = System.IO.Path.Combine(ManagerPath, FileName); //Combine Manager Path With New Id and sets to pathManagerString
string pathEmployeeString = System.IO.Path.Combine(EmployeePath, FileName); //Combine Manager Path With New Id and sets to pathEmployeeString
if (FirstName_AddEmployee_TextBox.Text == "" || LastName_AddEmployee_TextBox.Text == "" || EmployeeID_AddEmployee_TextBox.Text == "") //checks if any feild are empty if so show prompt
{
MessageBox.Show("Missing Information!");
}
else if (EmployeeID_AddEmployee_TextBox.TextLength < 5) //checks if employee id is less then 5 digits long, if so display message
{
MessageBox.Show("ID Needs To Be Five Numbers Long");
}
else if (IsManager_CheckBox.Checked) //checks if manager
{
if (FileExists = File.Exists(pathManagerString)) //Check If Employee ID File Exists in manager folder
{
MessageBox.Show("Employee ID Exists Please Choose Another Id #");
}
else if (FileExists = File.Exists(pathEmployeeString)) //Check If Employee ID File Exists in employee folder
{
MessageBox.Show("Employee ID Exists Please Choose Another Id #");
}
else
{
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(pathManagerString, true))
{
file.WriteLine("Manager First Name: " + FirstName_AddEmployee_TextBox.Text); //Writes First Name
file.WriteLine("Manager Last Name: " + LastName_AddEmployee_TextBox.Text);//Writes Last Name
}
}
}
else
{
if (FileExists = File.Exists(pathEmployeeString)) //Check If Employee ID File Exists in emoployee folder
{
MessageBox.Show("Employee ID Exists Please Choose Another Id #");
}
else if(FileExists = File.Exists(pathManagerString)) //Check If Employee ID File Exists in manager folder
{
MessageBox.Show("Employee ID Exists Please Choose Another Id #");
}
else
{
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(pathEmployeeString, true))
{
file.WriteLine("Employee First Name: " + FirstName_AddEmployee_TextBox.Text); //Writes First Name
file.WriteLine("Employee Last Name: " + LastName_AddEmployee_TextBox.Text);//Writes Last Name
}
}
}
}
private void FirstName_AddEmployee_TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void EmployeeID_AddEmployee_TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && //Checks Characters entered are Numbers Only and allows them
(e.KeyChar != '0'))
{
e.Handled = true;
}
}
private void IsManager_CheckBox_CheckedChanged(object sender, EventArgs e)
{
}
}
}
Upvotes: 0
Views: 87
Reputation: 1304
Change this line of code of Login_Btn_Click method:
Old
FileName = Employee_Id_TextBox.Text=".txt";
Replacement
FileName = Employee_Id_TextBox.Text+".txt";
And update location of both folder:
string ManagerPath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Managers";
string EmployeePath = @"C:\Users\mogee\Visual Studios Project Custom Files\Rent A Car Employee Id's\Staff";
Upvotes: 1