VB Code OK?

Posted on 2013-06-10
Last Modified: 2013-06-10
Is the following VB.Net code ok?

Normally would never use the actual select but inherited it and will move to other process.

    Private Sub LoadPanelBar(ByVal _s As String)
        Dim sqlSelect As String = Nothing
        sqlSelect = "Select id,parentID, panelText,CASE maintenance WHEN 0 THEN navigateUrl ELSE '~/maintenance.aspx?mPage=' + REPLACE([panelText],' ','%20') END AS navigateUrl,imageUrl,expandedImageUrl from dbo.activeDirPanelBar where id in (" & _s & ") and active = 'True' order by webpagesort, sort"

        Using dbCon As SqlConnection = ngSqlConnectionString(Clng0(AppConfig("isProduction")))
            Using adapter As New SqlDataAdapter(sqlSelect, dbCon)
                Using dt As New DataTable()

                    'Set Panel Paramaters
                    RadPanelBar1.DataTextField = "panelText"
                    RadPanelBar1.DataNavigateUrlField = "navigateUrl"
                    RadPanelBar1.DataFieldID = "id"
                    RadPanelBar1.DataFieldParentID = "parentId"
                    RadPanelBar1.Skin = "Web20"
                    RadPanelBar1.Width = 204
                    RadPanelBar1.DataSource = dt
                End Using
            End Using
        End Using
    End Sub

Open in new window

Question by:lrbrister
  • 2
  • 2
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 total points
ID: 39235529
In line 2, a simple thing that is not important, but for the knowledge base in your brain, you do no need to set the string to Nothing, it is already Nothing when you Dim it. If the environment is set to maximum checking, you might get a warning from time to time if you do not set a string when you declare it, but since you give it a value in the line that follows, declaring to Nothing is really useless. In fact, you could do the following:

Dim sqlSelect As String = "Select id,parentID, panelText,CASE maintenance ... "

I would give a proper name to the control. Having to do maintenance on an application that keeps the default name for the controls is a pain. Something like RadPanelBarProduction (or rpbProduction if you have defined standard prefixes to use with controls) would be more appropriate.

When you use a connection with Fill, you do not need to Open it, Fill opens and closes it automatically. But if you do Open it as you do, you need to Close it when done with it, something that you do not do. So either remove the Open (this is recommended with a single Fill like this one), or Close it once the table is created, that is after the Fill.

The rest of the code looks OK at first sigth, but I cannot be completely sure. Since the Connection is created in what looks like a custom method that is not shown(ngSqlConnectionString), I cannot comment on that part. And since I am not familiar with the RadPanelBar control, I cannot vouch for the way you initialize it, but it basically follows the way ones usually works with a databound control.

Author Closing Comment

ID: 39235721

My whole point was I didn't really understand the Using and the Fill I guess.
I changed the Dim statement
I removed the Open() statement

As far as the control, this is a text page.  I usually give them a descriptive name
The connection string works

In the Web COnfig I set isProduction to an integer

I then consume with the following

    Public Function AppConfig(ByVal i_key As String) As String
        Return System.Configuration.ConfigurationManager.AppSettings(i_key)
    End Function

    Public Function ngSqlConnection(ByVal live As Integer, ByVal checked As Boolean) As SqlConnection
        Dim objConn As SqlConnection = Nothing
        If live = 1 Then
            objConn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("StrConn").ToString())
            objConn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("StrDev").ToString())
        End If
        Return objConn
    End Function
LVL 40
ID: 39235907
Using is a way to declare some variable in a way that the garbage collector can do a better job. The garbage collector (or GC) is the object that manage the memory after an object is not in use anymore.

Declaring a variable with Using...End Using is a (most often) more convenient way of declaring with the standard Dim and then calling Dispose on the object when finished with it. Although usually not necessary, it can improve performance sometimes, specially when you are working with objects that require a lot of memory, as is the case with a DataTable.

Note that it won't work with all types of variable, the class defining the variable must have been prepared to be able to participate in a Using. The compiler will tell you if you ever try to call Using on a type that does not permit it.

Open should be called in order to connect to a database. Close should follow it when you are finished with the connection. But as stated before, you do not need them when you call Fill, because Fill does both operations for you. If you Open before the Fill however, Fill will leave the connection opened and it is thus your job of closing the connection.

Author Comment

ID: 39235921


Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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 is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

10 Experts available now in Live!

Get 1:1 Help Now