Add column and row to datatable on codebehind showing error
Exception Details: System.ArgumentException: Input array is longer than the number of columns in this table.
i m adding one row in datatable on button click, means can add many rows on each add button click, on first time it's working fine, but on second or next time it showing this error , coz it not finding datable column.
protected void btnadd_Click(object sender, EventArgs e)
{
try
{
if (dt.Rows.Count == 0)
{
dt.Columns.Add("SNo", typeof(int));
dt.Columns.Add("Product", typeof(string));
dt.Columns.Add("Price", typeof(int));
dt.Columns.Add("Quantity", typeof(int));
dt.Columns.Add("CGST", typeof(string));
dt.Columns.Add("IGST", typeof(string));
dt.Columns.Add("SGST", typeof(string));
dt.Columns.Add("TaxPercent", typeof(int));
dt.Columns.Add("Tax", typeof(double));
dt.Columns.Add("Total", typeof(int));
dt.Columns.Add("Product_id", typeof(int));
}
}
catch { }
int productid = Convert.ToInt32(drpdwnproduct.SelectedValue.ToString());
string product_name = drpdwnproduct.SelectedItem.ToString();
int productprice = Convert.ToInt32(txtper_price.Text);
int quantity = Convert.ToInt32(txtquantity.Text);
string igst="";
string cgst="";
string sgst="";
int totalpr_price = 0;
int tax=Convert.ToInt32(txttax.Text);
int totalprice = Convert.ToInt32(productprice * quantity);
double totaltax=Convert.ToInt32((totalprice * tax) / 100);
if (chkinter.Checked == true)
{
igst = ((totalprice * tax) / 100).ToString();
totalpr_price = (((totalprice * tax) / 100)+totalprice) ;
cgst = "0";
sgst = "0";
}
else
{
cgst = (totaltax / 2).ToString();
sgst = (totaltax / 2).ToString();
igst = "0";
totalpr_price = (((totalprice * tax) / 100) + totalprice);
}
int rownumber;
try{
rownumber=dt.Rows.Count;
}
catch{
rownumber = 0;
}
int myrownumber=rownumber+1;
dt.Rows.Add(myrownumber, product_name, productprice, quantity, cgst, igst, sgst, tax, totaltax, totalpr_price,productid);
}
}
}