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)
{
GetDataTable();
}
protected void GetDataTable()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
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;
da.Fill(dt);
}
}
}
dt.DataSet.EnforceConstraints = false;
DataTable dt2 = new DataTable();
dt2 = dt.Copy();
GridView1.DataSource = dt2;
GridView1.DataBind();
}
If you have any other question, please feel free to ask.
Best Regards,
Dillion