|
It's funny that it's called GrabDataSet instead of GetDataSet. What are you gonna do? Squeeze it until the data flows out.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|
|
I feel that "Strangle" would be the more appropriate verb.
|
|
|
|
|
He he he
Chris, The best comeback I have for that is "But ... Get did not work "
|
|
|
|
|
AspDotNetDev wrote: It's hard to find anything NOT wrong with this
M, actually no. I think there are things done right in there. What's wrong with this?
Dim oDAdapt As SqlClient.SqlDataAdapter
or this?
Dim cmdSQL As String = sSQL
And the author catches ALL exceptions, isn't that incredible?
And the user doesn't even know something bad occurred, how cool is that?
[Ok, ending sarcasm now ]
Full-fledged Java/.NET lover, full-fledged PHP hater.
Full-fledged Google/Microsoft lover, full-fledged Apple hater.
Full-fledged Skype lover, full-fledged YM hater.
|
|
|
|
|
I'm always curious as to why people insist on declaring ALL variables inside a try/catch block. It's not as though declaring the first three variables there are likely to cause an exception (nor that instantiating the DataSet and SqlDataAdapter will cause problems).
|
|
|
|
|
I'm fairly certain the person that made this code did not consider such things. As an example:
Dim oRs As New DataSet
oRs = New DataSet
An instance is created, then shortly after another instance is created. And I really don't like when the parens aren't put after calling a constructor.
|
|
|
|
|
AspDotNetDev wrote: I really don't like when the parens aren't put after calling a constructor
I seem to remember reading somewhere that the VB editor removes the parens from the default constructor. That was an old version, but it wouldn't surprise me if this behaviour was still present.
|
|
|
|
|
Just tested it.
VB doesn't remove them but it doesn't add them neither so if you type them yourself they'll stay there. 
|
|
|
|
|
VB only requires parens when it's necessary to add parameters. Otherwise, you can slip by without them.
XAlan Burkhart
|
|
|
|
|
Whaddyaknow
Thats a straight copy from MSDN
|
|
|
|
|
How can you trust a machine?
"Fear no factor", Prime Numbers.
|
|
|
|
|
Aside from redundant variable declarations and swallowed exceptions, I enjoy the way the author disposes of the DataSet and DataAdapter after the method has returned. It's the only way to be sure..
Also: VB.net and inherent naming convention make me grieve for the the 18 months I lost before I moved to C# and started calling things by names that infer even the slightest insight to their usage.
|
|
|
|
|
Trying to open a MessageBox in a webservice in case of an exception is also not the brightest idea. It was probably commented out because of 'unexpected' side effects, like somebody having to go to the server to click it away
At least artificial intelligence already is superior to natural stupidity
|
|
|
|
|
I've seen the MessageBox-without-a-GUI before, in a windows service but I didn't know it would "work" in a webservice too. Funnily enough, I've never tried this 
|
|
|
|
|
I might use this as a test question for students to identify all mistakes. It would worth an easy 5 marks. I love the hint that the connection is opened at some stage and then left open. Bonus mark for picking that up. In fact maybe there whole ADO.NET component of the test could be that one question
"You get that on the big jobs."
|
|
|
|
|
I am trying to learn better development practices so I would gratified if somebody could confirm what I see as well as show me the ideal replacement for this code.
Issues with the code
1) Sending in a SQL statement seems to be invitation for SQL injection
2) Exception handling seems to have been done exclusively so that the end user never sees a problem and nothing else
3) The Dispose statements seem to be useless .
Now is it good practice to open and close database connections , I have been reading elsewhere about using the Singleton pattern for database connections which in some cases do leave the connection open. Why is it essential to use dispose for the objects that were declared inside the function ? We know that they will be returned to the GC as soon as the function exits.
|
|
|
|
|
Regarding the issues, there are a few more, like initializing a variable only to re-initialize a few lines later, and others.
In .NET especially, there is connection pooling for database connections, meaning that even after you close a connection, the connection is kept alive by the framework, and next time you open the connection, the existing connection will be reused. No comment on the Singleton pattern.
It is considered a best practice to always call the Dispose method on a Class that implements IDisposable. This is why there is a
using (resource) { } construct built into the language. Classes usually implement IDisposable to indicate that they are using external resources which need to be released once you are finished with the class.
In .NET, once a method has completed (returned), any instances created that have no other references can be collected by the Garbage Collector (GC). However, because the GC decides to clean the memory in its own time, this can sometimes mean that instances remain alive for longer than necessary. For example, on a computer with a lot of RAM, the GC may not run for a long time, as there is no issue with Memory.
Another issue with relying on the GC is that the GC will not call the Dispose method on an instance, as it doesn't know anything about IDisposable . What it does is call the Finalize method (known as the Finalizer), which all classes inherit from Object . However, the way in which GC calls the Finalizer means that the instance has to be kept alive for longer than absolutely necessary, at minimum until the next GC collection. Additionally, not all classes necessarily implement a Finalizer.
If a class used external resources, such as a file or a database connection and doesn't release the resource, the external resources may remain inaccessible even after the .NET application closes.
|
|
|
|
|
Thank you for your comments. I had no idea that GC uses some form of Lazy scheduling. Any pointers on documentation that would guide me towards the accepted standards for .Net programming?
|
|
|
|
|
Accepted standards for programming should theoretically be the same for most languages, such as using recognized Design Patterns. This can depend on the type of environment you are working in, which can be desktop, web, mobile, cloud, etc. There are plenty of blogs and sites out there that discuss these issues.
For more information on .NET Garbage Collection and IDisposable/Finalizers, you can check out the following links:
Garbage Collection[^] - Covers Garbage Collection in .NET, with details about the how it works.
Cleaning Up Unmanaged Resources[^] - Discusses Disposing and Finalizing in .NET.
|
|
|
|
|
Thank you . Unfortunately the web seems to be a rather disparate source of information with more biases than useful information so I did not have much luck .I just finished the Heads First Patterns book and now I got the GoF book and intend to read it soon. Thanks for the info re Garbage collection
|
|
|
|
|
From my experience, the best way to learn is to read as much as you can, but at the same time to write as much code as possible. There are a number of sites out there with programming puzzles, which you may find interesting and instructive.
A good mentor is also an excellent way to learn those subjects, as they should be able to help guide you from experience, which is the best way to learn any subject. Code reviews are also a great way to gain experience.
Thing is, I think most of programming theory is going to be opinionated. Look at what's happening with the MVC pattern today: There is MVC, MVP, MVVM, there are even frameworks out there that just call themselves MV-Something or MV*. All of them are slightly different, even those that call themselves MVC.
Patterns are a shared language for design, not a particular implementation, so sometimes they are interpreted differently, or can/should be implemented differently in a particular technology.
Remember to keep an open mind: there is always more than one way of accomplishing your goal.
|
|
|
|
|
I think you hit the nail on the head, reading is key but application is more important. I seem to have a hard time adapting what I am reading unless it is blindingly obvious. What are the sites with programming puzzles that you are referring to. I have been writing code for almost 17 years now in various languages and platforms but I discovered Patterns just months ago (though I suspected their existence for years)and I am slowly realizing that the vast majority of my applications were a horrible implementation of MVC and Active Record patterns. I think I am a voracious reader but in the past I did not want to read anything related to programming or design but I am trying to change that. A good mentor seems to be extremely hard to find .
Again thanks for your time
|
|
|
|
|
Generally an excellent post, however this:
Schmuli wrote: If a class used external resources, such as a file or a database connection and doesn't release the resource, the external resources may remain inaccessible even after the .NET application closes.
... isn't true on a decent operating system (and Windows qualifies these days), because when the process ends all its resource ties will be killed by the OS.
But yes, relying on the GC to clean up after you is a bad plan, because there's no guarantee that the GC will run immediately, or even ever.
|
|
|
|
|
BobJanova wrote: ... isn't true on a decent operating system (and Windows qualifies these days),
because when the process ends all its resource ties will be killed by the
OS.
Actually, he was right in certain cases. The example I'm thinking of is an Oracle connection. If the connection isn't disposed, even if the underlying application dies, the connection may be left open. This was a real pain point for us on a project that we had to interface with. It took me and the client DBA ganging up on the project lead before he ensured the connections were closed.
All of a sudden, 300-400 active connections were reduced to less than a handful.
|
|
|
|
|
Are you sure the process was being killed? This sounds like it might have been a web app and IIS would have been the process in that case, so you'd be relying on the GC to come along and help you.
File handles, open sockets and properly registered graphical resources are definitely released when a process ends under Windows.
Obviously, it's still good practice to close your resources manually because you don't want to make the user close your application to free them!
|
|
|
|