Database with listbox (VB .NET 2003)

getting values from a database into a listbox and adding values

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 22063 Views - Last Post: 25 July 2006 - 03:25 PM Rate Topic: -----

#1 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Database with listbox (VB .NET 2003)

Posted 09 July 2006 - 05:49 AM

Hi,

i am currently working on a project where i am reading from a database to display menu items in a 'restaurant style ordering program'. I have been successful in loading the database into labels in the form to display the different menu options, and then i have managed to place individual items into the listboxes, which show the order, BUT i am having problems getting the totals of the selected orders.

i have read another post here http://forums.dreami...showtopic=16565 that had a similar problem to what i am experiencing, but i am having some difficulties adapting it to my program.

I know that i can add these prices to a total variable, and keep adding the items, but since i also would like to individually remove items from the order, i dont know if it is possible to subtract values.

I have 2 list boxes which display 1)the food item and additional information and 2)the price. I have these in separate list boxes, because i thought that i would be able to add the total price, but i cant seem to do this.

my second problem is that once i have set up the list boxes correctly, i need to save this information to a text file, which can later be displayed in another list box in another exe.

first things first though. is the way i am displaying the orders in the list boxes the most appropriate way to do it, or should i have it all in the one list box, and then, how do i find the total of the prices of the order?

I have attached my program with all the work i have done on it so far. the database location might need to be changed, so that you can view it properly, since i could not give a relative address for it, so it can be found in the bin folder.

I appreciate any help you can give me,

Luke

Attached File(s)


This post has been edited by Hiluke: 09 July 2006 - 05:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Database with listbox (VB .NET 2003)

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Database with listbox (VB .NET 2003)

Posted 09 July 2006 - 10:43 AM

Quote

I know that i can add these prices to a total variable, and keep adding the items, but since i also would like to individually remove items from the order, i dont know if it is possible to subtract values.

You are correct in that all you need is 3 class level variables to keep track of your subtotal, sales tax and totalprice. And yes you can recalculate those values as you add or subtract items from your listbox. They do need to be class level so that they retain their values through the entire run of your program.

Quote

I have 2 list boxes which display 1)the food item and additional information and 2)the price. I have these in separate list boxes, because i thought that i would be able to add the total price, but i cant seem to do this.

Better to have all the information displayed in one listbox, makes it easier for the user to see that all the information is related to whats in the listbox.
Don't worry about working with the pricing from the listbox. The listbox is just a mechanism to display information to the user. It is easier to adjust your pricing as you add or take an item from the listbox. In other words, as you add an item to the lisbox you will pull the information from your database. As soon as you retrieve the price from the database, start caculating the new pricing with your class level variables, add the order information to your listbox, and then update the total price with the new values.

Lets get your listboxes and your global variables working first and then we can deal with some other issues that I see will become a problem a little further down the road.

Let me know if you need me to clarify this a little better.
Was This Post Helpful? 0
  • +
  • -

#3 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Re: Database with listbox (VB .NET 2003)

Posted 10 July 2006 - 03:50 AM

thanks for the reply,

i can see that this way is probably the best, and i have used this method in the past of just adding items to a list box to calculate prices, BUT i have not done it from a database, but that can be done the same way as i am adding it to the current price list box.

using the class level variables was how i would have done it except for the fact that finding an items price and then removing it from the variable was impossible for me... how can i do this?

->->how to remove an item's price when i select it and click remove.

i have done all the adding of the items and put everything in one list box, except how do i remove the item's price from the subtotal?

Luke

This post has been edited by Hiluke: 10 July 2006 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Database with listbox (VB .NET 2003)

Posted 10 July 2006 - 03:40 PM

View PostHiluke, on 10 Jul, 2006 - 02:42 AM, said:

using the class level variables was how i would have done it except for the fact that finding an items price and then removing it from the variable was impossible for me... how can i do this?


How are you currently getting the prices to load into your form?

The process is exactly the same, except instead of retrieving the price of an item and storing it into a variable, you will just subtract the returned value from the subtotal variable.

Quote

i have done all the adding of the items and put everything in one list box, except how do i remove the item's price from the subtotal?


Do you mean how do you update the price stored in the listbox?

Well if it is a global variable, you would just subtract that particular items price from the subtotal and then recalculate the tax and total price. And then refresh the list box with the new values.
Was This Post Helpful? 0
  • +
  • -

#5 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Re: Database with listbox (VB .NET 2003)

