Click here to Skip to main content
15,888,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
System.ArgumentException: 'Input array is longer than the number of columns in this table.'


my code:
num = num + 1;
qty = Convert.ToInt32(QtyTb.Text);
totprice = qty * uprice;
table.Rows.Add(num,product,qty,uprice,totprice);
OrdersGv.DataSource = table;
flag = 0;
the exception shows at the
table.Rows.Add(num,product,qty,uprice,totprice);


What I have tried:

i am clueless about this exception
Posted
Updated 19-Jun-22 0:22am
Comments
CHill60 9-Jul-21 11:27am    
where have you defined 'table'?
Richard MacCutchan 9-Jul-21 11:31am    
Your table needs five columns, how many does it have?
meraj ali 6-Mar-23 9:02am    
Table.Columns.Add("Num", typeof(int));
Table.Columns.Add("Product", typeof(string));
Table.Columns.Add("qty", typeof(int));
Table.Columns.Add("uprice", typeof(int));
Table.Columns.Add("totprice", typeof(int));
Table.Rows.Add(num,Product,qty,uprice,Totpric);
order add in to Gv but when enter anther product first going to clear

The error says that your table does not have that many columns. Fix your code so that it does have enough columns.
 
Share this answer
 
num = num + 1;
qty = Convert.ToInt32(QtyTb.Text);
totprice = qty * uprice;

Add these lines here          
                DataTable table = new DataTable();
                table.Columns.Add("Num", typeof(int));
                table.Columns.Add("Product", typeof(string));
                table.Columns.Add("qty", typeof(int));
                table.Columns.Add("uprice", typeof(int));
                table.Columns.Add("totprice", typeof(int));
then continue your code

table.Rows.Add(num,product,qty,uprice,totprice);
OrdersGv.DataSource = table;
flag = 0;
 
Share this answer
 
Comments
CHill60 20-Jun-22 5:16am    
Won't work if table already exists (the declaration certainly does otherwise the OP's code would not have run), nor if any of the columns already exist. Better would be to find out how table is currently populated and to fix the underlying problem. However, the OP didn't respond a year ago so I doubt they will respond now

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