Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
My Current Result in Table[0]
Id  |   Name    |  City
1   |  Akshada  |  Baroda
2   |  Rutu     |  Pune


I want to add Cast from Table[2] into Table[0]

I Want Below Answer

Id  |   Name    |  City    | Cast
1   |  Akshada  |  Baroda  | Bramhin
2   |  Rutu     |  Pune    | Rajput
3   |  Priya    |  Jaipur  | Muslim



I Want to add Cast using foreach loop


Check My DATA in Below Tables for Example
----------

Table[0]
Id  |   Name    |  City    
1   |  Akshada  |  Baroda  
2   |  Rutu     |  Pune    
3   |  Priya    |  Jaipur  

----------

Table [2]
Bramhin 
Rajput 
Muslim

----------


What I have tried:

objResult = objDataBl.Result_SelectAll_ForDisplay(intBatchId);

objResult.ResutlDs.Tables[0].Columns.Add("Cast", typeof(string));

foreach (DataRow row in objResult.ResutlDs.Tables[0].Rows)
{
     row["Cast"] = objResult.ResutlDs.Tables[2].Rows[0]["Column1"].ToString();          
} 




Using above code i got wrong ans
My Answer is
Id  |   Name    |  City    | Cast
1   |  Akshada  |  Baroda  | Bramhin
2   |  Rutu     |  Pune    | Bramhin 
3   |  Priya    |  Jaipur  | Bramhin 
Posted
Updated 5-Aug-18 18:17pm
v2
Comments

Simple: stop using row zero each time:
C#
row["Cast"] = objResult.ResutlDs.Tables[2].Rows[0]["Column1"].ToString();
                                                ^
                                                |
Instead, set up a integer variable outside your loop (or better use a for loop instead of a foreach) and use that instead of the constant value.
 
Share this answer
 
Comments
Akshada Sane 4-Aug-18 6:13am    
for (int i = 0; i < objResult.ResutlDs.Tables[0].Rows.Count; i++)
{
row["Denier"] = objResult.ResutlDs.Tables[2].Rows[i]["Column1"].ToString();
}
Using for loop got error in "row["Denier"]"

Error : "Then name does not exist"
OriginalGriff 4-Aug-18 6:25am    
:sigh:
When you replace a foreach with a for, you are responsible for creating the variable, that used to be automatically created, scoped, and filled by the foreach; and filling it with a value ...
Akshada Sane 4-Aug-18 7:00am    
It would be better if I could code and show it to me
OriginalGriff 4-Aug-18 7:09am    
Oh, come on! This is the trivial stuff!
foreach (MyClass mc in MyCollection)
   {
   ...
   }

Is equivelant to:
for (int i = 0; i < MyCollection.Count; i++)
   {
   MyClass c = MyCollection[i];
   ...
   }
You know that!
Akshada Sane 4-Aug-18 7:16am    
DataRow row = objResult.ResutlDs.Tables[0].NewRow();
for (int i = 0; i < objResult.ResutlDs.Tables[0].Rows.Count; i++)
{
row["Denier"] = objResult.ResutlDs.Tables[0].Rows[i];
}
Thank you all for helping me i got solution using below code



objResult.ResutlDs.Tables[0].Columns.Add("Cast", typeof(string));
for (int i = 0; i < objResult.ResutlDs.Tables[0].Rows.Count; i++)
{
     objResult.ResutlDs.Tables[0].Rows[i]["Cast"] = objResult.ResutlDs.Tables[2].Rows[i]["Cast"];
}
 
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