Question

1) I'm trying to read a text file but it shows error Like

foreach statement cannot operate on variables of type 'System.IO.StreamReader' because 'System.IO.StreamReader' does not contain a public definition for 'GetEnumerator'

Here is my C# code...

 using (StreamReader sr = new StreamReader(@"D:\test1.txt"))
            {
                while (sr.Peek() >= 0)
                {
                    //contents of foreach loop go here

                        foreach (var line in sr)
                        {
                            var items = line.Split(new[] { '\t', '\n' }).ToArray();
                            if (items.Length != 3)
                                continue;
                            var Name = items[0].ToString();
                            var Email = items[1].ToString();
                            var Pwd = items[2].ToString();
                            cmd.CommandText = "insert into Employees values('" + Name + "','" + Email + "','" + Pwd + "')";
                            cmd.CommandType = CommandType.Text;
                            cmd.ExecuteNonQuery();
                        }

                }
Was it helpful?

Solution

Basically, what Alex.K comment say:

using (StreamReader sr = new StreamReader(@"D:\test1.txt"))
{
    while (sr.Peek() >= 0)
    {
        var line = sr.ReadLine();
        var items = line.Split(new[] { '\t', '\n' }).ToArray();
        if (items.Length != 3)
            continue;
        var Name = items[0].ToString();
        var Email = items[1].ToString();
        var Pwd = items[2].ToString();
        cmd.CommandText = "insert into Employees values('" + Name + "','" + Email + "','" + Pwd + "')";
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
}

You don't need foreach inside peek check loop, just read a line.

Here is a typical use from MSDN, which is very clear of how to read lines:

        using (StreamReader sr = new StreamReader(path)) 
        {
            while (sr.Peek() >= 0) 
            {
                Console.WriteLine(sr.ReadLine());
            }
        }

Edit

To avoid famous SQL injection vulnerability, deal with parameters using Parameters:

// this is baaaaad
cmd.CommandText = "insert into Employees values('" + Name + "','" + Email + "','" + Pwd + "')";

// will become
cmd.CommandText = "insert into Employees values(@Name, @Email, @Pwd)";
cmd.Parameters["@Name"].Value = Name;
cmd.Parameters["@Email"].Value = Email;
cmd.Parameters["@Pwd"].Value = Pwd;

// somewhere before, where you create command
cmd.Parameters.Add("@Name", type);
cmd.Parameters.Add("@Email", type);
cmd.Parameters.Add("@Pwd", type);

OTHER TIPS

The error is pretty clear.

You can only iterate over via foreach on elements that implements the GetEnumerator method, and StreamReader isn't.

There are plenty of excellent and good example for StreamReader just look one up.

This is going to work for this. Instead of ForEach Loop, use a while loop

using (StreamReader sr = new StreamReader(@"D:\test1.txt"))
 {
      while (sr.Peek() >= 0)
      {
       //contents of foreach loop go here

       while ((line = sr.ReadLine()) != null)
       {
       var items = line.Split(new[] { '\t', '\n' }).ToArray();
       if (items.Length != 3)
                    continue;
       var Name = items[0].ToString();
       var Email = items[1].ToString();
       var Pwd = items[2].ToString();
       cmd.CommandText = "insert into Employees values('" + Name + "','" + Email + "','" + Pwd + "')";
       cmd.CommandType = CommandType.Text;
       cmd.ExecuteNonQuery();


          }
  }

you can use for in this type

foreach (var line in File.ReadLines(@"D:\test1.txt"))
{
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top