Solved

VB Code OK?

Posted on 2013-06-10
4
183 Views
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")))
            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

0
Comment
Question by:lrbrister
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
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.
0
 

Author Closing Comment

by:lrbrister
ID: 39235721
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
0
 
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.
0
 

Author Comment

by:lrbrister
ID: 39235921
JamesBurger

Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Injector with Web Service 4 41
C# bracket error 3 31
Regular expression help 2 25
SQL Help 27 45
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

773 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