Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi My problem is this when I want to copy one data table items to another it copies but it does not bind to gridview on run time my code is below
C#
 foreach (string chk in City)
       {
           DataTable temps = new DataTable();
           foreach (string skls in keyskills)
           {
               cmd = new SqlCommand("Mat_Part2", cnn);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add("@Keys", SqlDbType.VarChar).Value = skls;
               cmd.Parameters.Add("@HighQualification", SqlDbType.VarChar).Value = HighQualification;
               cmd.Parameters.Add("@Exp", SqlDbType.Int).Value = TotalExpYear;
               cmd.Parameters.Add("@Location", SqlDbType.VarChar).Value = chk;
               da = new SqlDataAdapter(cmd);
               DataTable dt = new DataTable();
               da.Fill(dt);
               temps = dt.Clone();
               //DataRow dRow;
               //foreach(DataRow row in dt.Rows)
               //{
               //    dRow = temps.NewRow();
               //    dRow.ItemArray = row .ItemArray;
               //    temps.Rows.Add(dRow);
               //}
               temps = dt.Copy();
               temps.AcceptChanges();

           }
           mainTable = temps.Clone();
           mainTable = temps.Copy();
           mainTable.AcceptChanges();
           //DataRow dRow1;
           //foreach (DataRow row in temps.Rows)
           //{
           //    mainTable.ImportRow(row);
           //    //dRow1 = temps.NewRow();
           //    //dRow1.ItemArray = row.ItemArray;
           //    //mainTable.Rows.Add(dRow1);
           //}

       }
GridView1.DataSource = mainTable;
       GridView1.DataBind();



this no gives any exception.
Posted
Updated 3-Jan-13 17:46pm
v2
Comments
Sandeep Mewara 3-Jan-13 23:43pm    
Can you explain what you are trying to do here - first clone and then copy?
mainTable = temps.Clone();
mainTable = temps.Copy();
mainTable.AcceptChanges();
Dharmendra-18 3-Jan-13 23:47pm    
maintable is declare above from first foreach loop and this is for binding data to gridview

1 solution

this the solution
C#
int flg = 0,flg1=0;
     foreach (string chk in City)
     {
         foreach (string skls in keyskills)
         {
             cmd = new SqlCommand("Mat_Part2", cnn);
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.Add("@Keys", SqlDbType.VarChar).Value = skls;
             cmd.Parameters.Add("@HighQualification", SqlDbType.VarChar).Value = HighQualification;
             cmd.Parameters.Add("@Exp", SqlDbType.Int).Value = TotalExpYear;
             cmd.Parameters.Add("@Location", SqlDbType.VarChar).Value = chk;
             da = new SqlDataAdapter(cmd);
             DataTable dt = new DataTable();
             da.Fill(dt);
             if (flg == 0)
             {
                 mainTable = dt.Clone();
                 flg++;
             }
             DataRow dRow;
             foreach (DataRow row in dt.Rows)
             {
                 if (flg1 == 0)
                 {
                     mainTable = dt.Copy();
                     flg1++;
                 }
                 int xts = mainTable.Rows.Count;
                 for (int cv = 0; cv < xts; cv++)
                 {
                     dRow = mainTable.NewRow();
                     for (int gh = 0; gh < dt.Rows.Count; gh++)
                     {
                         int yst = Convert.ToInt32(dt.Rows[gh]["Id"].ToString());
                         int pretemp = 0;
                         int flag = 0;
                         while (pretemp < mainTable.Rows.Count)
                         {
                             int xst = Convert.ToInt32(mainTable.Rows[pretemp]["Id"].ToString());
                             if (xst == yst)
                             {
                                 flag = 1;
                             }
                             pretemp++;
                         }
                         if (flag == 0)
                         {
                             dRow.ItemArray = row.ItemArray;
                             mainTable.Rows.Add(dRow);
                         }
                     }
                 }

             }
         }
     }
 
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