Index (zero based) must be greater than or equal to zero and less than the size of the argument

StackOverflow https://stackoverflow.com/questions/7970209

  •  18-02-2021
  •  | 
  •  

質問

I get this error when clicking on any of the letter buttons in my repeater. The sql statement I have works in sql server, but on my page it just errors out. The only thing that is different is that in sql server I change '@CompanyID' to an actual ID and '{0}' to 'a' so the query will run.

All the posts I have found online with this problem say something about this-->{0}, but I don't see anywhere that I might be trying to pass more than one argument or have this wrong.

<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
役に立ちましたか?

解決

Is it just you have your quotation marks in the wrong place? The btnLetter.Text is inside your format string in your code:

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)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top