locked
Index was outside the bounds of the array. RRS feed

  • Question

  • User669521406 posted

    Imports System.Data.SqlClient
    Imports System.Data

    Partial Class Default2
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
    Dim query As String = "SELECT DISTINCT Item.Name FROM Auction INNER JOIN Item ON Auction.ItemID = Item.ItemID WHERE(Auction.Status = 'Valid')"
    Dim dt As DataTable = GetData(query)
    DropDownList1.DataSource = dt
    DropDownList1.DataTextField = "Name"
    DropDownList1.DataValueField = "Name"
    DropDownList1.DataBind()

    DropDownList2.DataSource = dt
    DropDownList2.DataTextField = "Name"
    DropDownList2.DataValueField = "Name"
    DropDownList2.DataBind()
    DropDownList2.Items(1).Selected = True
    End If
    End Sub


    Private Shared Function GetData(query As String) As DataTable
    Dim dt As New DataTable()
    Dim constr As String = ConfigurationManager.ConnectionStrings("VirgoPlaza").ConnectionString
    Using con As New SqlConnection(constr)
    Using cmd As New SqlCommand(query)
    Using sda As New SqlDataAdapter()
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    sda.SelectCommand = cmd
    sda.Fill(dt)
    End Using
    End Using
    Return dt
    End Using
    End Function

    Protected Sub Compare(sender As Object, e As EventArgs)
    Dim query As String = String.Format("SELECT DISTINCT DATENAME(MM, BID.Date) AS Month, COUNT(BID.BidID) AS TotalBids FROM BID INNER JOIN Auction ON BID.AuctionID = Auction.AuctionID INNER JOIN Item ON Auction.ItemID = Item.ItemID WHERE (Auction.Status = 'Valid') and Item.Name ='{0}' GROUP BY DATENAME(mm, BID.Date)", DropDownList1.SelectedItem.Value)
    Dim dt As DataTable = GetData(query)

    Dim x As String() = New String(dt.Rows.Count - 1) {}
    Dim y As Decimal() = New Decimal(dt.Rows.Count - 1) {}
    For i As Integer = 0 To dt.Rows.Count - 1
    x(i) = dt.Rows(i)(0).ToString()
    y(i) = Convert.ToInt32(dt.Rows(i)(1))
    Next
    LineChart1.Series.Add(New AjaxControlToolkit.LineChartSeries() With { _
    .Name = DropDownList1.SelectedItem.Value, _
    .Data = y _
    })

    query = String.Format("SELECT DISTINCT DATENAME(MM, BID.Date) AS Month, COUNT(BID.BidID) AS TotalBids FROM BID INNER JOIN Auction ON BID.AuctionID = Auction.AuctionID INNER JOIN Item ON Auction.ItemID = Item.ItemID WHERE (Auction.Status = 'Valid') and Item.Name ='{0}' GROUP BY DATENAME(mm, BID.Date)", DropDownList2.SelectedItem.Value)
    dt = GetData(query)

    y = New Decimal(dt.Rows.Count - 1) {}
    For i As Integer = 0 To dt.Rows.Count - 1
    x(i) = dt.Rows(i)(0).ToString()
    y(i) = Convert.ToInt32(dt.Rows(i)(1))
    Next
    LineChart1.Series.Add(New AjaxControlToolkit.LineChartSeries() With { _
    .Name = DropDownList1.SelectedItem.Value, _
    .Data = y _
    })
    LineChart1.CategoriesAxis = String.Join(",", x)

    LineChart1.ChartTitle = String.Format("{0} and {1} Order Distribution", DropDownList1.SelectedItem.Value, DropDownList2.SelectedItem.Value)
    LineChart1.Visible = True
    End Sub
    End Class

    can please help 

    telling this error

    Saturday, January 25, 2014 6:05 AM

Answers

  • User281315223 posted

    Have you tried calling the DataBind() method for your Chart to properly bind your values after adding them to the Series?

    'Consider using a simple List to iterate through your values'
    Dim x = New List(Of String)()
    Dim y = New List(Of Integer)()
    
    'Iterate through each row'
    For i As Integer = 0 to dt.Rows.Count
        'Your need to ensure here that a value exists in both the 1st (0) and 2nd (1) columns'
        x.Add(dt.Rows(i)(0).ToString())
        y.Add(Convert.ToInt32(dt.Rows(i)(1)))
    Next
    
    'Bind your values'
    LineChart1.Series.Add(New AjaxControlToolkit.LineChartSeries() With { .Name = DropDownList1.SelectedItem.Value, .Data = y })
    
    'Actually Bind your Data'
    LineChart1.DataBind()

    The issue that is occuring here is that you are referencing an index that may or may not exist and it is likely occurring on the following lines :

    x.Add(dt.Rows(i)(0).ToString())
    y.Add(Convert.ToInt32(dt.Rows(i)(1)))

    I would highly consider placing a breakpoint in your code and checking to see what the values of your indicies are when this error occurs and ensuring that the values you are trying to access exist.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 25, 2014 9:37 AM

