I want to read just the last record of file....I m able to read the whole file.

Secondly I want to know that in VB.Net we have ubound to get the upper bound of the array..What is the equivalent in ASP.net?

protected void Page_Load(object sender, EventArgs e)
    {
       
        string sProdID;
        string sID="P000";
        string sLastLine;
         string[] sArr;

        string sPath = Server.MapPath("~/") ;
        string sFileName = sPath + "Products.txt";
        if (File.Exists(sFileName.Trim()) == true)
        {
            sArr = System.IO.File.ReadAllLines(sFileName);
      }
           

    }

Above code read the File...

Suppose mine file contains-

1^^^^Desc1^^^^50.00^^^^12
2^^^^Desc2^^^^50.00^^^^12
3^^^^Desc3^^^^50.00^^^^12
4^^^^Desc4^^^^50.00^^^^12
5^^^^Desc5^^^^50.00^^^^12
6^^^^Desc6^^^^50.00^^^^12
7^^^^Desc7^^^^50.00^^^^12
8^^^^Desc8^^^^50.00^^^^12
9^^^^Desc9^^^^50.00^^^^12
10^^^^Desc10^^^^50.00^^^^12

Recommended Answers

All 10 Replies

using System;
using System.IO;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmReadLine : Form
  {
    public frmReadLine()
    {
      InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      const string fName = @"C:\file.txt";
      using (StreamReader sr = new StreamReader(fName))
      {
        string curLine, prevLine = null;
        while ((curLine = sr.ReadLine()) != null)
        {
          prevLine = curLine;
        }
        Console.WriteLine("Last line: {0}", prevLine);
      }
    }
  }
}

Secondly I want to know that in VB.Net we have ubound to get the upper bound of the array..What is the equivalent in ASP.net?

arrayName.GetUpperBound(dimension);
        arrayName.GetLowerBound(dimension);

or you can use:

arrayName.GetLength(dimension);

you are really famous...i replied you on the same thread at vbforums..

read the file contents and add in to the arraylist and iterate till second last record...

cos next is the thing you are loking 4....

hi first of all thx to all who replied..
hi IdanS , can u plz tell me what is dimension in bracket....
hi dnanetwork,I m famous....Oh i dont know it..Thx for telling me....

Easiest solution I found ,

{
       
        string sProdID;
        string sID="P000";
        string sLastLine;
         string[] sArr;
        int lUBound;

        string sPath = Server.MapPath("~/") ;
        string sFileName = sPath + "Products.txt";
        if (File.Exists(sFileName.Trim()) == true)
        {
            sArr = System.IO.File.ReadAllLines(sFileName);
            lUBound = sArr.Length;
            string lastline = sArr[sArr.Length - 1];
      }
       

    }

hi first of all thx to all who replied..
hi IdanS , can u plz tell me what is dimension in bracket....
hi dnanetwork,I m famous....Oh i dont know it..Thx for telling me....

Easiest solution I found ,

{
       
        string sProdID;
        string sID="P000";
        string sLastLine;
         string[] sArr;
        int lUBound;

        string sPath = Server.MapPath("~/") ;
        string sFileName = sPath + "Products.txt";
        if (File.Exists(sFileName.Trim()) == true)
        {
            sArr = System.IO.File.ReadAllLines(sFileName);
            lUBound = sArr.Length;
            string lastline = sArr[sArr.Length - 1];
      }
       

    }

The "dimension" in the bracket means "read the last line".

sArr = System.IO.File.ReadAllLines(sFileName);

That loads an array with all your lines. Say you have 10 lines in the file -- that means sArr.length = 10 (your 10 lines). Now you want to read the last line and arrays are 0 indexed meaning the first value of an array is always array[0] . So with that code you do string lastLine = sArr[sArr.length-1] and when the compiler evaluates that it turned in to sArr[10-1] which is sArr[9] . Since you have 10 lines and the array is 0 indexed, then sArr[9] is your last line. That is what the code does.

sknake i think she was reffering to what I have posted about the:

arrayName.GetLength(dimension);

sonia sardana, the dimension in the bracket are A zero-based dimension of the array whose length needs to be determined.
In other words when you declaring a array like this:

string[] strArr=new string(5);

you creating en array of 2 dimension.
but of course you can create 3 dimension or more...!
Think of it 3D and you will understand what i mean.
If you still didnt understand you are welcome to google about multi dimension array and if you still didnt understand i will be happy to direct you to the answer.

Hello sir IdanS,ACtually i didnt know previously how to find the dimension of the array,,,,,dats y confused what is dimension,Cz i m newbie to ASP.net,,,Bot now i got it..Thx all for ur help.......

1) Suppose file contents.File NAME item.txt

P00001^^^^Apple^^^^50.00^^^^100

I have form name FrmSellProd.aspx on which i have to enter product id in txtSellProdID & if that ID exists in above file..I have to do some action.

Suppose user enters _P00001
ABove _indicates space.

protected void btnSellProduct_Click(object sender, EventArgs e)
    {
        string[] sArr ;
        string sPath = Server.MapPath("~/");
        string sFileName = sPath + "item.txt";
        int lCount;
        int lCurrentStock;
        int lSellingStock;
        

        if (File.Exists(sFileName.Trim()) == true)
        {
            sArr = System.IO.File.ReadAllLines(sFileName);
            for (lCount =0;lCount <= sArr.Length -1; lCount ++)
            {
                sArr = sArr[lCount].Split(new string[] { "^^^^" }, StringSplitOptions.None);

               [B] if (sArr[0].Trim  == txtSellProdID.Text.Trim )[/B]
                {
                    lCurrentStock = Convert.ToInt32(sArr[3]);
                    lSellingStock = Convert.ToInt32(txtQtyToSold.Text);
                }
             }
        }
       }

Error (BOLD LINE)

Error	1	Operator '==' cannot be applied to operands of type 'method group' and 'method group'

Can somebody help me out??

2) Secondly, 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.

Change:

if (sArr[0].Trim  == txtSellProdID.Text.Trim )

to

if (sArr[0].Trim()  == txtSellProdID.Text.Trim() )

THX VERY MUCH.......Atleast one probs is solved just because of U...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.