Solved

Correct code

Posted on 2016-07-22
3
35 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 69

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now