-4

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.

13
  • 5
    Please add the programming language you are using, and some more info about the environment. (Because "The code loads the available file extensions from SQL into a listview" is unclear/not-useful without this context) Commented Dec 28, 2024 at 10:39
  • My apologies. VB.NET Commented Dec 30, 2024 at 0:13
  • This still applies: "Because "The code loads the available file extensions from SQL into a listview" is unclear/not-useful without this context". Can you share a minimal reproducible example of your problem in code ? It should be made clear why you cannot use AdressOf in combination with Listbox or Listview, because you did not share any code, and did not make clear what your exact problem is. What is it that you exactly need, that you think your are missing? Commented Dec 30, 2024 at 9:27
  • It is still unclear to me what this means, "but I cannot find anything that will work on lists", but I cannot get AddressOff working, even without lists, so .... (voting to re-open)😉 Commented Dec 30, 2024 at 11:28
  • 1
    Looks like the conversation has gone astray. I'll just hard-code the items into the list, and call the functions individually. Commented Jan 1 at 7:32

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.