I have a table-valued parameter like this
CREATE TYPE dbo.Loc AS TABLE(Lo integer);
My stored procedure looks like this:
ALTER PROCEDURE [dbo].[T
There are some examples of how to do this at http://msdn.microsoft.com/en-us/library/bb675163%28v=vs.110%29.aspx (see the section titled "Passing a Table-Valued Parameter to a Stored Procedure ").
The simplest thing would seem to be filling a DataTable
with the values the user selected and passing that to the stored procedure for the @locations
parameter.
Perhaps something along the lines of (note I don't have VB.NET installed, so treat this as an outline of how it should work, not necessarily as code that will work straight away):
cnt = LSTlocations.SelectedItems.Count
' *** Set up the DataTable here: *** '
Dim locTable As New DataTable
locTable.Columns.Add("Lo", GetType(Integer))
If cnt > 0 Then
For i = 0 To cnt - 1
Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
' *** Add the ID to the table here: *** '
locTable.Rows.Add(locid)
next
end if
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd23 As New SqlCommand("IBS_TransactionSummary", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = startdate
cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate
' *** Supply the DataTable as a parameter to the procedure here: *** '
Dim tvp1 As SqlParameter =cmd23.Parameters.AddWithValue("@location", locTable)
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.Loc"
da.SelectCommand = cmd23
da.Fill(ds)
cnt = LSTlocations.SelectedItems.Count
' *** Set up the DataTable here: *** '
Dim locTable As New DataTable
locTable.Columns.Add("Lo", GetType(Integer))
If cnt > 0 Then
For i = 0 To cnt - 1
Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
' *** Add the ID to the table here: *** '
locTable.Rows.Add(locid)
next
end if
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd23 As New SqlCommand("IBS_TransactionSummary", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = startdate
cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate
' *** Supply the DataTable as a parameter to the procedure here: *** '
Dim tvp1 As SqlParameter =cmd23.Parameters.AddWithValue("@location", locTable)
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.Loc"
da.SelectCommand = cmd23
da.Fill(ds)
Table types SQL SERVER 2008
if you version of sql server > = 2008.