Posted 10 July 2006 - 03:47 PM

ok well when i load the program, i have all of the information load from the database into the various labels in the form. When an 'Add' button is clicked, it retrieves the information from that particular item from the database and displays the 'item', 'additional' and 'price' fields in one list box.

at the same time i am adding the items to the list box, i am also reading from the database to add the item price to the global variable 'SubTotal'.

here is the code i am using. I am using the same code for every 'add' button on the form, just with a different datasource.
	Private Sub btnAdd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd1.Click
		' Add the corresponding meal name, additional item and price to the list boxes
		lstOrder.Items.Add(BindingContext(dsItems1, "Items").Current.Row("Item") + BindingContext(dsItems1, "Items").Current.Row("Additional") + (FormatCurrency(BindingContext(dsItems1, "Items").Current.Row("Price"))))
		'Calculate the value of the subtotal with the new item on the menu included
		SubTotal = SubTotal + (BindingContext(dsItems1, "Items").Current.Row("Price"))
		'Display the new subtotal
		lblSubTotal.Text = FormatCurrency(SubTotal)
	End Sub



i am not saving individual items to any variables... is this what i am meant to be doing? the only information i am saving to variables is the global variable 'SubTotal' which i am constantly updating with each new price.

This post has been edited by Hiluke: 10 July 2006 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Database with listbox (VB .NET 2003)

Posted 10 July 2006 - 04:07 PM

View PostHiluke, on 10 Jul, 2006 - 02:39 PM, said:

When an 'Add' button is clicked, it retrieves the information from that particular item from the database and displays the 'item', 'additional' and 'price' fields in one list box.

This is exactly what I was getting at, you already know how to retreive the information from the database. You are currently doing just that to load information into your listbox. The only difference is when you remove an item you only need to pull the 'price' from the database. Subtract the price from the subtotal and then recalculate the new tax and total price.

Quote

i am not saving individual items to any variables... is this what i am meant to be doing? the only information i am saving to variables is the global variable 'SubTotal' which i am constantly updating with each new price.

No you don't need to do that, you only need the global variable.

Did I misunderstand your question?

Would not be the first time if I did. LOL

Perhaps you are not sure how to pull only the price from database. Is that it?

This post has been edited by jayman9: 10 July 2006 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Re: Database with listbox (VB .NET 2003)

Posted 10 July 2006 - 04:23 PM

yeah thats exactly it.

i dont know how to find the price of the item from just the stuff i have in the list box. if i knew how to do that, i would have no problem subtracting it from the total.
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Database with listbox (VB .NET 2003)

Posted 10 July 2006 - 05:27 PM

View PostHiluke, on 10 Jul, 2006 - 03:15 PM, said:

yeah thats exactly it.

i dont know how to find the price of the item from just the stuff i have in the list box. if i knew how to do that, i would have no problem subtracting it from the total.


Ok I see now what you are asking, sorry about that.

Let me first ask you this, are you still working with 2 listboxes or did you switch to one?

In this situation 2 would be easier to work with and would simply matters quite a bit.

The reason I say that is because using data sets and the binding manager base. You currently have no methods by which to search for information. You would need to add a Data Row object to work with the data in the data sets. Then you would have a FindBy method available to search with, but it would only be able to search using the primary key. And since you currently have 9 Data Sets, that is not gonna work.

Anyhow, if you have 2 listboxes then before you delete the item from the listbox. Retreive the value from listbox by using the index value which you have already retreived. Now convert the item to a number and do the math. As you can see it is on one line inside your btnRemove_Click event. I made an assumption as to the name of your variable and the data type, is it single or double?

Either way you can see how I am converting the value just scroll the list to replace ToDouble with the correct data type of your variable.

lstPrice.Items( b ) retrieves the actual value of whatever is stored in that index of the listbox. Since you are using it in a formula you need to convert from a String to the correct data type.

	Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
		Dim a As Integer
		Dim b As Integer
		a = lstOrder.SelectedIndex
		b = lstPrice.SelectedIndex

		subTotal = subTotal - Convert.ToDouble(lstPrice.Items(b))

		lstOrder.Items.RemoveAt(a)
		lstPrice.Items.RemoveAt(b)
	End Sub





Hope that helps. :D
Was This Post Helpful? 0
  • +
  • -

#9 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Re: Database with listbox (VB .NET 2003)

Posted 10 July 2006 - 10:27 PM

thanks so much for that... it works just how i wanted it to. Instead of
subTotal = subTotal - Convert.ToDouble(lstPrice.Items(b))

