Solved

Correct code

Posted on 2016-07-22
3
45 Views
Last Modified: 2016-07-22
Hello,

Is there a better way of writing this:
 Public Function FindValue(ByVal tablename As String, ByVal StrCol1 As String, ByVal StrCol2 As String, ByVal StrValue As String)
        'get max value in the column
        MedicalSQL.OpenConnection()
        Dim SQLobj As New SqlCommand("al_FrmCol")
        Dim StrReturnValue As Object = String.Empty
        Try
            SQLobj.Parameters.AddWithValue("@tableName", tablename)
            SQLobj.Parameters.AddWithValue("@ColumnName1", StrCol1)
            SQLobj.Parameters.AddWithValue("@ColumnName2", StrCol2)
            SQLobj.Parameters.AddWithValue("@Value", StrValue)
            SQLobj.CommandType = CommandType.StoredProcedure
            SQLobj.Connection = MedicalSQL.GetConnection
            If SQLobj.ExecuteScalar Is DBNull.Value = False Then
                StrReturnValue = SQLobj.ExecuteScalar
            End If
            MedicalSQL.CloseConnection()
            Return StrReturnValue
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Function

Cheers
0
Comment
Question by:RIAS
3 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 41724745
you run the proc two times

dim res as object = SQLobj.ExecuteScalar
    If not( res Is DBNull.Value ) Then
                StrReturnValue = res
            End If
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 41724746
it looks like you are executing the command twice (if it is not null) which is not very efficient.

I would replace:
            If SQLobj.ExecuteScalar Is DBNull.Value = False Then
                StrReturnValue = SQLobj.ExecuteScalar
            End If

Open in new window


with:
StrReturnValue = SQLobj.ExecuteScalar
if string.isnullorwhitespace(StrReturnValue) then
   StrReturnValue = string.empty
End If

Open in new window

0
 

Author Closing Comment

by:RIAS
ID: 41724761
Cheers!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Capture logon name 13 75
get row value in vb.net 4 14
Following an example - Why do I need to click Save button twice? 3 26
vb.net 1 month apart 11 31
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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