Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


VB Code OK?

Posted on 2013-06-10
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

730 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