at the first glance my problem looks simple, still I can't tackle it. I have to do some corrections to an ASP.NET program a document mangement system, with its backendcode written in vb. The user in my specific case can choose several documents from a list, which can then be processed on a different webpage. So the ids of those documents are put into a sessionvariable and on the new page a query concerning only those ids is done. The implementation is quite horrid:
For Each Doks As String In Session.Item("choosenDocuments")
strSQL = "SELECT [...] FROM Dokument where DokumentID = " & Doks
cmd = New SqlCommand(strSQL, conDatabase)
dr = cmd.ExecuteReader
dr.Read()
[...]
dr.Close()
Next
I thought of using a parameterized statement, to create something like:
SELECT * FROM DOCUMENT WHERE DokumentID in (5,78,456)
But I fail to see, how to achive this programatically. Is is what i came up with so far:
Private Sub display(ByVal argument As IEnumerable(Of Integer))
Try
Dim result As New DataTable()
Using con As SqlConnection = getCon()
Dim querystring As String = "select * from Dokuments where " &
"GPDokumenteID in @id"
Using command As New SqlCommand(querystring, con)
command.Parameters.Add("@id", SqlDbType.Structured)
command.Parameters("@id").Value = argument
Using adapter As New SqlDataAdapter
adapter.SelectCommand = command
adapter.Fill(result)
End Using
End Using
End Using
For Each dr As DataRow In result.Rows
Console.WriteLine(dr(0).ToString() & " " & dr(1).ToString())
Next
Catch ex As Exception
Console.WriteLine("BOOM: " & ex.Message)
End Try
End Sub
This code fails at runtime, complaining that integer arrays cant be cast in IEnumerable.
I have found some sources like this or this but I still fail to construct a simple query example from that. Is my thinking to simple? Since there is such an elegant way to fill a SQL Query from a list I thought there would be a way to use this programatically.
Kind regards
Lorgarn