Skip to content

C#, Restful Web Api on MVC4

Abdullah Zafar

Asked Feb 14, 2014, 8:13 AM EST1 answer
Share
LinkedInX
i am new in c#, and making a  Restful API on  MVC4,
 i am using following code and gets an error on
 " foreach (DataRow menuDR in menuDataSet.Tables [7].Rows)"
 
IndexOutOfRangeException was unhandled by user code, and in details in show "cannot find table7"
i used the same approach with 1 table database and it was working, i don't know how to handle this exception and tried various things to get it sort.... 
 
C#
  public IEnumerable<RestaurantsMenu> GetAllMenu()
        {
                menuCon = new SqlConnection ("Data Source=MyPc\\MyPc;Initial Catalog=MENU;Integrated Security=True");
                menuDataAdapter = new SqlDataAdapter (" SELECT * FROM RestaurantsMenu ", menuCon);

                foreach (DataRow menuDR in menuDataSet.Tables [7].Rows)
                {
                    Rst_Menu.Add(new RestaurantsMenu(){Restaurant_ID = int .Parse (menuDR[0].ToString()), RestaurantName =menuDR[1].ToString(),
                        Menu =menuDR[2].ToString(),  Price=int .Parse (menuDR[3].ToString()) }   );

                }
                return Rst_Menu;
        }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    This error means that you are indexing for a table that does not exist. Don't forget that indexing is Zero based. Meaning that if you have a DataSet with 7 tables, then the seventh table will have an index of 6.  

    Example:

    Dataset.Tables[0] = 1st table.
    Dataset.Tables[2] = 2nd table.
    ...
    Dataset.Tables[6] = 7th table.
    0