why the InvalidOperationException in foreach loop which prints contents of database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hawabi
    New Member
    • May 2013
    • 2

    #1

    why the InvalidOperationException in foreach loop which prints contents of database

    Code:
    DataRow[] returnedrows = data.Tables["off"].Select("empid='" + employeeid + "'");
    
                count = returnedrows.Length;
                if (count > 0)
                {
                    foreach(DataRow dr in Table_main.Rows)
                    {
    
                        Console.WriteLine(dr["empid"] + "\t" + dr["empname"] + "\t" + dr["designation"]);
                        Console.WriteLine("select the operation you would like to perform");
                        Console.WriteLine("1.UPDATE INFORMATION \n2.DELETE EMPLOYEE INFORMATION \n3.EXIT\n");
                        int info_emp;
                        string info = Console.ReadLine();
                        int.TryParse(info, out info_emp);
                        
                        
                        switch (info_emp)
                        {
                            case 1:
                                
                                Console.WriteLine(">>>>>>please enter the NEW DESIGNATION u want to update to<<<<<<<<");
                                string desg = Console.ReadLine();
                                cmd.CommandText = "UPDATE off SET designation='"+desg+"' WHERE empid='" + employeeid + "';";
                                cmd.ExecuteNonQuery();
    
                                cmd.CommandText = "select * from off where empid='" + employeeid + "'";
                                adap.SelectCommand = cmd;
                                adap.Fill(data, "off");
                                
    
                                 int.TryParse(id, out employeeid);
                                //adap.Update(data, "off");
                                 trans.Commit();
                                
                                 
                                    foreach (DataRow myDataRow in data.Tables["off"].Rows)
                                         {
                                         Console.WriteLine("EMPLOYEEID = " + myDataRow["empid"] );
                                        
                                         Console.WriteLine("\tEMPLOYEEName = " + myDataRow["empname"]);
                                         
                                         Console.WriteLine("\tDESIGNATION = " + myDataRow["designation"]+"\n");
                                        
                                          }
                            break;
                            case 2:
                                cmd.CommandText = "delete from off where empid='" + employeeid + "';";
                                cmd.ExecuteNonQuery();
                                Console.WriteLine("DELETE SUCCESSFULLLLL");
                                trans.Commit();
                                break;
                            case 3:
                                Environment.Exit(0);
                                break;
                            default:
                                Console.WriteLine("Invalid selection");
                                break;
                        }
                    }
    
                }
                else
                {
                    char c;
                    Console.WriteLine("SORRY...THE EMPLOYEE ID IS INCORRECT..do you want to add new employee?");
                    string input= Console.ReadLine();
                    char.TryParse(input, out c);
                    while (c == 'y')
                    {
                        Console.WriteLine("ENTER EMPLOYEE NAME :");
                        string employeename = Console.ReadLine();
                        Console.WriteLine("ENTER DESIGNATION :");
                        string employeedesg = Console.ReadLine();
    
    
    
                        cmd.CommandText = "insert into off (empid,empname, designation)" + " values " + "('" + employeeid + "',' " + employeename + "','" + employeedesg + " ')";;
                        cmd.ExecuteNonQuery();
                        Console.WriteLine("RECORD INSERTED SUCCESSFULLLY");
                        cmd.CommandText = "select * from off";
                        adap.SelectCommand = cmd;
                        adap.Fill(data, "off");
                        DataTable Table = data.Tables["off"];
                        foreach (DataRow myDataRow in Table.Rows)
                        {
                            Console.WriteLine("EMPLOYEEID = " + myDataRow["empid"]);
                            Console.WriteLine("\t");
                            Console.WriteLine("EMPLOYEEName = " + myDataRow["empname"]);
                            Console.WriteLine("\t");
                            Console.WriteLine("DESIGNATION = " + myDataRow["designation"]);
                            Console.WriteLine("\t");
                            
                        }
                        break;
                      
                    }
                    Console.WriteLine("press q to exit");
                    input = Console.ReadLine();
                    char.TryParse(input, out c);
                    while (c == 'q')
                        Environment.Exit(0);
                         
               
                }
    
    
                conn.Close();
    
                Console.ReadLine();
    
            }
    Last edited by Rabbit; May 3 '13, 03:12 PM. Reason: Please use code tags when posting code.
  • Hawabi
    New Member
    • May 2013
    • 2

    #2
    How do i edit this for each loop??

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Please use code tags when posting code.

      Which line causes the error?

      Comment

      Working...