2 Replies - 1056 Views - Last Post: 11 November 2011 - 01:27 PM Rate Topic: -----

#1 ChrisTech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-November 11

Error when adding item to a listview

Posted 10 November 2011 - 02:44 AM

am developing a point of sale program for a project subject. The trouble im having is when i press on the button to add the item from a textbox to a listview i receive an error: See attached Screenshot.

my code is:
Private Sub SalesScreen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.CustomerTextBox.Text = "Cash Customer"

        'Sets the properties of the listview
        SalesScreenListView.View = View.Details
        SalesScreenListView.GridLines = True
        SalesScreenListView.FullRowSelect = True

        'Add 4 columns and column names
        SalesScreenListView.Columns.Add("Item", 100)
        SalesScreenListView.Columns.Add("Description", 430)
        SalesScreenListView.Columns.Add("Quantity", 70)
        SalesScreenListView.Columns.Add("Price", 70)

        'Set all Textbox properties to default
        Me.SubTotalTextBox.Text = Format("0.00", "currency")
        Me.TaxTextBox.Text = Format("0.00", "currency")
        Me.TotalTextBox.Text = Format("0.00", "currency")
        Dim InvoiceNum As String
        InvoiceNum = IncrementInvoice(InvoiceNumberTextBox.Text)
        Me.InvoiceNumberTextBox.Text = InvoiceNum
    End Sub

Private Sub ProductCodeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProductCodeButton.Click
        Dim connection As New SqlClient.SqlConnection
        connection.ConnectionString = ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True")
        connection.Open()

        Dim adaptor As New SqlClient.SqlDataAdapter
        Dim dataset As New DataSet

        Try
            
            'Search for data in the SQL database using an SQL Select statement.
            Dim command As New SqlClient.SqlCommand("SELECT * FROM [Products] WHERE ProductID= '" & ProductCodeTextBox.Text & "'OR Description='" & ProductCodeTextBox.Text & "';", connection)
            Dim dr As SqlClient.SqlDataReader = command.ExecuteReader(Commandbehavior.Default)
            'Add rows to the listview for every match found in the database
            While dr.Read()
                Dim new_item As New  _
                ListViewItem(dr.Item("Item").ToString)
                new_item.SubItems.Add(dr.Item("Description").ToString)
                new_item.SubItems.Add(dr.Item("Quantity").ToString)
                new_item.SubItems.Add(dr.Item("Price").ToString)
                SalesScreenListView.Items.Add(new_item)


            End While
            dr.Close()

        Catch ex As Exception
            MsgBox(ex.ToString)  'Show error when something goes wrong
        End Try

        'Close database connection
        connection.Close()
    End Sub



Any help on what causes this error and how to correct it?

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Error when adding item to a listview

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Error when adding item to a listview

Posted 10 November 2011 - 02:58 AM

In the .Item area, I believe that you use a 0-based Integer value instead of a string.
Was This Post Helpful? 0
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Error when adding item to a listview

Posted 11 November 2011 - 01:27 PM

for your error, MSDN says

Quote

IndexOutOfRangeException
No column with the specified name was found.


Maybe is "item" and not "Item". Also you can use GetString(<columnIndex>)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1