Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a datatable, and I am applying conditions to that.
C#
DataTable dt = new DataTable();
dt = (DataTable)Session["table"];
DataRow[] rows = dt.Select("Name = 'abc'");
dt.Clear();
dt.Rows.Add(rows);

When I add rows to datatable, I am getting an error,
Input array is longer than the number of columns in this table.

DataTable columns :
C#
if (Session["table"] != null)
{
        DataColumn col = new DataColumn();
        col.DataType = System.Type.GetType("System.Int32");
        col.ColumnName = "sno";
        DataTable dt = new DataTable();
        dt = (DataTable)Session["table"];
        if (!dt.Columns.Contains("RegNo"))
        {
                dt.Columns.Add("RegNo").SetOrdinal(6);
        }
        if (!dt.Columns.Contains(col))
        {
             dt.Columns.Add(col);
             col.SetOrdinal(0);
        }
        int index = 0;
        foreach (DataRow row in dt.Rows)
        {
               row.SetField(col, ++index);
        }
        DataRow[] rows = dt.Select("Name = 'abc'");
        dt.Clear();
        if (flag == 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                row.SetField("RegNo", "null");
            }
            flag = 1;
         }
         dt.Rows.Add(rows);
         gd.Visible = true;
         gd.DataSource = dt;
         gd.DataBind();
}

Please help. Thanks in advance
Posted
Updated 20-Feb-14 23:56pm
v2
Comments
Faisalabadians 21-Feb-14 5:41am    
Error is quite explaining. your data table has less columns then your new data row has columns.
make sure your number of columns in data table and new data row are same. Also look at column names, the should also be same.
srmohanr 21-Feb-14 5:42am    
I have checked no. of columns in both DataTable and DataRow
Faisalabadians 21-Feb-14 5:46am    
could you show your data table columns and row columns (source code) ?
srmohanr 21-Feb-14 5:53am    
if (Session["table"] != null)
{
DataColumn col = new DataColumn();
col.DataType = System.Type.GetType("System.Int32");
col.ColumnName = "sno";
DataTable dt = new DataTable();
dt = (DataTable)Session["table"];
if (!dt.Columns.Contains("RegNo"))
{
dt.Columns.Add("RegNo").SetOrdinal(6);
}
if (!dt.Columns.Contains(col))
{
dt.Columns.Add(col);
col.SetOrdinal(0);
}
int index = 0;
foreach (DataRow row in dt.Rows)
{
row.SetField(col, ++index);
}
DataRow[] rows = dt.Select("Name = 'abc'");
dt.Clear();
if (flag == 0)
{
foreach (DataRow row in dt.Rows)
{
row.SetField("RegNo", "null");
}
flag = 1;
}
dt.Rows.Add(rows);
gd.Visible = true;
gd.DataSource = dt;
gd.DataBind();
}
srmohanr 21-Feb-14 5:55am    
When dt.Clear() is executed, my rows[] also becoming null.

1 solution

http://social.msdn.microsoft.com/Forums/vstudio/en-US/f0753e31-efd0-44b8-b5a5-836e5f058420/remove-rows-from-datatable?forum=wpf[^]

Try to remove only the undesired rows :
C#
DataRow[] dr = null;//a datarow array
dr = dt.Select("Name != 'abc'"); //get the rows with matching condition in arrray
//loop throw the array and delete those rows from datatable
foreach (DataRow row in dr) {
    dt.Rows.Remove(row);
}
 
Share this answer
 
Comments
srmohanr 21-Feb-14 6:38am    
Its showing me an error 'Cannot interpret token '!' at position 6.'
srmohanr 21-Feb-14 6:44am    
I have changed the condition to
dr = dt.Select("Name <> 'abc'");
It is working fine.
Thanks for your help
Artefakt94 21-Feb-14 8:24am    
Yes, sorry for the "!=" ;)
You're welcome !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900