Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

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")))
            dbCon.Open()
            Using adapter As New SqlDataAdapter(sqlSelect, dbCon)
                Using dt As New DataTable()
                    adapter.Fill(dt)

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Larry Brister

ASKER

JamesBUrger,
  Perfect...thanks.

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())
        Else
            objConn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("StrDev").ToString())
        End If
        Return objConn
    End Function
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.
JamesBurger

Thanks!