Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, January 13, 2009 4:18 AM
Dear friends,
How can I destroy a variable in ASP.Net(C#).
If anybody knows, please answer
Regards
Shivan
All replies (15)
Friday, January 16, 2009 12:14 AM âś…Answered
Hello,
DataTable.Clear() > clears both columns and rows, so try it out.
Another option is use , datatable.reset --> to set the data table in a re-initialization set.
Another alternative, is create a new instance of data table when you want to create it/fill it with new data using 'New' statement.
Thanks,
Dharnendra
Tuesday, January 13, 2009 4:56 AM
What type of variable you want to destory
Tuesday, January 13, 2009 5:13 AM
this way
//Create Variable
String str = "MAK";
//Destry Varaiable
str = null;
//Create object
DataSet ds = new DataSet();
//Destroy object
ds.Dispose();
Tuesday, January 13, 2009 12:32 PM
How can I destroy a variable in ASP.Net(C#).
If anybody knows, please answer
Generally you dont. The .Net garbage collector manages memory for you. If you have items that need deterministic lifetime managment, have that object implment IDisposable.
Tuesday, January 13, 2009 11:29 PM
Then how to destroy a int variable
Tuesday, January 13, 2009 11:57 PM
this way
create
int val = 21;
destroy
val = null;
Wednesday, January 14, 2009 8:48 AM
this way
create
int val = 21;
destroy
val = null;
That will not compile. Even if val is made a nullable type by declaring it like this: int? val = 21; setting it to null does not destroy it, it just has a value of null.
Value type variables are "destroyed" when they go out of scope:
{
// val exists in the scope of {}
int val = 21;
}
// val is "destroyed"
However, keep in mind, the memory is not overwritten. Until the memory is overwritten for some other use, a memory sniffer type program could have access to it.
Wednesday, January 14, 2009 8:53 AM
Then how to destroy a int variable
Variables allocated on the stack generally never need to be explicitly freed. This was true even in C++. Things that you would call free/delete for in C/C++, what C# calls reference types, are automatically destroyed for you when they go out of scope / lose all references to them. BTW mudassarkhan's last code snippet wont compile, ignore it. If you can give an example of what you're trying to do in C, C++, Pascal, Ada or FORTRAN I think it would be helpful.
Wednesday, January 14, 2009 11:45 PM
I am using dataTable to temporarily display data in GridView.So in one function I wrote code for adding fields and in another function for adding records.
For the first time it runs perfectly.
Then while running it for the second time, it displays error that column name already exists.
So I think that the datatable is not getting cleared while stopping the program.
Wednesday, January 14, 2009 11:45 PM
I am using dataTable to temporarily display data in GridView.So in one function I wrote code for adding fields and in another function for adding records.
For the first time it runs perfectly.
Then while running it for the second time, it displays error that column name already exists.
So I think that the datatable is not getting cleared while stopping the program.
Thursday, January 15, 2009 12:13 AM
I am using dataTable to temporarily display data in GridView.So in one function I wrote code for adding fields and in another function for adding records.
For the first time it runs perfectly.
Then while running it for the second time, it displays error that column name already exists.
So I think that the datatable is not getting cleared while stopping the program
Can you paste the code??
Thursday, January 15, 2009 12:37 AM
Clear the data table before you fetching the data.
After the clear statement, user AcceptChanges() property.
I seem that might be solve your probs , if not please paste your code for review.
Thanks,
Dhams
Thursday, January 15, 2009 11:29 PM
I am able to clear the fields using the command
datatable.columns.clear();
But now my problem is that the rows are not cleared.
ie, At Page_Load event I cleared and added the columns, In btnAdd_Click I added the rows and bind it to the GridView.
I gave datatable.clear() in btnAdd_Click, but the rows are not getting cleared which was created in the last session.
Friday, January 16, 2009 12:07 AM
datatable.Rows.Clear();
Try this it should clear all the rows
Friday, January 16, 2009 12:39 AM
Thank it is working