473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

why the InvalidOperationException in foreach loop which prints contents of database

2
Expand|Select|Wrap|Line Numbers
  1. DataRow[] returnedrows = data.Tables["off"].Select("empid='" + employeeid + "'");
  2.  
  3.             count = returnedrows.Length;
  4.             if (count > 0)
  5.             {
  6.                 foreach(DataRow dr in Table_main.Rows)
  7.                 {
  8.  
  9.                     Console.WriteLine(dr["empid"] + "\t" + dr["empname"] + "\t" + dr["designation"]);
  10.                     Console.WriteLine("select the operation you would like to perform");
  11.                     Console.WriteLine("1.UPDATE INFORMATION \n2.DELETE EMPLOYEE INFORMATION \n3.EXIT\n");
  12.                     int info_emp;
  13.                     string info = Console.ReadLine();
  14.                     int.TryParse(info, out info_emp);
  15.  
  16.  
  17.                     switch (info_emp)
  18.                     {
  19.                         case 1:
  20.  
  21.                             Console.WriteLine(">>>>>>please enter the NEW DESIGNATION u want to update to<<<<<<<<");
  22.                             string desg = Console.ReadLine();
  23.                             cmd.CommandText = "UPDATE off SET designation='"+desg+"' WHERE empid='" + employeeid + "';";
  24.                             cmd.ExecuteNonQuery();
  25.  
  26.                             cmd.CommandText = "select * from off where empid='" + employeeid + "'";
  27.                             adap.SelectCommand = cmd;
  28.                             adap.Fill(data, "off");
  29.  
  30.  
  31.                              int.TryParse(id, out employeeid);
  32.                             //adap.Update(data, "off");
  33.                              trans.Commit();
  34.  
  35.  
  36.                                 foreach (DataRow myDataRow in data.Tables["off"].Rows)
  37.                                      {
  38.                                      Console.WriteLine("EMPLOYEEID = " + myDataRow["empid"] );
  39.  
  40.                                      Console.WriteLine("\tEMPLOYEEName = " + myDataRow["empname"]);
  41.  
  42.                                      Console.WriteLine("\tDESIGNATION = " + myDataRow["designation"]+"\n");
  43.  
  44.                                       }
  45.                         break;
  46.                         case 2:
  47.                             cmd.CommandText = "delete from off where empid='" + employeeid + "';";
  48.                             cmd.ExecuteNonQuery();
  49.                             Console.WriteLine("DELETE SUCCESSFULLLLL");
  50.                             trans.Commit();
  51.                             break;
  52.                         case 3:
  53.                             Environment.Exit(0);
  54.                             break;
  55.                         default:
  56.                             Console.WriteLine("Invalid selection");
  57.                             break;
  58.                     }
  59.                 }
  60.  
  61.             }
  62.             else
  63.             {
  64.                 char c;
  65.                 Console.WriteLine("SORRY...THE EMPLOYEE ID IS INCORRECT..do you want to add new employee?");
  66.                 string input= Console.ReadLine();
  67.                 char.TryParse(input, out c);
  68.                 while (c == 'y')
  69.                 {
  70.                     Console.WriteLine("ENTER EMPLOYEE NAME :");
  71.                     string employeename = Console.ReadLine();
  72.                     Console.WriteLine("ENTER DESIGNATION :");
  73.                     string employeedesg = Console.ReadLine();
  74.  
  75.  
  76.  
  77.                     cmd.CommandText = "insert into off (empid,empname, designation)" + " values " + "('" + employeeid + "',' " + employeename + "','" + employeedesg + " ')";;
  78.                     cmd.ExecuteNonQuery();
  79.                     Console.WriteLine("RECORD INSERTED SUCCESSFULLLY");
  80.                     cmd.CommandText = "select * from off";
  81.                     adap.SelectCommand = cmd;
  82.                     adap.Fill(data, "off");
  83.                     DataTable Table = data.Tables["off"];
  84.                     foreach (DataRow myDataRow in Table.Rows)
  85.                     {
  86.                         Console.WriteLine("EMPLOYEEID = " + myDataRow["empid"]);
  87.                         Console.WriteLine("\t");
  88.                         Console.WriteLine("EMPLOYEEName = " + myDataRow["empname"]);
  89.                         Console.WriteLine("\t");
  90.                         Console.WriteLine("DESIGNATION = " + myDataRow["designation"]);
  91.                         Console.WriteLine("\t");
  92.  
  93.                     }
  94.                     break;
  95.  
  96.                 }
  97.                 Console.WriteLine("press q to exit");
  98.                 input = Console.ReadLine();
  99.                 char.TryParse(input, out c);
  100.                 while (c == 'q')
  101.                     Environment.Exit(0);
  102.  
  103.  
  104.             }
  105.  
  106.  
  107.             conn.Close();
  108.  
  109.             Console.ReadLine();
  110.  
  111.         }
May 3 '13 #1
2 1404
Hawabi
2
How do i edit this for each loop??
May 3 '13 #2
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code.

Which line causes the error?
May 3 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mike P | last post by:
In a foreach loop, how do you break out of the iteration but not the loop? Break and continue both seem to exit the loop, I need something that for example on an if statement will cease execution...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
25
by: David C | last post by:
I posted this question, and from the replies, I get the impression that I worded my posting very poorly, so let me try this again. While debugging and stepping through this foreach loop ...
10
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) {...
3
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop,...
1
by: Perl Beginner | last post by:
I hope i can articulate this question properly. i have been trying to figure this out for over a week. I am comparing the contents of two files, but the comparison is done inside of a foreach...
1
by: djmeltdown | last post by:
I'm having trouble getting a foreach() loop to insert a record into a MySQL Database. Which I've never had any trouble before, it just seems quirky. I've tried the mysql_query statement without a...
2
by: somacore | last post by:
VS2005, C# Windows Form I can't quite figure out how to do this, but i think I'm close. I have a groupbox which contains 8 radio buttons, and I need to figure out which one is checked for entry...
15
by: fjm | last post by:
Hello everyone and happy Sunday. :) I have a problem that I *think* I may know the solution to but have no idea how to write the code for it. I am working on a templating system wher I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.