VB Code OK?

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

Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
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.
lrbristerAuthor Commented:

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
Jacques Bourgeois (James Burger)PresidentCommented:
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.
lrbristerAuthor Commented:

All Courses

From novice to tech pro — start learning today.