Link to home
Start Free TrialLog in
Avatar of Saravanu
Saravanu

asked on

Copy DataTable Structure without Data

I have a one datatable dt1 with some data

I need to create a new datatable dt2 and I shud copy the structure of dt1 without copying data.

is it possible ?

I know it is possible to copy both data and structure using copy method





ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me get it right.

You have a table in your database and you need to create exact copy of that , but not the data. A one time job. right ?

If yes,

SELECT TOP 0 * INTO <NewTableName> FROM <ExistingTableName>

This will work with MS SQL Server and Access.
Avatar of Saravanu
Saravanu

ASKER

B..M

No way denying  points u deserve

but when I refer .Clone I get NullException error , y is that..

I have a dataTable which has been set in Page_Load (!isPostBack)

I am using that dataset now in a button click event.

when I refer that dataTable.Clone am getting nullexception error.

how to solve this?



the syntax for using Clone method is

DataTable dt = _srouceDataTable.Clone();

in your case may be the datatable object is null in the button's click event.
you have to trace(debug) your application and check where the datatable became null

Regards,
B..M
Hi Saravanu,

Can you tell me why B Grade?
Check this in the rules of EE : https://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP.NET/help.jsp#hi73

B..M
please let me know how to change the grade
as a Dot.Net Page Editor i can unaccept the answer and you can change it

B..M
pls do that