1 Replies - 4270 Views - Last Post: 19 November 2009 - 12:22 AM Rate Topic: -----

#1 greenbluekidz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

How to sum a record in ListView control to a textbox and how to sort L

Posted 18 November 2009 - 06:14 AM

1. I am trying to sum up a record in ListView1 called NumberInStock and have that value added to a text box called txtInventoryTotal. I have been playing with code to do this and have come with something that is placing a result in my text box. The problem is that it is the wrong answer.

On my first attempt, I went through and created a new function called InventoryTotals() that calculates the sum of the NumberInStock but couldn't get it to display in the text box.

2. In ListView2, I want to show a location and then have the NumberInStock show a subtotal for what is available per location. How do I go about doing this? Do I have to create a connection to the database every time I want to accomplish this? Right now, doing that causes errors in Vs 2005. If i have more than one connection trying to access my database, it tells me that it cannot establish a connection to the database.

I have added a screenshot of my application in hopes that it gives you an idea of what it is I am trying to do.

Help??


Option Strict On
Imports System.Data.OleDb

Public Class Form1
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		' Adding ListView Columns


		ListView1.Columns.Add("ProductID", 70, HorizontalAlignment.Left)
		ListView1.Columns.Add("ProductName", 80, HorizontalAlignment.Left)
		ListView1.Columns.Add("NumberInStock", 95, HorizontalAlignment.Left)
		ListView1.Columns.Add("Location", 70, HorizontalAlignment.Left)
		ListView1.Columns.Add("DateOfLastReorder", 110, HorizontalAlignment.Left)
		ListView2.Columns.Add("NumberInStock", 95, HorizontalAlignment.Left)
		ListView2.Columns.Add("Location", 70, HorizontalAlignment.Left)
		AddInListView()




	End Sub
	Private Const gstrConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
													"Data Source=C:\Chapter10\Inventory.mdb"
	Private Sub AddInListView()
		Dim objConnection As OleDbConnection
		Dim objDataReader As OleDbDataReader
		Dim objCommand As OleDbCommand

		Try
			objConnection = New OleDbConnection(gstrConnectionString)
			objCommand = New OleDbCommand()
			objCommand.Connection = objConnection
			objCommand.Connection.Open()
			objCommand.CommandText = "SELECT * FROM Inventory ORDER BY Location"
			objDataReader = objCommand.ExecuteReader
			Do While (objDataReader.Read())
				ListView1.Items.Add(objDataReader("ProductId").ToString)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(objDataReader("ProductName").ToString)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(objDataReader("NumberInStock").ToString)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(objDataReader("Location").ToString)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(objDataReader("DateOfLastReorder").ToString)
				ListView2.Items.Add(objDataReader("NumberInStock").ToString)
				ListView2.Items(ListView1.Items.Count - 1).SubItems.Add(objDataReader("Location").ToString)

				Dim intIndex As Integer
				Dim db1subtotal As Double
				Dim db1price As Double
				Dim strprice As String
				Dim strNPrice As String


				For intIndex = 0 To ListView1.Items.Count - 1
					strprice = ListView1.Items(intIndex).SubItems(2).Text

					db1price = Double.Parse(strprice)

					db1subtotal += db1price
					txtInventoryTotal.Text = CStr(db1subtotal)
				Next intIndex
			Loop
			objDataReader.Close()
			Return
		Catch objException As Exception
			MessageBox.Show("A database error occurred.", "Database error")
			End
		End Try
	End Sub

	Private Function InventoryTotal() As String
		Dim objConnection As OleDbConnection
		Dim objDataReader As OleDbDataReader
		Dim objCommand As OleDbCommand
		Dim strOutput As String

		Try
			objConnection = New OleDbConnection(gstrConnectionString)
			objCommand = New OleDbCommand()
			objCommand.Connection = objConnection
			objCommand.Connection.Open()
			objCommand.CommandText = "SELECT SUM(NumberInStock) FROM Inventory"
			objDataReader = objCommand.ExecuteReader
			Do While (objDataReader.Read())
				strOutput &= Convert.ToString(objDataReader.Item("NumberInStock")) & ControlChars.Tab & ControlChars.Tab


			Loop
			objDataReader.Close()
			Return strOutput
		Catch objException As Exception
			MessageBox.Show("A database error occurred.", "Database error")
			End
		End Try
	End Function


Attached image(s)

  • Attached Image

This post has been edited by greenbluekidz: 18 November 2009 - 01:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to sum a record in ListView control to a textbox and how to sort L

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to sum a record in ListView control to a textbox and how to sort L

Posted 19 November 2009 - 12:22 AM

Hi,

Ok just to start off with. About your connectins. You need to close them.

   objConnection.Close()



That should allow you to make a new conneciton....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1