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

时间:2014-08-03 07:36:51

标签: sql asp.net sql-server vb.net

有两个列表框。 [lstid和lstselected]。当用户在[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>

代码背后

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 >= 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 >= 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

存储过程

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

1 个答案:

答案 0 :(得分:0)

根据您的评论判断,您的即时错误 - startIndex cannot be larger than length of string是因为您尝试对字符串进行子字符串处理,只要您预期它就不会。

你说它出现在这一行:

strID = strID.Substring(1)

这样做会返回一个从第一个索引(第二个字符)一直到字符串结尾的字符串。如果strID长度不超过两个字符,则会收到错误。

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