|
I guess you always throw an exception in the try block as well. If that's the case, the return statement would never execute and the compiler ignored the fact that you used tbl without assigning a value to it. If you remove the throw statement (either in the catch or in the try block), that line of code would execute and therefore the compiler complains.
|
|
|
|
|
I guess you are assigning value to tbl in try block don't you? If an exception occurs then tbl variable can be unassigned so if you remove throw new Exception(ex.Message); from your catch block then the function must return tbl but it will be unassigned hence the error. On the other hand, throwing new exception means that there is no need to return tbl.
By the way, why don't you use log4net[^] for logging purposes?
|
|
|
|
|
THX for the tips..
Giorgi Dalakishvili wrote: By the way, why don't you use log4net[^] for logging purposes?
Ignorance....I did not know it existed. I will look into that.
thanks again
Rafone
Statistics are like bikini's...
What they reveal is astonishing ...
But what they hide is vital ...
|
|
|
|
|
I found NLog[^] more accessible, although it appears not to have been updated for a while. NLog is derived from log4net. The forum is active though and 2.0 is in the works.
Kevin
|
|
|
|
|
Rafone wrote: Morning
It is early evening where I am.
Rafone wrote: If I eliminate the throw new Exception(ex.Message); line I get an error
Without seeing the rest of your code I'm guessing here.
You have, I guess, declared tbl outside of the try/catch block. Something is assigned to it inside the try block.
When you throw the exception in the catch the compiler knows that return tbl will always have something assigned to it, because in all other instances you are exiting the method before it can return it.
Also, your code contains a bit of a WTF. At the end of your catch you have throw new Exception(ex.Message) - By doing that you are discarding all the information in the current exception (there is a lot more in there besides the message you know). You should retain that information so that you can debug things properly in the future. You have two strategies:
1. throw new Exception(ex.Message, ex);
or
2. throw;
In this situation option #2 is preferable because you are adding no value in #1.
Finally, don't catch Exception, use a specific exception type. Don't throw an Exception either, again, use a specific exception type. If a specific type does not exist then create one.
|
|
|
|
|
Thanks for the explanation it has helped alot.
Colin Angus Mackay wrote: You should retain that information so that you can debug things properly in the future. You have two strategies:
1. throw new Exception(ex.Message, ex);
or
2. throw;
I have implemented option #2 here
Thanks for the advice...
Rafone
Statistics are like bikini's...
What they reveal is astonishing ...
But what they hide is vital ...
|
|
|
|
|
Rafone wrote: Error 3 Use of unassigned local variable 'tbl'.
That's pretty self explanatory. What is the code that is in the try block? You are attempting to return something for tbl when it has not been assigned anything. Not an error but rather a warning.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Hey guys.
In the try block I am populating a datset and a table(tbl) and doing some other processing then saving the data to disk. All of the code works in the try block.
I set up an exception in the file name that I am saving to to get some error logging going.
the catch is almost a verbatum translation from some older vb code that works fine.
Rafone
Statistics are like bikini's...
What they reveal is astonishing ...
But what they hide is vital ...
|
|
|
|
|
When you declare tbl, you aren't actually assigning anything to it. So, it probably looks something like this
DataTable tbl; Then you are assigning items to it in the try block. What you actually need to do is set it up like this
DataTable tbl = null; or better still, assign it in the try block and return it there. If there's an error then the return is superfluous, and it won't be hit.
|
|
|
|
|
I just finished reading a great article on Garbage Collection in the .NET Framework:
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/
This article says that a garbage collection occurs whenever a new object needs to be created and there is not enough room on the heap for it. It says that the garbage collection will clear out the garbage objects to make room for the new object, but what if you have a large number of live (non-garbage) objects and the garbage collector is unable to free enough space for the new object?
Can the size of the heap grow at this point?
If so, what is the heap's default size and by how much will it grow in this situation?
I have searched and I have been unable to find the answers to these questions documented anywhere. There needs to be some official documentation on this.
Thanks,
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|
|
Mogaambo wrote: Can the size of the heap grow at this point?
Yes, you can check it out yourself. Open Perfmon (type perfmon.msc at the command prompt), right click on the graph and select Add Counters, select .NET CLR Memory and select one of the heap related parameters (#Total Committed Bytes, for example). Select your process and click OK. You should be able to watch the GC heap expand as you create more objects and don't release much.
Mogaambo wrote: If so, what is the heap's default size and by how much will it grow in this situation?
I don't the heap's default size (Gen 0 + Gen 1 + Gen 2 + Large Object Heap), I would imagine it would vary by platform (x86 vs x64), workstation vs server etc. The max size of the heap would be restricted to the max virtual memory of a process, which is 2 GB (actually 4, but the OS takes 2) on x86.
|
|
|
|
|
I recently read about writing Unsafe code in C#. Can anyone provide me with a simple scenario where this can prove to be useful. I can then implement it to have a better understanding of it.
I also read about stackalloc . It seems to be a very efficient way of using arrays. Still can not see it that prominent in codes. What is reason for that?
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
|
|
|
|
|
d@nish wrote: Can anyone provide me with a simple scenario where this can prove to be useful.
When you have to call into unmanaged code.
d@nish wrote: I also read about stackalloc. It seems to be a very efficient way of using arrays. Still can not see it that prominent in codes. What is reason for that?
Because C# has its own managed mechanisms that are safer.
|
|
|
|
|
Thanks for reply.
Colin Angus Mackay wrote: When you have to call into unmanaged code.
In that case I can create a managed wrapper and consume it. Isn't it going to perform same thing?
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
|
|
|
|
|
Yes. In some circumstances you may wish to write your own wrapper, though IMO C++/CLI is a much better tool for the job in this sitation.
|
|
|
|
|
d@nish wrote: I recently read about writing Unsafe code in C#. Can anyone provide me with a simple scenario
Look for image processing articles. They typically use unsafe code to directly manipulate the bitmap using pointers.
d@nish wrote: I also read about stackalloc. It seems to be a very efficient way of using arrays.
1. The size of the stack is usually 1MB
2. The memory will be lost when the function returns.
|
|
|
|
|
Thanks. Will try that.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
|
|
|
|
|
Hi ,
I am getting out of memory exception, what i am trying to do is to read 25 text files, all files have over million records, and write them to database.
My approach....
1. Created a list of 25 datatable.
2. Read the column names from text file and add them to DataTable and with their datatype and size.
3. Pass this list to a function whose purpose is to read each file and add records to each of the datatable.
It is just an rough but an important overview.
In between i am running two progress bar.
but i get out of memory exception.
can anybody tell me any other approach......
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|
|
Your strategy is to dump the entire contents of the files in to memory then write them to the database. Why?
Why not just read the files one-by-one and write them to the database?
|
|
|
|
|
Colin Angus Mackay wrote: Your strategy is to dump the entire contents of the files in to memory then write them to the database. Why?
Because I am Passing That List Datables to a function whose purpose is to write data to table using sqlbulkcopy
Colin Angus Mackay wrote: Why not just read the files one-by-one and write them to the database?
So you want me to change strategy.
1. for( int i=0; i<totalnumberoffilestoscan;> {
Step1: // Create A New DataTable
Step2: // Call a Function That accepts the ith File and newly created DataTable as parameter and do the work of adding required number of Columns with their DataType and size
Step3: // Call a function that will read the text file row by row and add them to datatable
Step 4: // Pass this DataTable to another function whose purpose is to use SQlBulkCopy to write Records from the datatable.
}
Is This Strategy Feasible, will not be again give me out of memory Exception. in each loop i am creating a new Datatable ??????
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|
|
Mogaambo wrote: Is This Strategy Feasible, will not be again give me out of memory Exception. in each loop i am creating a new Datatable ??????
I don't know if it will work for you because I don't know enough about your data. I do know that it will use a lot less memory than you are using now.
If an individual file is large enough then you will still get an out of memory exception. It has been suggested to you else where that you read each file in chunks and process the chunks. That seems the most sensible strategy to me.
|
|
|
|
|
25 files x 1 Million rows = 25 Million
If you say each row is 1024 bytes. Its totally 25 GB of memory required. As you might already know, when RAM is not sufficent the system writes to harddisk & its slow. Unless you have free space of 40GB (to accomodate calculation issues), you cannot run your application & still it will be slow.
So your approach is to break it up into pieces. You got few approaches:
1. Dont use datatables and use memory streams, use SqlCommand - this is more code, i guess
2. If you want to use datatables. You have to run the BulkCopy, for each datatable & call DataTable.Dispose() method (and may be call to Garbage collector, as Dispose will not free memory immediatly) after each use.
3. Use SQL Server, to import the files directly. DTS / SISS supports importing of various file formats, excel, csv, access. It will need a SQL command, which will take the file location & it will import it. You can google up for the sql syntax or use DTS Export / Import wizard & in last step, save the command.
|
|
|
|
|
Didn't you already ask this question once? Actually, this is the 3rd time.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: Didn't you already ask this question once?
Yes, he did, like 10 days ago and got some replies. Must be some amount of density involved.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Paul Conrad wrote: Didn't you already ask this question once?
Yes I did , but check yourself what answers i received, are they fruitful
no no no no
So this time I asked my question in a different manner.
If, you can provide a good path then take thanks in Advance.
“You will never be a leader unless you first learn to follow and be led.”
–Tiorio
"Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
|
|
|
|