Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

EXECUTE SCALAR WITH PARAMETER

Hi All,

I have below code :

 Public Function Get_Member_Name(ByVal strMemberId As String) As String

        Try

            Dim dic As New Dictionary(Of String, String)
            dic.Add("@MemberId", strMemberId)

            sbdSQLCommand.Length = 0

            sbdSQLCommand.AppendLine("SELECT")
            sbdSQLCommand.AppendLine("ISNULL(FullName, '') AS FullName")
            sbdSQLCommand.AppendLine("FROM TMMEMBERDATA ")
            sbdSQLCommand.AppendLine("WHERE MemberId = @MemberId")

            Using conApp As SqlConnection = New SqlConnection(strSQLCompanyConnection)
                conApp.Open()

                Dim cmdSqlCommand As New SqlCommand

                cmdSqlCommand = ERV_Global_Web.Query_Data(dic)

                cmdSqlCommand.Connection = conApp
                cmdSqlCommand.CommandText = sbdSQLCommand.ToString
                cmdSqlCommand.CommandType = CommandType.Text

                Get_Member_Name = cmdSqlCommand.ExecuteScalar

                If Get_Member_Name Is Nothing Then Get_Member_Name = ""

                Return Get_Member_Name
            End Using

        Catch ex As Exception

        End Try

        Return ""

    End Function

   Public Shared Function Query_Data(ByVal dctOfParameterVariables As Dictionary(Of String, String)) As SqlCommand

        Dim cmdSqlCommand As New SqlCommand

        Dim pair As KeyValuePair(Of String, String)
        For Each pair In dctOfParameterVariables
            cmdSqlCommand.Parameters.AddWithValue(pair.Key, _
                                                  pair.Value)
        Next

        Return cmdSqlCommand

    End Function

What's wrong with my code ?

Thank you.
Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

What happens?
Avatar of emi_sastra
emi_sastra

ASKER

Always return nothing.

Thank you.
I can't see anything obviously wrong... I'd step through it in the debugger and confirm that the right values are being passed.
I am sorry, it works.

I have simplified into :


  Public Function Get_Member_Name(ByVal strMemberId As String, _
                                    Optional ByVal blnMemberName As Boolean = False) As String

        Try

            sbdSQLCommand.Length = 0

            sbdSQLCommand.AppendLine("SELECT")

            If blnMemberName Then
            Else
                sbdSQLCommand.AppendLine("ISNULL(FullName, '') AS FullName")
            End If

            sbdSQLCommand.AppendLine("FROM TMMEMBERDATA ")
            sbdSQLCommand.AppendLine("WHERE MemberId = @MemberId")

            Dim dic As New Dictionary(Of String, Object)
            dic.Add("@MemberId", strMemberId)

            Return ERV_Global_Web.Execute_Scalar(sbdSQLCommand.ToString, dic, strSQLCompanyConnection)

        Catch ex As Exception

        End Try

        Return ""

    End Function

  Public Shared Function Execute_Scalar(ByVal strSQLCommand As String, _
                                          ByVal dctOfParameterVariables As Dictionary(Of String, Object), _
                                          ByVal strConnection As String, _
                                          Optional ByVal strSQLType As String = "S") As Object

        Try

            Using conApp As New SqlConnection(strConnection)

                Dim cmdSqlCommand As New SqlCommand

                cmdSqlCommand = ERV_Global_Web.Query_Data(dctOfParameterVariables)

                cmdSqlCommand.Connection = conApp
                cmdSqlCommand.CommandText = strSQLCommand
                cmdSqlCommand.CommandType = CommandType.Text

                conApp.Open()

                Execute_Scalar = cmdSqlCommand.ExecuteScalar

                If Execute_Scalar Is Nothing Then
                    Return Return_Empty_Value(strSQLType)
                End If

            End Using

        Catch ex As Exception

        End Try

        Return ""

    End Function

 Public Shared Function Return_Empty_Value(ByVal strSQLType) As Object

        Select Case strSQLType
            Case "S"
                Return ""
            Case "D"
                Return System.DBNull.Value
            Case "N"
                Return 0
        End Select

        Return Nothing

    End Function



Could it be made more robust ?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In web app.
Regarding the connection string, should we always call a function to get it every time a query is going to counduct?

Thank you.
In a web app I would store it in the connection strings section of web.config.
Yes, and just use it ?

Thank you.
Would you please provide simple sample of database access library ?

Thank you.
Unfortunately I can't. But roughly just take what you have and make it generic. A query pretty much needs 2 things, the SQL and the parameters. So create a static class library with the methods you need to access the database e.g. GetValue(String Sql, Dictionary Params), ExecuteSql(String Sql, Dictionary Params), GetRecordSet(String Sql, Dictionary Params).

HTH
Ok. Is there any link that is discuss about it ?

Thank you.
I don't know of any, but seriously its hardly more than you've done, just generic so you can use the same code from 2 pages.
Ok then.

Thank you very much for your help.
This is pretty useful overview of data access - its old now, but still mostly relevant.

http://msdn.microsoft.com/en-us/library/ee817654.aspx
It is great.

Thank you.