Multiple variables in a commandargument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vertigo262
    New Member
    • Jul 2007
    • 62

    Multiple variables in a commandargument

    I'm trying to figure out this little piece of code so that I can send more then 1 value through the commandargument .

    Converted this code from C but having a problem. probably something really stupid.

    The Error I am getting is

    Index was outside the bounds of the array.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IndexOut OfRangeExceptio n: Index was outside the bounds of the array.

    Source Error:

    Line 138:
    Line 139: Dim String1 = arg(0)
    Line 140: Dim String2 = arg(1)
    Line 141:


    I first thought it might be a null or empty value, but I don't think that's the case


    Here is the code

    Code:
     
    
    <asp:LinkButton ID="LinkButtonWebsiteURL" runat="server" Text='<%# Container.DataItem("CompanyWebsiteName")%>' CommandName="WebsiteClick" OnCommand="Btn_WebClick" CommandArgument='<%#Eval("UserAccountsID") + ";" +Eval("CompanyWebsiteURL")%>'/>
    
    
    
    
    
    
    Protected Sub Btn_WebClick(ByVal sender As Object, ByVal e As CommandEventArgs)
    
    
            Dim commandArgs As LinkButton = CType(sender, LinkButton)
            Dim info As String = commandArgs.CommandArgument
            Dim arg() As String = New String(2) {}
            Dim splitter() As Char = {";"c}
            arg = info.Split(splitter)
    
            Dim String1 = arg(0)
            Dim String2 = arg(1)
    
            Response.Write("Worked: " & String1 & " " & String2)
    
     End
  • vertigo262
    New Member
    • Jul 2007
    • 62

    #2
    Fixed!

    working code

    Code:
    <asp:LinkButton ID="LinkButtonWebsiteURL" runat="server" Text='<%# Container.DataItem("CompanyWebsiteName")%>' CommandName="WebsiteClick" OnCommand="Btn_WebClick" CommandArgument='<%# Container.DataItem("UserAccountsID") + ";" + Container.DataItem("CompanyWebsiteURL") %>'/>
    
    
    
     Protected Sub Btn_WebClick(ByVal sender As Object, ByVal e As CommandEventArgs)
    
    
            Dim commandArgs As LinkButton = CType(sender, LinkButton)
            Dim info As String = commandArgs.CommandArgument
            Dim arg(2) As String
            Dim splitter() As Char = {";"c}
            arg = info.Split(splitter)
    
    
            Dim String1 = arg(0)
            Dim String2 = arg(1)
    
     Response.Write("Worked: " & String1 & " " & String2)
    
    End

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Just a suggestion, but you should probably check to see if the array has two items in it before accessing the items...

      For example:
      Code:
      Protected Sub Btn_WebClick(ByVal sender As Object, ByVal e As CommandEventArgs)
       
       
              Dim commandArgs As LinkButton = CType(sender, LinkButton)
              Dim info As String = commandArgs.CommandArgument
              Dim arg() As String = info.Split(";"c)
              
              If arg IsNot Nothing AndAlso arg.Length > 1
                Dim String1 = arg(0)
                Dim String2 = arg(1)
                ErrorLabel.Text = "Worked: " & String1 & " " & String2
              Else
                 ErrorLabel.Text = "Didn't work, the array was not the correct length."
              End If
      End Sub
      Also, I do not recommend using Response.Write in your VB.NET code because it tends to place the string that you are writing into an invalid place in your HTML. I recommend using a Label, Literal, or a Localizable control to display the text instead of using Response.Write.

      -Frinny

      Comment

      • vertigo262
        New Member
        • Jul 2007
        • 62

        #4
        Thanks frinny

        some good advice. will try it out

        Comment

        Working...