Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
I am trying to merge two data table dt and dt4. While using the given below code getting one error. The error is : "'column' argument cannot be null." Help me to find a proper soluion. Thank you.

Code:

C#
protected void ddlCircle_SelectedIndexChanged(object sender, EventArgs e)
{
    ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter cd;
    cd = new ShadingAnalysisDataSetTableAdapters.tbl_CadEngineersTeamTableAdapter();
    DataTable dt = new DataTable();
    dt = cd.GetAvailableData(ddlCircle.SelectedValue); // Getting details of unassigned site

    int x, y;

    DataTable dt3 = new DataTable();
    dt3 = cd.GetTeam();
    y = dt3.Rows.Count;

    x = dt.Rows.Count; // counting the unassinged sites

    DataTable dt2 = new DataTable();
    dt2 = cd.GetAssignTeam(x);           //Getting team based on count

    string[] arr = new string[dt2.Rows.Count];
    int i = 0;
    foreach (DataRow r in dt2.Rows)
    {
        arr[i] = r["Team"].ToString(); // assigning available team to array
        i++;
    }

    string[] strArr = new string[x+1]; // another array to copy arr values.

    i = 0; int j = 0;
    while (j <= x)
    {
        strArr[j]=  arr[i] ; // copying the arr[] values into strArr[] based on count.
        i++;
        j++;

        if (i == y)
        {
            i = 0;
        }
    }
     DataTable dt4 = new DataTable();
     dt4.Columns.Add("Team");

     foreach (string s in strArr)
       {
          dt4.Rows.Add(s); // Converting string array strArr[] to data table dt4
        }

     dt.Merge(dt4);  // error poppup here. Error : 'column' argument cannot be null.
     GridView2.DataSource = dt;
     GridView2.DataBind();
}


dt contain

C#
State  District  SiteID  SiteName
-----  --------  ------  --------
Sate1  District1  1001     A
Sate2  District2  1002     B
Sate3  District3  1003     C


dt4 contain

C#
Team
-----
Team1
Team2
Team3


I need a final output as:

C#
State  District  SiteID  SiteName  Team
-----  --------  ------  --------  -----
Sate1  District1  1001     A       Team1
Sate2  District2  1002     B       Team2
Sate3  District3  1003     C       Team3
Posted
Comments
V5709 29-Oct-14 7:54am    
you can use concepts like inner join to fetch details which will reduce your code

SQL
select a.*,b.team from table_name1 as a,table_name2 as b
 
Share this answer
 
Comments
aarif moh shaikh 29-Oct-14 8:31am    
He asking about Data Table .. not Database Tables
you can't use Merge for this Case better you could use Join in Datatable


Refer this:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28284094.html[^]
 
Share this answer
 
try like this
C#
string[] strArr = { "a", "b", "c" };
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("NAME");

            dt.Rows.Add("1", "NAME1");
            dt.Rows.Add("2", "NAME2");
            dt.Rows.Add("3", "NAME3");
            dt.Columns.Add("Team");
            int iCount = 0;
            
            foreach (DataRow item in dt.Rows)
            {
                item["Team"] = strArr[iCount];
                iCount++;
            }

            dt.AcceptChanges();
 
Share this answer
 
v2

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