How to solve Input array is longer than the number of columns ? RRS feed

  • Question

  •  protected void Add_Click(object sender, EventArgs e)
                GridView1.Visible = true;
                ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);/*here Input is no longer than the number of columns*/
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                GridView1.DataSource = ds;
                GridView1.DataSource = null;

    Wednesday, January 31, 2018 8:40 AM

All replies

  • You're asking what to do when the input array is too long, but you gave us an example where this worst case does not happen? 

    To avoid an ArgumentException, where the array is larger than the number of columns in your table, you could check for the length of your array before applying the DataRowCollection.Add() method to it.

    object[] objects = ...;
    if(objects.Length > 3)
        //Do sth., e.g. issue a warning and return.
    Wednesday, January 31, 2018 10:26 AM
  • Hi Divya,

    When you create a table, you declare a specific number of columns, with names, header text, datatype and so forth - and the number of columns must match the number of objects you are inserting as a row. If you have created 4 columns in your table, and try to add 5 items, the system doesn't know what to do with them all and you get an exception.

    Like the following code:

    DataTable dt = new DataTable();
                dt.Columns.Add("Id", typeof(Int32));
                dt.Columns.Add("Column1", typeof(string));
                dt.Columns.Add("Column2", typeof(string));
                //dt.Rows.Add(1,"A","aa","aaa"); the table only has three column
                dt.Rows.Add(1, "A", "aaa");
                DataRow row = dt.NewRow();
                row["Id"] = 1;
                row["Column1"] = "A";
                row["Column2"] = "aaa";

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, February 1, 2018 2:14 AM
  • You can always check against the column count.


    If there is at least one row we can also check the ItemArray count 


    You can strongly type and target column in the row e.g. where dt is the DataTable.

    var row = dt.Rows.Add();
    row.SetField<string>("FirstName", firstName);

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 1, 2018 2:44 AM