Solved

Correct code

Posted on 2016-07-22
3
56 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
[X]
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
3 Comments
 
LVL 143

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 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