5 Replies - 320 Views - Last Post: 21 July 2014 - 03:17 AM Rate Topic: -----

#1 Noorul_A  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-July 14

Value is not updating in dt

Posted 21 July 2014 - 12:26 AM

DataTable BU_dt, Udt;
Udt = Util_dt(1);//getting this dt from one excel file
Udt.Columns.Add("%BU");
BU_dt = BUFile_dt(1);//getting this dt from another excel file
int i = 0;
for (int i = 0; i < BU_dt.Rows.Count-1;i++ )
{
if (Udt.Rows[i][0]==BU_dt.Rows[i][0])
{
Udt.Rows[i][4]= BU_dt.Rows[i][1].ToString();

}
}

}
Both Udt and BU_dt have same columns. I am trying to compare each in both columns then copying the value from Bu_dt to Udt 5th column. but the above code copying only the first value alone.

Is This A Good Question/Topic? 0
  • +

Replies To: Value is not updating in dt

#2 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Value is not updating in dt

Posted 21 July 2014 - 01:46 AM

first you need to enclose your codes with this one [ code ]"Your codes here"[ /code ]
to make it easier to read.
int i = 0;
for (int i = 0; i < BU_dt.Rows.Count-1;i++ )
{
if (Udt.Rows[i][0]==BU_dt.Rows[i][0])
{
Udt.Rows[i][4]= BU_dt.Rows[i][1].ToString();

}
}

}


second:
why are you declaring and initializing your int variable "i" twice?..
you should try removing that line and see what happends next. and also check the actual number of rows you have on on your "BU_dt" table. that way you can start debugging your code and see where it went wrong.

third here's a helpful tutorial from andrewsw on how to Dedbug your code.

This post has been edited by jimzcoder: 21 July 2014 - 01:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 Noorul_A  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-July 14

Re: Value is not updating in dt

Posted 21 July 2014 - 02:02 AM

 for (int i = 0; i < Udt.Rows.Count - 1; i++)
            {
                for (int j = 0; j < BU_dt.Rows.Count - 1; j++)
                {
                    if (Udt.Rows[i].ItemArray[0].ToString().Contains(BU_dt.Rows[j].ItemArray[0].ToString()))
                    {
                        Udt.Rows[i][4] =BU_dt.Rows[j].ItemArray[1].ToString();
                        
                    }
                     else
                    {
                        Udt.Rows.Add(BU_dt.Rows[j].ItemArray[1].ToString());
                    }


This works fine. But the problem is in else part. If the value is not in Udt when comparing BU_dt. that row sholud add a row in Udt with the 0th and 4th column filled.
  Udt.Rows.Add(BU_dt.Rows[j].ItemArray[1].ToString());

this adds row with oth column filled for every incremental of j
Was This Post Helpful? 0
  • +
  • -

#4 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Value is not updating in dt

Posted 21 July 2014 - 02:13 AM

for (int i = 0; i < Udt.Rows.Count - 1; i++) //The logic of this loop will only get the actual items on your datatable -1, you don't really have to subtract the row count by 1 because if "i" will be equal to Udt.Rows.Count the loop automatically stops. try changing it. to 
for (int i = 0; i < Udt.Rows.Count; i++)
02
           {
03
               for (int j = 0; j < BU_dt.Rows.Count - 1; j++)
04
               {
05
                   if (Udt.Rows[i].ItemArray[0].ToString().Contains(BU_dt.Rows[j].ItemArray[0].ToString()))
06
                   {
07
                       Udt.Rows[i][4] =BU_dt.Rows[j].ItemArray[1].ToString();
08
                        
09
                   }
10
                    else
11
                   {
12
                       Udt.Rows.Add(BU_dt.Rows[j].ItemArray[1].ToString());
//you can simply add the rows in here try not to override it with the ToString function.
                       Udt.Rows.Add(BU_dt.Rows[j]);//this will add the the selected object into the other table.

13
                   }


Hope it helped.

This post has been edited by jimzcoder: 21 July 2014 - 02:14 AM

Was This Post Helpful? 1
  • +
  • -

#5 Noorul_A  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-July 14

Re: Value is not updating in dt

Posted 21 July 2014 - 02:58 AM

For this code
 Udt.Rows.Add(BU_dt.Rows[j]);

i am getting exception at this line. exception states that this row currently belongs to another table
Was This Post Helpful? 0
  • +
  • -

#6 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Value is not updating in dt

Posted 21 July 2014 - 03:17 AM

you can use the import row instead of add.

Udt.ImportRow(BU_dt.Rows[j]);
//that should do the trick

//check also this line here
for (int j = 0; j < BU_dt.Rows.Count - 1; j++)
//you also need to change it if you really want to loop through all the rows available into:
for (int j = 0; j < BU_dt.Rows.Count; j++)




here's the link from ms.
Hope it helped good luck
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1