tags:

views:

702

answers:

3

how to add new column in new DataTable But Erorr

private void LoadtoList()
{
    SqlDataAdapter sa = new SqlDataAdapter("SELECT * FROM EmpSign", _constr);
    dt = new DataTable("CCTVTable");
    sa.Fill(dt);

    dtpass = new DataTable("CCTVpassword");
    SqlDataAdapter sa2 = new SqlDataAdapter("SELECT PASSWORD FROM Emp e WHERE e.EmpID IN ('f2123', 'f2124', 'f2126', 'rt015', 'f2133')",
        ConfigurationManager.ConnectionStrings["SaraburiEmp"].ConnectionString);
    sa2.Fill(dtpass);
    DataColumn dc = new DataColumn();
    dc = dtpass.Columns["password"];
    dt.Columns.Add(dc);
}


Column 'PASSWORD' already belongs to another DataTable. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Column 'PASSWORD' already belongs to another DataTable.

Source Error:

Line 1040:        DataColumn dc = new DataColumn();
Line 1041:        dc = dtpass.Columns["password"];
Line 1042:        dt.Columns.Add(dc);
Line 1043:    }
Line 1044:}
+2  A: 

First of all you must remove column from dtpass table:


...
DataColumn dc = dtpass.Columns["password"];
dtpass.Columns.Remove("password");
dt.Columns.Add(dc);

Tadas
hello, the value nothing in Datatable in object dt ?
monkey_boys
+2  A: 

You have explanation in exception message: "Column 'PASSWORD' already belongs to another DataTable"

Which basically means that You cannot reuse this column. You have to create new one (similar to this password column) and then add it to table.

Rafal Ziolkowski
A: 

error itself says that "Column 'PASSWORD' already belongs to another DataTable" .Because you are already use password in DataTable.

same thing as in DDL , you cant able to add multiple data column with same name in a single table :)

anishmarokey
Sorry, i have 2 table DataTable dt; DataTable dtpass;
monkey_boys
u never coding ado.net ?
monkey_boys
dt.Columns.Add("password", typeof(string)); for (int i = 0; i < dtpass.Rows.Count; i++) { dt.Rows[i]["password"] = dtpass.Rows[i]["password"].ToString(); }
monkey_boys
when you do SqlDataAdapter sa2 = new SqlDataAdapter("SELECT PASSWORD FROM Emp e WHERE e.EmpID IN ('f2123', 'f2124', 'f2126', 'rt015', 'f2133')", ConfigurationManager.ConnectionStrings["SaraburiEmp"].ConnectionString); sa2.Fill(dtpass);PASSWORD is set to datatable.how can you add again to same dt again ?
anishmarokey