though, i used the
subTotal = subTotal - CDbl(lstPrice.Items(b))

command. It didnt seem to like the other one.

i now have another couple of questions about this database and list box thing

firstly, i have all of my information from the database appearing in the various labels in the form, and i also wanted to display pictures matching the different food items, the same way i am displaying the text. How can i do this?

also, now that i have all of the order displaying in the text boxes, i want to save these to 2 separate text files. One for the order, and one for the prices, which can then be read by another list box in a different exe. looking at it as a restaurant situation, i want the orders from the tables saved to a text file which can then be read by another program in the kitchen part of the restaurant. What is the best method of saving the data to the text files?

thanks,

Luke

This post has been edited by Hiluke: 10 July 2006 - 10:48 PM

Was This Post Helpful? 0
  • +
  • -

#10 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Re: Database with listbox (VB .NET 2003)

Posted 11 July 2006 - 04:44 AM

i did some other research about writing from a list box to a text file, and this site, http://www.kdkeys.ne...thread/676.aspx, contained exactly the code that i needed. I am now able to save the data from the list box into the text files, which i intend to read in another exe.

I was intending to use a timer to constantly be trying to read from the text file to update the listbox in the 'kitchen' form. Should i use another method other than a timer, because i think that there will be some issues if the file is trying to read and write at the same time.

i still cannot work out how to read images from a database though.

any suggestions are appreciated.

thanks,

Luke
Was This Post Helpful? 0
  • +
  • -

#11 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Re: Database with listbox (VB .NET 2003)

Posted 12 July 2006 - 06:23 AM

originally i was considering using the winsock control for this step of the program, but i found that this was far too complex for me, and i felt that for my needs, using a text file like this, would be the best solution. Would you please tell me the best code i should use for updating using the timer, or should i use another method not involving a timer, when the list box will update once the text file is updated?

i.e. when the text file is written from the tables module, i want the list box to update in the kitchen module. Even if it is updating every 5 seconds, as long as it reads the data from the text file.

Thanks,

Luke
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Database with listbox (VB .NET 2003)

Posted 12 July 2006 - 08:47 AM

Are these seperate applications?

One is writing data to the text file and the other is reading from the text file?
Was This Post Helpful? 0
  • +
  • -

#13 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Re: Database with listbox (VB .NET 2003)

Posted 12 July 2006 - 03:41 PM

View Postjayman9, on 12 Jul, 2006 - 07:39 AM, said:

Are these seperate applications?

One is writing data to the text file and the other is reading from the text file?


yeah thats exactly it. the one i have been working on is the one that is writing to the text file, and there is another separate application with a list box in it, which i want to be able to read from the text file.
Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Database with listbox (VB .NET 2003)

Posted 12 July 2006 - 04:21 PM

Well then you have 2 ways that I know of, to do what you want. The first and most traditional is to use a timer.

But a new added feature to VB.NET is the FileSystemWatcher component. With this component you can monitor a directory or a directory tree and be notified of any changes to any of the files that you are monitoring.

So basically you could set the path to the folder that has your file in it. Set the filter to only look at your text file .Filter = "yourFile.txt" and then you will receive a notification event that would tell your kitchen app it's time to upload the new file.

Here is a link to MSDN describing the component. If you google it, you will find a bunch of tutorials on how to set it up.

You will find the FileSytemWatcher on the Components tab of the Toolbox.
Was This Post Helpful? 0
  • +
  • -

#15 Hiluke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-June 06

Re: Database with listbox (VB .NET 2003)

Posted 13 July 2006 - 12:59 AM

jayman9, you are a legend... that thing works perfectly, i didnt even need to use any other code examples because it was so simple to work out and use, and it runs perfectly! :D :D :D

thankyou so much for putting me onto that.

one question though... how do i put in a relative path for the path of the file? I have the file name and everything, but the only way i can link to it is if i put an absolute address. I thought something like app.path might work, but im not sure... i tried and nothing seemed to work. Is there something i am meant to import at the start of the form??

Error: Name 'App' is not declared.

i should probably be using something similar when linking my database to my other app, but it seems to be working file by just having
Data Source=""Food Items.mdb""


do you have any idea on how to get images to load from a database into the form? I dont care what it loads into really, just as long as i can get them to load from the database, and change like the rest of the text i am displaying in the labels.

thanks,

Luke

This post has been edited by Hiluke: 13 July 2006 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3