Reputation: 7348
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:}
Upvotes: 0
Views: 4335
Reputation: 11397
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 :)
Upvotes: 0
Reputation:
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.
Upvotes: 2
Reputation: 4220
First of all you must remove column from dtpass table:
...
DataColumn dc = dtpass.Columns["password"];
dtpass.Columns.Remove("password");
dt.Columns.Add(dc);
Upvotes: 2