Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to learn more about inheritance and was experimenting and can't get this to work.

 

I keep getting the error:

Specified argument was out of the range of valid values. Parameter name: '0' is not a valid value for 'index'.

 

What I was experimenting with:

I want to take a combo box and fill it with information from my database. As a simple test I tried to do it with States.

 

I put this into a form and it works fine:

 

       Dim strStoredProcedure As String = "GetStateCombo"
       Dim cn As New SqlClient.SqlConnection

       With cn
           .ConnectionString = "Server=<SERVERNAME>;Trusted_Connection=false;database=<DBNAME>;user id=<UID>;password=<PASSWORD>"
       End With

       Dim cmd As SqlClient.SqlCommand

       Dim da As New SqlClient.SqlDataAdapter
       Dim ds As New DataSet

       cmd = New SqlClient.SqlCommand
       With cmd
           .CommandType = CommandType.StoredProcedure
           .CommandText = strStoredProcedure
           .Connection = cn
       End With
       da.SelectCommand = cmd

       Try
           da.Fill(ds)
           If Not IsNothing(ds) Then
               ds.Tables(0).TableName = "States"
               Dim dr As DataRow
               Me.ComboBox1.DataSource = ds.Tables("States")
               Me.ComboBox1.DisplayMember = "cboState"
               Me.ComboBox1.ValueMember = "cboStateCode"
           End If

       Catch ex As Exception
           Throw New Exception(ex.Message, ex)
       End Try       

 

As I said, I'm trying to learn about inheritance. So I decided to design a combo box that automatically inherits from the normal combo box and fills in the states so I can use this in any future project. I call it StateComboBox.

 

So I tried doing this in a class library

Public Class StateComboBox
'Here I'm inheriting from the original combo box
   Inherits System.Windows.Forms.ComboBox
   Private components As System.ComponentModel.IContainer
   Protected Overrides Sub Refre****em(ByVal index As Integer)

   End Sub

   Protected Overrides Sub SetItemsCore(ByVal items As System.Collections.IList)

   End Sub
   Public Sub New()
       InitializeComponent()
       FillComboBox()
   End Sub
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If (disposing) Then
           If IsNothing(components) Then
               components.Dispose()
           End If
           MyBase.Dispose(disposing)
       End If
   End Sub



   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.components = New System.ComponentModel.Container
   End Sub
   Private Sub FillComboBox()
       Dim strStoredProcedure As String = "GetStateCombo"
       Dim cn As New SqlClient.SqlConnection("Server=<SERVERNAME>;Trusted_Connection=false;database=<DBNAME>;user id=<UID>;password=<PASSWORD>")
       Dim cmd As New SqlClient.SqlCommand(strStoredProcedure, cn)

       Dim da As New SqlClient.SqlDataAdapter
       Dim ds As New DataSet

       With cmd
           .CommandType = CommandType.StoredProcedure
       End With
       da.SelectCommand = cmd

       Try
           da.Fill(ds)
           If Not IsNothing(ds) Then
               ds.Tables(0).TableName = "States"
               'THE LINE BELOW THIS COMMENT SEEMS TO BE GENERATING THE ERROR AND THE ERROR APPEARS 2X in a ROW
               Me.DataSource = ds.Tables("States")
               Me.DisplayMember = "cboState"
               Me.ValueMember = "cboStateCode"
           End If
       Catch ex As Exception
           Throw New Exception(ex.Message, ex)
       End Try

   End Sub
End Class

 

Any help would be appreciated!

:confused:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...