Solved

Copy DataTable Structure without Data

Posted on 2004-08-05
9
3,430 Views
Last Modified: 2008-06-21
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





0
Comment
Question by:Saravanu
  • 4
  • 3
9 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 50 total points
ID: 11725173
Hi,

there is a method called Clone() that do exactly what you want

Regards,
B..M
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 11725335
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.
0
 

Author Comment

by:Saravanu
ID: 11725435
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?



0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11725576
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 28

Expert Comment

by:mmarinov
ID: 11733147
Hi Saravanu,

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

B..M
0
 

Author Comment

by:Saravanu
ID: 11733158
please let me know how to change the grade
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11733174
as a Dot.Net Page Editor i can unaccept the answer and you can change it

B..M
0
 

Author Comment

by:Saravanu
ID: 11733184
pls do that
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now