none
An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Input array is longer than the number of columns in this table. RRS feed

  • Question

  • public DataTable LoadformItemsNamesDT(String FormName)
            {
                var formType = Assembly.GetExecutingAssembly().GetTypes()
                    .Where(a => a.BaseType == typeof(Form) && a.GetTypeInfo().Name == FormName)
                    .FirstOrDefault();

                Form f = (Form)Activator.CreateInstance(formType);
                DataTable formsItem = new DataTable();

                var MS = f.Controls.OfType<MenuStrip>();

                if (MS.Count() > 0)
                {

                    for (int i = 0; i < MS.ElementAt(0).Items.Count; i++)
                    {
                        String name = MS.ElementAt(0).Items[i].Name;
                        String Text = MS.ElementAt(0).Items[i].Text;
                        String mType = MS.ElementAt(0).Items[i].GetType().ToString();
                        // formsItems.Add(Text);
                        formsItem.Rows.Add(f.Name, name, Text, mType);


                   }

                }
                var BTN = f.Controls.OfType<Button>();
                if (BTN.Count() > 0)
                {
                    for (int i = 0; i < BTN.Count(); i++)
                    {
                        String name = BTN.ElementAt(i).Name;
                        String Text = BTN.ElementAt(i).Text; 
                        String mType = BTN.ElementAt(i).GetType().ToString();
                        // formsItems.Add(Text);

                        formsItem.Rows.Add(f.Name, name, Text, mType);
                    }
                }

                return formsItem;


            }
    Wednesday, May 23, 2018 1:47 PM

All replies

  • Hi Robin,

    This is because you created a datatable but you are adding rows without defining the columns. First you need to define columns and then add the data to the rows. Hope this will helps you.

    DataTable formsItem = new DataTable();
     
    formsItem .Columns.Add("Column1", typeof (string));
    formsItem .Columns.Add("Column2", typeof (string));
    formsItem .Columns.Add("Column3", typeof(string));
    formsItem .Columns.Add("Column4", typeof(string));

    Now you can write,

    formsItem.Rows.Add(f.Name, name, Text, mType);


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, May 23, 2018 8:41 PM
    Moderator