Solved

InvalidArguement=Value of '0' is not valid for 'index'.Parameter name index

Posted on 2014-07-31
8
185 Views
Last Modified: 2014-08-05
I decided to put all classes in the same project. so I got rid of errors but now when I click a button to load a listview control. I get
after button click I get this message box this here is code
DB Connection
Imports System.Data.SqlClient
Public Class DB
    Public Shared Function GetConnection() As SqlConnection
        Dim connectionString As String =
            "Persist Security Info=False;User ID=sa;Password=50Cent;Initial Catalog=TSN2;Server=tsnappdev01"
        Return New SqlConnection(connectionString)
    End Function
End Class

Open in new window


here is form code

Public Class Form1
    Public Sub FormatLVStatus()
        'lvHotfixes.Location = New System.Drawing.Point(13, 54)
        lvPlayers.Margin = New System.Windows.Forms.Padding(4)
        lvPlayers.MultiSelect = False
        lvPlayers.Name = "LVCONNECT1"

        'LVCONNECT.TabIndex = 7
        lvPlayers.UseCompatibleStateImageBehavior = True
        lvPlayers.View = System.Windows.Forms.View.Details
        lvPlayers.Columns.Add("", 25, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("PlayerID", 100, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Firstname", 150, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Lastname", 150, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Position", 150, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Height", 150, HorizontalAlignment.Left)
        lvPlayers.Columns.Add("Weight", 150, HorizontalAlignment.Left)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        FormatLVStatus()
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Dim playersList As List(Of Players)
        Try
            playersList = PlayersSQL.GetPlayers
            If playersList.Count > 0 Then
                Dim players As Players
                For i As Integer = 0 To playersList.Count - 1
                    players = playersList(i)
                    '  lvPlayers.Items.Add(players.playersNumber)
                    lvPlayers.Items(i).SubItems.Add(
                        CInt(players.PlayerID).ToString)
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Firstname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Lastname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Position))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Height))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Weight))
                Next
            Else
                MessageBox.Show("All playerss are paid in full.",
                    "No Balance Due")
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
            Me.Close()
        End Try
    End Sub
End Class

Open in new window


PlayersSQL

Imports System.Data.SqlClient
Public Class PlayersSQL
    Public Shared Function GetPlayers() As List(Of Players)
        Dim playersList As New List(Of Players)
        Dim connection As SqlConnection = DB.GetConnection
        Dim selectStatement As String =
            "SELECT PlayerID, Firstname, Lastname, " &
             "Position, Weight, Height " &
             "FROM NFLPlayers " &
             "ORDER BY Lastname"
        Dim selectCommand As New SqlCommand(selectStatement, connection)
        Try
            connection.Open()
            Dim reader As SqlDataReader =
                selectCommand.ExecuteReader()
            Dim players As Players
            Do While reader.Read
                players = New Players
                players.PlayerID = CInt(reader("playerID"))
                players.Firstname = CStr(reader("firstname"))
                players.Lastname = CStr(reader("lastname"))
                players.Position = CStr(reader("position"))
                players.Height = CStr(reader("height"))
                players.Weight = CStr(reader("weight"))
                playersList.Add(players)
            Loop
            reader.Close()
        Catch ex As SqlException
            Throw ex
        Finally
            connection.Close()
        End Try
        Return playersList
    End Function
End Class

Open in new window


FINALLY Players Class

Public Class Players
    Private m_PlayerID As Integer
    Private m_Firstname As String
    Private m_Lastname As String
    Private m_Position As String
    Private m_Height As String
    Private m_Weight As String

    Public Sub New()

    End Sub

    Public Property PlayerID() As Integer
        Get
            Return m_PlayerID
        End Get
        Set(ByVal value As Integer)
            m_PlayerID = value
        End Set
    End Property

    Public Property Firstname() As String
        Get
            Return m_Firstname
        End Get
        Set(ByVal value As String)
            m_Firstname = value
        End Set
    End Property

    Public Property Lastname() As String
        Get
            Return m_Lastname
        End Get
        Set(ByVal value As String)
            m_Lastname = value
        End Set
    End Property

    Public Property Position() As String
        Get
            Return m_Position
        End Get
        Set(ByVal value As String)
            m_Position = value
        End Set
    End Property

    Public Property Height() As String
        Get
            Return m_Height
        End Get
        Set(ByVal value As String)
            m_Height = value
        End Set
    End Property

    Public Property Weight() As String
        Get
            Return m_Weight
        End Get
        Set(ByVal value As String)
            m_Weight = value
        End Set
    End Property
End Class

Open in new window


Any suggestions pleas let me know or fixes thanks EE Never really had a problem doing this before but that was a few years ago. help EE Please
0
Comment
Question by:powerztom
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
Comment Utility
It looks like you commented out the code to add items to lvPlayers, and only left in code to add subitems to the item you didn't add?
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
Please, specify which line causes a problem when you ask a question. This makes our job a lot of helping you a lot easier.

I suspect that it is line 33 of the second piece of code. The line that adds a new item to the ListView is commented out, so there is no item defined when you try to add subitems.
0
 

Author Comment

by:powerztom
Comment Utility
When you add a new project to your current Project in my case I add a Class Library. Now whatever I name that Class Library I want to reference that new project in current Project Like for example I create a form then I add new project called NFLRoster which is a class library then within I create my Classes for the solution now in the first project that has a Form I add IMPORTS NFLRoster in beginning however I get that error line under the IMPORTS NFLRoster well the NFLRoster word. Now this stops me from using Classes I created in The class library called NFLRoster. Is there a step I missed out on. I think this is my problem. Please help EE
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
Have you tried removing the comment on line 33 as suggested?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:powerztom
Comment Utility
James AI remmed that line out cause it had a error line after it so I unremmed it and error line came back
this is the line lvPlayers.Items.Add(players.playersNumber) But any suggestions or do you understand what I am asking in my lastest post?
0
 

Author Comment

by:powerztom
Comment Utility
I just chaged lvPlayers.Items.Add(players.playersNumber) to lvPlayers.Items.Add(players.Index) and got the data I needed except I got a few records with PlayerID AND NOTHING MORE for Firstname , Lastname ect. James do you know of a way to avoid showing these records. A Null detector ?

Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Dim playersList As List(Of Players)
        Try
            playersList = PlayersSQL.GetPlayers
            If playersList.Count > 0 Then
                Dim players As Players
                For i As Integer = 0 To playersList.Count - 1
                    players = playersList(i)
                    lvPlayers.Items.Add(players.Index)
                    lvPlayers.Items(i).SubItems.Add(
                        CInt(players.PlayerID).ToString)
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Firstname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Lastname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Position))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Height))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Weight))
                Next
            Else
                MessageBox.Show("All playerss are paid in full.",
                    "No Balance Due")
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
            Me.Close()
        End Try
    End Sub

Open in new window

0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
Well, the ideal would be to filter them at the source, when you query your SQL database. But if for some reason you cannot do that, here is how you can do it in your code:

                    players = playersList(i)
If Cstr(players.FirstName).Length>0 AndAlso Cstr(players.LastName).Length>0 Then
                    lvPlayers.Items.Add(players.Index)
                    lvPlayers.Items(i).SubItems.Add(
                        CInt(players.PlayerID).ToString)
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Firstname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Lastname))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Position))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Height))
                    lvPlayers.Items(i).SubItems.Add(
                        CStr(players.Weight))
End If

Open in new window

0
 

Author Closing Comment

by:powerztom
Comment Utility
Thanks Bro
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now