Sql 将列表框中的多个选定项传递给存储过程

Sql 将列表框中的多个选定项传递给存储过程,sql,asp.net,sql-server,vb.net,Sql,Asp.net,Sql Server,Vb.net,有两个列表框。[选择lstid和lstid]。当用户在[lstid]中选择一个或多个ID并单击向右移动时。所选项目将位于[lstselected]中 我试图将多个值从[lstselected]传递到存储过程,并将数据绑定到[gridview],但收到以下错误 [startIndex不能大于字符串的长度。] 感谢任何人能在这方面提供帮助 ASP <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="reimbursementlb

有两个列表框。[选择lstid和lstid]。当用户在[lstid]中选择一个或多个ID并单击向右移动时。所选项目将位于[lstselected]中

我试图将多个值从[lstselected]传递到存储过程,并将数据绑定到[gridview],但收到以下错误

[startIndex不能大于字符串的长度。]

感谢任何人能在这方面提供帮助

ASP

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="reimbursementlb.aspx.vb" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Reimbursenment Report</title>
    <style type="text/css">
        .auto-style1 {
            width: 153px;
        }
    </style>
</head>
<body>
<form id="form2" runat="server">
<div>
 <table align=center>
<tr>
<td align="center" class="auto-style1" >
    <h4>Reimbursement ID </h4>
</td>
<td>
    <h4></h4>
    </td>
<td>
    <h4>
<b>Selected ID</b></h4>
    </td>
</tr>
<tr>
<td class="auto-style1">
<asp:ListBox ID="lstid" runat="server"  Height="175px" Width="131px" SelectionMode="Multiple" DataSourceID="SqlDataSource1" DataTextField="ID" DataValueField="ID">
</asp:ListBox>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CLMSConnectionString %>" SelectCommand="SELECT [ID] FROM [PaymentConsoleClosing] ORDER BY [ID] DESC"></asp:SqlDataSource>
</td>
<td>
<table>
<tr>
<td>
<asp:Button ID="btnMoveRight" runat="server" Text=">" Width="45px" onclick="btnMoveRight_Click" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnMoveLeft" runat="server" Text="<" Width="45px" onclick="btnMoveLeft_Click" />
</td>
</tr>
</table>
</td>
<td>
<asp:ListBox ID="lstSelected" runat="server" Height="175px" Width="120px"  SelectionMode="Multiple" style="margin-left: 0px">

</asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
    <asp:Button ID="Button1" runat="server" Text="Submit"
        onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
    <asp:GridView ID="GridView1" runat="server" style="margin-left: 0px">
    </asp:GridView>
</form>
</body>
</html>
存储过程

SELECT *
FROM PaymentTransaction_ConsoleClosing
WHERE
    PaymentTransaction_ConsoleClosing.PaymentConsoleClosingID IN (@ConsoleClosingID

根据您的评论判断,您的直接错误-
startIndex不能大于字符串的长度
,因为您正在尝试对一个没有预期长度的字符串执行子字符串

你说它发生在这一行:

strID = strID.Substring(1)
这将返回一个从第一个索引(第二个字符)开始一直到字符串末尾的字符串。如果
strID
的长度不超过两个字符,则会出现错误


确保正确填充了
strID
字符串,并可能在Substring()调用之前添加一个条件,以确保字符串足够长。

取决于您使用的SQL Server版本。如果您使用的是SQL Server 2008或更新版本,请使用表值参数功能将“数据表”从VB.NET代码传递到SQL Server存储过程。例如,请参见如何执行此操作(在VB.NET中也可以正常工作)哪一行产生错误?这一行产生错误[strID=strID.Substring(1)]。我使用的是sql server 2005可能重复的
strID = strID.Substring(1)