Search code examples
c#asp.netstreamreader

My streamreader only reading one line at a time


string UploadFile = Server.MapPath("~/Uploads/");
UploadFile += FileUpload1.FileName;`
try
{
    if (File.Exists(UploadFile))
    {
        File.Delete(FileUpload1.FileName);
    }
    else
    {
        FileUpload1.PostedFile.SaveAs(UploadFile);
    }
}

This is where it goes wrong in this Finally where my While loop is located..

finally
{
    string InputLine, tempSTR, productname, strQuantity, strUnitprice;
    StreamReader ReadFile = new StreamReader(UploadFile);

    while (!ReadFile.EndOfStream)
    { 

        InputLine = ReadFile.ReadLine();

        tempSTR = InputLine.Substring(0, InputLine.IndexOf(","));
        productname = tempSTR;
        InputLine = InputLine.Substring(InputLine.IndexOf(",") + 1).Trim();

        tempSTR = InputLine.Substring(0, InputLine.IndexOf(","));
        strQuantity = tempSTR;
        InputLine = InputLine.Substring(InputLine.IndexOf(",") +1).Trim();

        strUnitprice = InputLine;

        b.NAME = productname;
        b.QUANTITY = int.Parse(strQuantity);
        b.UNITPRICE = int.Parse(strUnitprice);
        b.AddProducts();
    }

    GridView1.DataSource = b.ViewProducts();
    GridView1.DataBind();
}

In my while loop there's is the error when I try to upload several lines like:

Acer P166HQL LED Monitor, 97, 2999
Acer S200HQL LED Monitor, 50, 4499
DELL UltraSharp 2005FPW LCD Monitor, 22, 29999
Gateway KX1563 LED Monitor, 66, 2988


Solution

  • I don't know your file structure, but i think if you using parse this way, you haven't problem anymore :

    finally
    {
    
        string InputLine, tempSTR, productname, strQuantity, strUnitprice;
        StreamReader ReadFile = new StreamReader(UploadFile);
        string fileContent = ReadFile.ReadToEnd();
        fileContent = fileContent.Replace("\r\n",",");
        string[] splitedText = fileContent .Split(',');
        int j = 0;
        for(int i = 0;i < splitedText.Length ; i++)
        {
          switch(j)
          {
             case 0:
              j++;
              b.NAME = splitedText[i];
             break;
             case 1:
              j++;
              b.QUANTITY = int.Parse(splitedText[i]);
             break;
             case 2:
              j=0;
              b.UNITPRICE = int.Parse(splitedText[i]);
              b.AddProducts();
             break;
          }
        }
        GridView1.DataSource = b.ViewProducts();
        GridView1.DataBind();
    
    }
    

    UPDATE

    I change the entire code based on your comments :

      string s = @" Acer P166HQL LED Monitor, 97, 2999 ASRock Penryn 1600SLi, 42, 4688 Gateway KX1563 LED Monitor, 66, 2988";
                string[] splitedText = s.Split(' ');
                StringBuilder sr = new StringBuilder();
                int countOfCommas = 0;
                bool needComma = false;
                foreach (string item in splitedText)
                {
                    if (needComma)
                    {
                        needComma = false;
                        sr.Append(item).Append(',').Append(' ');
                    }
                    else if (item.Contains(","))
                    {
                        if (countOfCommas == 1)
                        {
                            countOfCommas = 0;
                            needComma = true;
                        }
                        else
                        {
                            countOfCommas++;
                        }
                        sr.Append(item).Append(' ');
                    }
                    else
                    {
                        sr.Append(item).Append(' ');
                    }
                }
    
                string str = sr.ToString();
                string[] splitedTextByComma = str.Split(',');
                int j = 0;
                for (int i = 0; i < splitedTextByComma.Length; i++)
                {
                    switch (j)
                    {
                        case 0:
                            j++;
                            string NAME = splitedTextByComma[i];
                            break;
                        case 1:
                            j++;
                            int QUANTITY = int.Parse(splitedTextByComma[i]);
                            break;
                        case 2:
                            j = 0;
                            int UNITPRICE = int.Parse(splitedTextByComma[i]);
                            break;
                    }
                }
            }
    

    the s variable has some values that your said.i hope this helps.