All replies

  • User-1408041064 posted

    Add following bold line: ( i.e. You have not recreated x array based on number of rows in table for a second query)

    .....
    Protected Sub Compare(sender As Object, e As EventArgs)
     Dim query As String = String.Format("SELECT DISTINCT DATENAME(MM, BID.Date) AS Month, COUNT(BID.BidID) AS TotalBids FROM BID INNER JOIN Auction ON BID.AuctionID = Auction.AuctionID INNER JOIN Item ON Auction.ItemID = Item.ItemID WHERE (Auction.Status = 'Valid') and Item.Name ='{0}' GROUP BY DATENAME(mm, BID.Date)", DropDownList1.SelectedItem.Value)
     Dim dt As DataTable = GetData(query)
    
    Dim x As String() = New String(dt.Rows.Count - 1) {}
     Dim y As Decimal() = New Decimal(dt.Rows.Count - 1) {}
     For i As Integer = 0 To dt.Rows.Count - 1
     x(i) = dt.Rows(i)(0).ToString()
     y(i) = Convert.ToInt32(dt.Rows(i)(1))
     Next
     LineChart1.Series.Add(New AjaxControlToolkit.LineChartSeries() With { _
     .Name = DropDownList1.SelectedItem.Value, _
     .Data = y _
     })
    
    query = String.Format("SELECT DISTINCT DATENAME(MM, BID.Date) AS Month, COUNT(BID.BidID) AS TotalBids FROM BID INNER JOIN Auction ON BID.AuctionID = Auction.AuctionID INNER JOIN Item ON Auction.ItemID = Item.ItemID WHERE (Auction.Status = 'Valid') and Item.Name ='{0}' GROUP BY DATENAME(mm, BID.Date)", DropDownList2.SelectedItem.Value)
     dt = GetData(query)
    
    y = New Decimal(dt.Rows.Count - 1) {}
    ''Add below line of code.
    x = New String(dt.Rows.Count - 1) {} For i As Integer = 0 To dt.Rows.Count - 1 x(i) = dt.Rows(i)(0).ToString() y(i) = Convert.ToInt32(dt.Rows(i)(1)) Next LineChart1.Series.Add(New AjaxControlToolkit.LineChartSeries() With { _ .Name = DropDownList1.SelectedItem.Value, _ .Data = y _ }) LineChart1.CategoriesAxis = String.Join(",", x) LineChart1.ChartTitle = String.Format("{0} and {1} Order Distribution", DropDownList1.SelectedItem.Value, DropDownList2.SelectedItem.Value) LineChart1.Visible = True End Sub

    Regards
     

    Saturday, January 25, 2014 8:26 AM
  • User669521406 posted

    but nothing appear on the page :(

    Saturday, January 25, 2014 9:01 AM
  • User281315223 posted

    Have you tried calling the DataBind() method for your Chart to properly bind your values after adding them to the Series?

    'Consider using a simple List to iterate through your values'
    Dim x = New List(Of String)()
    Dim y = New List(Of Integer)()
    
    'Iterate through each row'
    For i As Integer = 0 to dt.Rows.Count
        'Your need to ensure here that a value exists in both the 1st (0) and 2nd (1) columns'
        x.Add(dt.Rows(i)(0).ToString())
        y.Add(Convert.ToInt32(dt.Rows(i)(1)))
    Next
    
    'Bind your values'
    LineChart1.Series.Add(New AjaxControlToolkit.LineChartSeries() With { .Name = DropDownList1.SelectedItem.Value, .Data = y })
    
    'Actually Bind your Data'
    LineChart1.DataBind()

    The issue that is occuring here is that you are referencing an index that may or may not exist and it is likely occurring on the following lines :

    x.Add(dt.Rows(i)(0).ToString())
    y.Add(Convert.ToInt32(dt.Rows(i)(1)))

    I would highly consider placing a breakpoint in your code and checking to see what the values of your indicies are when this error occurs and ensuring that the values you are trying to access exist.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 25, 2014 9:37 AM