Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Employee details,have to store in file and to retrieve data from file in c#

I am trying with diff codes.Its not getting data and but store as filestroing.employee.


Suggest some code to work for this?

What I have tried:

C#
public class BaseEmployee
  {
      public int Id;
      public string Name;
  }

  public class Employee : BaseEmployee
  {
      public string Address;


      public  static  Object Adding()
      {
          Employee employeeDetail = new Employee();
          Console.Write("Employee Id:");
          employeeDetail.Id = int.Parse(Console.ReadLine());

          Console.Write("Employee Name:");
          employeeDetail.Name = Console.ReadLine();
          Console.Write("Employee Address:");
          employeeDetail.Address = Console.ReadLine();
          return employeeDetail;


      }
static void Main(string[] args)
      {
          string pathString = @"c:\SpecifiedFiles\MyNewFile.txt";

          Boolean p = true;

          while (p)
          {
              Console.WriteLine("Employee Record Managemnet system");
              Console.WriteLine("1)Add");
              Console.WriteLine("2)Edit");
              Console.WriteLine("3)Dispaly");
              Console.WriteLine("4)Delete");
              Console.WriteLine("Select operation");


              int option = int.Parse(Console.ReadLine());
              switch(option)
              {
                  case 1:
                      TextWriter tw = new StreamWriter(@"c:\SpecifiedFiles\MyNewFile.txt");
                      tw.Write(Adding());

                      tw.Close();
                       break;

                  case 2:
                      TextReader tr = new StreamReader(@"c:\SpecifiedFiles\MyNewFile.txt");

                      Console.WriteLine(tr.ReadLine());
                      Console.WriteLine(tr.ReadToEnd());
                      tr.Close();
                     break;
               }
          }
       }
      }
  }
Posted
Updated 20-Oct-16 21:26pm
v2

you haven't mentioned what type of files. refer below link it may help you
Writing and reading a text file (C#)[^]
 
Share this answer
 
Comments
Member 12675499 21-Oct-16 2:12am    
That link i tried but when retrieving the data ,not getting
Its dispaly as filestoring.Employee
manu_dhobale 21-Oct-16 2:14am    
Update question with your code
Member 12675499 21-Oct-16 2:20am    
public class BaseEmployee
{
public int Id;
public string Name;
}

public class Employee : BaseEmployee
{
public string Address;


public static Object Adding()
{
Employee employeeDetail = new Employee();
Console.Write("Employee Id:");
employeeDetail.Id = int.Parse(Console.ReadLine());

Console.Write("Employee Name:");
employeeDetail.Name = Console.ReadLine();
Console.Write("Employee Address:");
employeeDetail.Address = Console.ReadLine();
return employeeDetail;


}
static void Main(string[] args)
{
string pathString = @"c:\SpecifiedFiles\MyNewFile.txt";

Boolean p = true;

while (p)
{
Console.WriteLine("Employee Record Managemnet system");
Console.WriteLine("1)Add");
Console.WriteLine("2)Edit");
Console.WriteLine("3)Dispaly");
Console.WriteLine("4)Delete");
Console.WriteLine("Select operation");


int option = int.Parse(Console.ReadLine());
switch(option)
{
case 1:
TextWriter tw = new StreamWriter(@"c:\SpecifiedFiles\MyNewFile.txt");
tw.Write(Adding());

tw.Close();
break;

case 2:
TextReader tr = new StreamReader(@"c:\SpecifiedFiles\MyNewFile.txt");

Console.WriteLine(tr.ReadLine());
Console.WriteLine(tr.ReadToEnd());
tr.Close();
break;
}
}
}
}
}
Member 12675499 21-Oct-16 2:21am    
this code i tried,suggest some code to work this?
Here is updated Case 1, please check
C#
using System;
using System.IO;

namespace ConsoleApplication1
{
    class textread
    {
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
        }

        public static Employee Adding()
        {
            Employee employeeDetail = new Employee();
            Console.Write("Employee Id:");
            employeeDetail.Id = int.Parse(Console.ReadLine());
            Console.Write("Employee Name:");
            employeeDetail.Name = Console.ReadLine();
            Console.Write("Employee Address:");
            employeeDetail.Address = Console.ReadLine();
            return employeeDetail;

        }
        public static void Main(string[] args)
        {
            string pathString = @"D:\temp\MyNewFile.txt";

            Boolean p = true;

            while (p)
            {
                Console.WriteLine("Employee Record Managemnet system");
                Console.WriteLine("1)Add");
                Console.WriteLine("2)Edit");
                Console.WriteLine("3)Dispaly");
                Console.WriteLine("4)Delete");
                Console.WriteLine("Select operation");


                int option = int.Parse(Console.ReadLine());
                switch (option)
                {
                    case 1:
                        TextWriter tw = new StreamWriter(@"D:\temp\MyNewFile.txt");
                        var outObj = Adding();
                        string tempStr = "Id: " + outObj.Id + Environment.NewLine + "Emp Name: " + outObj.Name + Environment.NewLine + "Emp Address: " + outObj.Address;
                        tw.Write(tempStr);
                        tw.Close();
                        break;

                    case 2:
                        TextReader tr = new StreamReader(@"D:\temp\MyNewFile.txt");

                        Console.WriteLine(tr.ReadLine());
                        Console.WriteLine(tr.ReadToEnd());
                        tr.Close();
                        break;
                }
            }
        }
    }

}
 
Share this answer
 
Comments
[no name] 21-Oct-16 6:41am    
How many times do you have to answer the same question? Saving up those worthless points for something special?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900