索引(从零开始)必须大于或等于零且小于参数的大小

时间:2011-11-01 17:17:20

标签: vb.net exception string.format

点击转发器中的任何字母按钮时出现此错误。 sql语句我在sql server中工作,但在我的页面上它只是出错了。唯一不同的是,在sql server中,我将'@CompanyID'更改为实际ID,将'{0}'更改为'a',以便查询将运行。

我在网上找到的有关此问题的所有帖子都说明了这一点 - > {0},但我没有看到任何地方我可能尝试传递多个参数或者说错了。

<asp:repeater id="rptLetters" runat="server" datasourceid="dsLetters">
<headertemplate>
|
</headertemplate>
<itemtemplate>
<asp:linkbutton id="btnLetter" runat="server" onclick="btnLetter_Click" 
 text='<%#Eval("Letter")%>' />
</itemtemplate>

<separatortemplate>
|
</separatortemplate>
</asp:repeater>

Protected Sub btnLetter_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim btnLetter As LinkButton = TryCast(sender, LinkButton)
    If btnLetter Is Nothing Then
        Return
    End If
    'SELECT statement to filter Gridview
    dsCategoryLookup.SelectCommand = [String].Format("SELECT 
         Category.CategoryID, Category.CategoryName, Product.ProductID, 
         Product.ProductName, Company.CompanyID
         FROM Category 
         LEFT JOIN CategoryLink 
         ON Category.CategoryID = CategoryLink.CategoryID 
         LEFT JOIN Product ON Product.ProductID = CategoryLink.ProductID
         LEFT JOIN CompanyLink ON CompanyLink.ProductID = Product.ProductID
         LEFT JOIN Company ON Company.CompanyID = CompanyLink.CompanyID 
         WHERE (Category.CategoryName LIKE '{0}%') 
         AND Company.CompanyID = @CompanyID), btnLetter.Text")

    'declare @CompanyID
    dsCategoryLookup.SelectParameters.Clear()
    Dim controlParam As ControlParameter = New ControlParameter
    controlParam.ControlID = "ddlCompany"
    controlParam.DefaultValue = "-1"
    controlParam.Name = "CompanyID"
    controlParam.PropertyName = "SelectedValue"
    controlParam.Type = TypeCode.Decimal

    dsCategoryLookup.SelectParameters.Add(controlParam)
End Sub

1 个答案:

答案 0 :(得分:0)

只是你的引号在错误的地方吗? btnLetter.Text位于代码中的格式字符串中:

dsCategoryLookup.SelectCommand = [String].Format("SELECT 
     Category.CategoryID, Category.CategoryName, Product.ProductID, 
     Product.ProductName, Company.CompanyID
     FROM Category 
     LEFT JOIN CategoryLink 
     ON Category.CategoryID = CategoryLink.CategoryID 
     LEFT JOIN Product ON Product.ProductID = CategoryLink.ProductID
     LEFT JOIN CompanyLink ON CompanyLink.ProductID = Product.ProductID
     LEFT JOIN Company ON Company.CompanyID = CompanyLink.CompanyID 
     WHERE (Category.CategoryName LIKE '{0}%') 
     AND Company.CompanyID = @CompanyID)", btnLetter.Text)