Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
protected DataTable tblGridRow()
{
int counta = Convert.ToInt32(Session["AddToListCount"]);
DataRow dr = dt.NewRow();
for (int i = 0; i < counta; i++)
{

dr["LG_ITEM_DSC"] = oDS.Tables[0].Rows[0]["LG_ITEM_DSC"].ToString();
dr["MFR_SKU_NUM"] = oDS.Tables[0].Rows[0]["MFR_SKU_NUM"].ToString();
string Price = oDS.Tables[0].Rows[0]["LIST_AMT"].ToString();
int length = Price.Length;
dr["LIST_AMT"] = Price.Remove(length - 2, 2);
dr["BRND_LOGO"] = oDS.Tables[0].Rows[0]["BRND_LOGO"].ToString();
dr["INV_UN_CDE"] = oDS.Tables[0].Rows[0]["INV_UN_CDE"].ToString();
dt.Rows.Add(dr);
}
return dt;
}
Posted
Comments
Member 10184990 2-Aug-13 6:44am    
error is (This row already belongs to this table.)in dt.Rows.Add(dr);

i have define all var
Jean A Brandelero 2-Aug-13 7:19am    
Just put "DataRow dr = dt.NewRow();" inside the loop.

This question was previously posted, solutions were given and then it was deleted, why I do not know.

But this is the solution I provided:

Your issue relates to when the new datarow is created. I would change your code to this:

protected DataTable tblGridRow()
{
   int counta = Convert.ToInt32(Session["AddToListCount"]);
   
   for (int i = 0; i < counta; i++)
   {
      DataRow dr = dt.NewRow();
      dr["LG_ITEM_DSC"] = oDS.Tables[0].Rows[0]["LG_ITEM_DSC"].ToString();
      dr["MFR_SKU_NUM"] = oDS.Tables[0].Rows[0]["MFR_SKU_NUM"].ToString();
      string Price = oDS.Tables[0].Rows[0]["LIST_AMT"].ToString();
      int length = Price.Length;
      dr["LIST_AMT"] = Price.Remove(length - 2, 2);
      dr["BRND_LOGO"] = oDS.Tables[0].Rows[0]["BRND_LOGO"].ToString();
      dr["INV_UN_CDE"] = oDS.Tables[0].Rows[0]["INV_UN_CDE"].ToString();
      dt.Rows.Add(dr);
   }
   return dt;
}


I would also consider changing your for loop because at the moment it will just input the same line X number of times (where X = counta).

I would suggest changing the for loop declaration to this:

int countLimit = counta <= oDS.Tables[0].Rows.Count ? counta :  oDS.Tables[0].Rows.Count;
for (int i = 0; i &lt; countLimit ; i++)


I would also replace all instances of
Rows[0]
with
Rows[i]


If that solves your issue please accept the answer.
 
Share this answer
 
Comments
Adarsh chauhan 2-Aug-13 8:19am    
nice one.. +5
Just move
DataRow dr = dt.NewRow();

inside the loop.
 
Share this answer
 
Comments
Adarsh chauhan 2-Aug-13 8:18am    
I agree.. +5
Try this,
protected DataTable tblGridRow()
{
int counta = Convert.ToInt32(Session["AddToListCount"]);
for (int i = 0; i < counta; i++)
{
DataRow dr = dt.NewRow();

dr["LG_ITEM_DSC"] = oDS.Tables[0].Rows[i]["LG_ITEM_DSC"].ToString();
dr["MFR_SKU_NUM"] = oDS.Tables[0].Rows[i]["MFR_SKU_NUM"].ToString();
string Price = oDS.Tables[0].Rows[i]["LIST_AMT"].ToString();
int length = Price.Length;
dr["LIST_AMT"] = Price.Remove(length - 2, 2);
dr["BRND_LOGO"] = oDS.Tables[0].Rows[i]["BRND_LOGO"].ToString();
dr["INV_UN_CDE"] = oDS.Tables[0].Rows[i]["INV_UN_CDE"].ToString();
dt.Rows.Add(dr);
}
return dt;
} 


I hope this will help you..
 
Share this answer
 

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