PROJECT DETAILS-
This system uses two files....item.txt and transaction.txt..
.when u add products, it goes to item.txt with its current stock.
...when u sell product, the stock decreases from item.txt and a
transaction entry adds to transaction.txt

First Part is completed.Dat is item.txt file..Now if i sell product,I want to decrement the stock from item.txt,,,I m facing probs in dat...

See project as attachment.

item.txt

P00001^^^^Apple^^^^50.00^^^^100
P00002^^^^Computer^^^^25000^^^^50

PROBS
IN FrmSellProd.aspx, In DecrementFromItemFile,y txtSellProdID.text is going to be ""

2) I want to ask dat suppose to edit the one line of file.I have to write the whole file again..OR I can edit that particular line in textfile.....

CZ in VB.net,we can edit that particular line.

Out of curiosity why don't you use a database for this? Using text/flat files is archaic and error and prone. You will need to rewrite the text file. If your parser ever blows up while adding a transaction or modifying inventory you stand to lose all of your data.

Sir This is only a home project..I m newbie to ASP.net..So this project is only for practise.....Nothing else....

Can I make a suggestion instead of using a flat file like that? You should take a look at XML serialization and it will handle all this for you. This is a concept called "business objects" where you have a class represent something about your business and in this case that is stock and transaction.

Use a class like this:

public class InventoryItem
  {
    public string ItemName { get; set; }
    public string ItemNumber { get; set; }
    public decimal Price { get; set; }
    public int QtyOnHand { get; set; }
    public InventoryItem()
    {
    }
    public static void Save(List<InventoryItem> List, string FileName)
    {
      //create a backup
      string backupName = Path.ChangeExtension(FileName, ".old");
      if (File.Exists(FileName))
      {
        if (File.Exists(backupName))
          File.Delete(backupName);
        File.Move(FileName, backupName);
      }

      using (FileStream fs = new FileStream(FileName, FileMode.Create))
      {
        XmlSerializer ser = new XmlSerializer(typeof(List<InventoryItem>));
        ser.Serialize(fs, List);
        fs.Flush();
        fs.Close();
      }
    }
    public static List<InventoryItem> Load(string FileName)
    {
      if (!File.Exists(FileName))
        throw new FileNotFoundException("The inventory file could not be found", FileName);

      List<InventoryItem> result;

      using (FileStream fs = new FileStream(FileName, FileMode.Open))
      {
        XmlSerializer ser = new XmlSerializer(typeof(List<InventoryItem>));
        result = (List<InventoryItem>)ser.Deserialize(fs);
      }
      return result;
    }
  }

Here we create some inventory items, add them to a collection, and save them a file:

private void button1_Click(object sender, EventArgs e)
    {
      List<InventoryItem> lst = new List<InventoryItem>();
      {
        InventoryItem item = new InventoryItem();
        item.ItemName = "Item 1";
        item.ItemNumber = "abc123";
        item.Price = 2.5M;
        item.QtyOnHand = 5;
        lst.Add(item);
      }
      {
        InventoryItem item = new InventoryItem();
        item.ItemName = "Item 2";
        item.ItemNumber = "abc123";
        item.Price = 3.5M;
        item.QtyOnHand = 2;
        lst.Add(item);
      }
      {
        InventoryItem item = new InventoryItem();
        item.ItemName = "Item 3";
        item.ItemNumber = "123abc";
        item.Price = 99.5M;
        item.QtyOnHand = 5;
        lst.Add(item);
      }
      InventoryItem.Save(lst, @"C:\inventory.xml");
    }

Here we load that list and show all the items we have:

private void button2_Click(object sender, EventArgs e)
    {
      List<InventoryItem> lst = InventoryItem.Load(@"C:\inventory.xml");
      foreach (InventoryItem item in lst)
        MessageBox.Show(item.ItemName);
    }

I think you will like that solution much more than parsing a text file like you are currently doing. You can deal with it all as strong typed code then just .Save() and .Load() when you need to make changes.

Sir i will be very thankfull to You,If u help me in solving mine probs.dat i m facing....But thx for suggestion...But first to have knowledge of textfile,Then shift to XML

I disagree with that but OK, that is your decision.

To answer both of your questions: Load the entire text file in to memory, decrement the value, and rewrite the entire file. You will need to parse the line (i am assuming you already have a parser done) and parse the numeric value, decrement it, rewrite the text file.

Then switch to XML and you won't have to worry about it :)

This article has been dead for over six months. Start a new discussion instead.