DataTable.Copy getting error - cannot find relation 0 RRS feed

  • Question

  • User-893002196 posted

    Hi All,

    I try to copy all the dataTable with data to another new dataTable and trying to remove the column if type = object as below:-

     public static void bulkCopyIntoTable(DataTable dtData, string sDatabaseTableName, string sConnectionString)
                    DataTable dtDatas = new DataTable();
                    dtData.DataSet.EnforceConstraints = false;
                    dtDatas = dtData.Copy();  << Getting error as below
                    dtData.DataSet.EnforceConstraints = true;
                    dtDatas.Constraints.Add("PK_"+dtData.Columns[0].ColumnName, dtData.Columns[0], true);
                    for (int i = 0; i < dtDatas.Columns.Count; i++)
                        if (dtDatas.Columns[i].DataType.ToString().Contains("System.Object"))
                            var names = dtDatas.Columns[i].ColumnName;

    But, I getting error:

    cannot find relation 0

    Please advise,

    Thank you.



    Thursday, July 31, 2014 5:09 AM

All replies

  • User-271186128 posted

    Hi micnie2020,

    According to your code and description, it seems the issue is not related to the DataTable.Copy() Method. I have written a sample using the DataTable.Copy Method, and it worked well on my side. So, I would like to suggest you check your code to see if there is anything breaking. I suggest you set a breakpoints to check whether the tdData is empty or not. The following is the sample what I did, you could refer to it:

            protected void Page_Load(object sender, EventArgs e)
            protected void GetDataTable()
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                string constr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
                //Here I use NorthWind Database and I test the table Orders and Order Details, and they all worked well
                string query = "select * from [Order Details]";
                using (SqlConnection con = new SqlConnection(constr))
                    using (SqlCommand cmd = new SqlCommand(query))
                        using (SqlDataAdapter da = new SqlDataAdapter())
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = con;
                            da.SelectCommand = cmd;
                dt.DataSet.EnforceConstraints = false;
                DataTable dt2 = new DataTable();
                dt2 = dt.Copy();
                GridView1.DataSource = dt2;

    If you have any other question, please feel free to ask.

    Best Regards,

    Tuesday, August 5, 2014 5:56 AM