Dieter Stalmann
Dieter Stalmann

Reputation: 97

Calling a function in VB.NET based on function name stored in SQL

I have a table in SQL Server that has two fields: FileType and FunctionToCall. Depending on the file purpose, I want the code to execute a function to import the contents of the file to SQL Server, e.g if the first 4 characters of the filename are 'Pers', it is a file containing personal details of staff; if the first 4 characters of the filename are 'Exps', it is a file containing expenses of staff. Each of these files have a specific template that needs to be adhered to.

I use VB.NET to fill a list (listview or listbox) with the files (FileType) that can be imported.

Dim Function2bCalled As String = ""
lstIdentifiers.Items.Clear()

Dim myConn As SqlConnection
Dim SQLCmd As SqlCommand
myConn = New SqlConnection(SQLConnString)
Try
    myConn.Open()
    SQLCmd = New SqlCommand()
    SQLCmd.CommandText = "get_LU_FileUploadList"
    SQLCmd.CommandType = CommandType.StoredProcedure

    SQLCmd.Parameters.AddWithValue("@OrderBy", Odbc.OdbcType.NVarChar).Value = "Prefix"
    SQLCmd.Connection = myConn
    Using myReader = SQLCmd.ExecuteReader()

        If myReader.HasRows Then

            Do While myReader.Read()
                lstIdentifiers.Items.Add(myReader.Item("PrefixFileDescription").ToString())
                Function2bCalled = myReader.Item("PrefixFileDescription").ToString()
                AddHandler lstIdentifiers.Click, AddressOf Function2bCalled
            Loop

        End If
    End Using

Catch SQLEx As SqlClient.SqlException
    '-----  If there is a problem with the connection string syntax (i.e. server name is invalid) you will incur a SqlException
    LogAction(LoggedInName, AppTitle & " error: " & SQLEx.Message, "Error <SQL> - " & GetCurrentMethod().Name, GetCurrentMethod().DeclaringType().Name, "", DoingWhat)
Catch DataEx As DataException
    '----- Catch-all
    LogAction(LoggedInName, AppTitle & " error: " & DataEx.Message, "Error <Data> - " & GetCurrentMethod().Name, GetCurrentMethod().DeclaringType().Name, "", DoingWhat)
Catch GenEx As Exception
    LogAction(LoggedInName, AppTitle & " error: " & GenEx.Message, "Error <General> - " & GetCurrentMethod().Name, GetCurrentMethod().DeclaringType().Name, "", DoingWhat)
Finally
End Try

When the user drops a file onto the window, the code determines the file purpose (e.g. staff names) and looks in the list whether the file description is listed. If it is, I need the software to execute the related function.

I have done research on AddressOf, which is exactly what I need, but I cannot find anything that will work on lists. All the examples are done on button-clicks, which is a single control, whereas the listview or listbox has one or more items that each need to be linked to a function to be called, e.g. listitem 1 calls function ImportPersonalDetails, listitem 2 calls function ImportExpenses, etc.

Upvotes: -4

Views: 84

Answers (1)

Gary S
Gary S

Reputation: 46

Use CallByName or Reflection as discussed here:

Dynamically invoke properties by string name using VB.NET

In this example, the function calc is called using the string "calc", which could come from a SQL Server query or elsewhere.

Private Class Calculations
    Public Function calc(ByVal ParamArray args() As Integer) As Integer
       Return args(0) * 2
    End Function
End Class

Dim calculator As New Calculations
Dim args(0) As Integer
args(0) = 7
Dim result = Microsoft.VisualBasic.CallByName(calculator, "calc", Microsoft.VisualBasic.CallType.Get, args)

'result = 14

Upvotes: 0

Related Questions