Solved

Correct code

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

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

26 Experts available now in Live!

Get 1:1 Help Now