title: Pass multiple selected items in Listbox to stored procedure
body: <p>There are two listboxes. [lstid and lstselected]. When user selects one or multiple ID in [lstid] and click move right. The selected items will be in [lstselected]   </p>
  
  <p>I am trying to pass the multiple value from [lstselected] to stored procedure and bind the data to [gridview] but received the following error.</p>
  
  <p>[startIndex cannot be larger than length of string.]</p>
  
  <p>Appreciate that anyone can help on this?</p>
  
  <p><strong>ASP</strong></p>
  
  <pre><code>&lt;%@ Page Language="vb" AutoEventWireup="false" CodeBehind="reimbursementlb.aspx.vb" Inherits="WebApplication1.WebForm1" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head id="Head1" runat="server"&gt;
  &lt;title&gt;Reimbursenment Report&lt;/title&gt;
      &lt;style type="text/css"&gt;
          .auto-style1 {
              width: 153px;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form2" runat="server"&gt;
  &lt;div&gt;
   &lt;table align=center&gt;
  &lt;tr&gt;
  &lt;td align="center" class="auto-style1" &gt;
      &lt;h4&gt;Reimbursement ID &lt;/h4&gt;
  &lt;/td&gt;
  &lt;td&gt;
      &lt;h4&gt;&lt;/h4&gt;
      &lt;/td&gt;
  &lt;td&gt;
      &lt;h4&gt;
  &lt;b&gt;Selected ID&lt;/b&gt;&lt;/h4&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td class="auto-style1"&gt;
  &lt;asp:ListBox ID="lstid" runat="server"  Height="175px" Width="131px" SelectionMode="Multiple" DataSourceID="SqlDataSource1" DataTextField="ID" DataValueField="ID"&gt;
  &lt;/asp:ListBox&gt;
      &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:CLMSConnectionString %&gt;" SelectCommand="SELECT [ID] FROM [PaymentConsoleClosing] ORDER BY [ID] DESC"&gt;&lt;/asp:SqlDataSource&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;asp:Button ID="btnMoveRight" runat="server" Text="&gt;" Width="45px" onclick="btnMoveRight_Click" /&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;asp:Button ID="btnMoveLeft" runat="server" Text="&lt;" Width="45px" onclick="btnMoveLeft_Click" /&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;asp:ListBox ID="lstSelected" runat="server" Height="175px" Width="120px"  SelectionMode="Multiple" style="margin-left: 0px"&gt;
  
  &lt;/asp:ListBox&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td colspan="3"&gt;
  &lt;asp:Label ID="lbltxt" runat="server" ForeColor="Red"&gt;&lt;/asp:Label&gt;
      &lt;asp:Button ID="Button1" runat="server" Text="Submit"
          onclick="btnSubmit_Click" /&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
      &lt;asp:GridView ID="GridView1" runat="server" style="margin-left: 0px"&gt;
      &lt;/asp:GridView&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Code Behind</strong></p>
  
  <pre><code>Imports System.Collections.Generic
  Imports System.Web
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Collections
  Imports System.Data.SqlClient
  Public Class WebForm1
      Inherits System.Web.UI.Page
      Private arraylist1 As New ArrayList()
      Private arraylist2 As New ArrayList()
  
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
      End Sub
      Protected Sub btnMoveRight_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMoveRight.Click
          lbltxt.Visible = False
          If lstid.SelectedIndex &gt;= 0 Then
              For i As Integer = 0 To lstid.Items.Count - 1
                  If lstid.Items(i).Selected Then
                      If Not arraylist1.Contains(lstid.Items(i)) Then
                          arraylist1.Add(lstid.Items(i))
                      End If
                  End If
              Next
              For i As Integer = 0 To arraylist1.Count - 1
                  If Not lstSelected.Items.Contains(DirectCast(arraylist1(i), ListItem)) Then
                      lstSelected.Items.Add(DirectCast(arraylist1(i), ListItem))
                  End If
                  lstid.Items.Remove(DirectCast(arraylist1(i), ListItem))
              Next
              lstSelected.SelectedIndex = -1
          Else
              lbltxt.Visible = True
              lbltxt.Text = "Please select at least one in lstid to move"
          End If
      End Sub
  
      Protected Sub btnMoveLeft_Click(ByVal sender As Object, ByVal e As EventArgs)
          lbltxt.Visible = False
          If lstSelected.SelectedIndex &gt;= 0 Then
              For i As Integer = 0 To lstSelected.Items.Count - 1
                  If lstSelected.Items(i).Selected Then
                      If Not arraylist2.Contains(lstSelected.Items(i)) Then
                          arraylist2.Add(lstSelected.Items(i))
                      End If
                  End If
              Next
              For i As Integer = 0 To arraylist2.Count - 1
                  If Not lstid.Items.Contains(DirectCast(arraylist2(i), ListItem)) Then
                      lstid.Items.Add(DirectCast(arraylist2(i), ListItem))
                  End If
                  lstSelected.Items.Remove(DirectCast(arraylist2(i), ListItem))
              Next
              lstid.SelectedIndex = -1
          Else
              lbltxt.Visible = True
              lbltxt.Text = "Please select at least one in lstSelected to move"
          End If
      End Sub
      Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
          Dim strConnString As String = ConfigurationManager.ConnectionStrings("CLMSConnectionString").ConnectionString
          Dim con As New SqlConnection(strConnString)
          Dim cmd As New SqlCommand()
          Dim strID As String = ""
          For Each li As ListItem In lstSelected.Items()
              If li.Selected Then
                  strID = (strID + ("," + li.Value))
              End If
          Next
          strID = strID.Substring(1)
  
          cmd.CommandType = CommandType.StoredProcedure
          cmd.CommandText = "dbo.ConsoleClosingListSearch"
          cmd.Parameters.Add("@ConsoleClosingID", SqlDbType.Int).Value = strID
          cmd.Connection = con
          Try
              con.Open()
              GridView1.EmptyDataText = "No Records Found"
              GridView1.DataSource = cmd.ExecuteReader()
              GridView1.DataBind()
          Catch ex As Exception
              Throw ex
          Finally
              con.Close()
              con.Dispose()
          End Try
      End Sub
  End Class
  </code></pre>
  
  <p><strong>Stored Procedure</strong></p>
  
  <pre><code>SELECT *
  FROM PaymentTransaction_ConsoleClosing
  WHERE
      PaymentTransaction_ConsoleClosing.PaymentConsoleClosingID IN (@ConsoleClosingID
  </code></pre>