2 Replies - 654 Views - Last Post: 10 March 2016 - 09:06 AM Rate Topic: -----

#1 TroopPC   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 10-March 16

Trying to change a specific line in a text file from one button

Posted 10 March 2016 - 05:14 AM


I am currently making a shopping basket using GTIN8 codes, and have done fine with being able to read a certain line of text from a text file and put it into a text box,

My only problem is that im currently trying to update stock after a purchase my program currently displays the stock amount, which is read from the same text file, yet I have no idea how to update the the certain line of the file after they take out for example 30 from the stock 50 and the text file now shows 20.

Here is what I have so far, I have also tried so many different ways yet they are not suited to me code.

    Private Sub btnQuantity_needed_Click(sender As Object, e As EventArgs) Handles btnAmount.Click

        Dim instock As Boolean

        instock = False

            quantity = InputBox("Enter the quantity needed")

            If quantity > txtStock.Text Then MsgBox("Not enough in stock")
            If quantity < txtStock.Text Then instock = True

        Loop Until instock

        total = (quantity * txtCost.Text)

        txtTotal.Text = "" & total

        stock = txtStock.Text

        txtStock.Text = stock - quantity

    End Sub

Thank you for your help in advance. :bigsmile:

Is This A Good Question/Topic? 1
  • +

Replies To: Trying to change a specific line in a text file from one button

#2 PeterH   User is offline

  • D.I.C Regular

Reputation: 60
  • View blog
  • Posts: 289
  • Joined: 03-September 09

Re: Trying to change a specific line in a text file from one button

Posted 10 March 2016 - 06:39 AM

That's the problem when using a text file, you will have to 'save' the complete file, after making any changes to the specific line. There are topic at the foot of this page which will show you how to do this. Normally if you have multiple items in the file, you would read the complete file into an array, change the details for the line and assign it back to the correct position in the array and then write the file back to disk.

You should also be aware of performing calculations on string fields e.g. 'txtCost.Text', these need to converted to a numeric value before used. check out the decimal.TryParse method.

This post has been edited by PeterH: 10 March 2016 - 06:47 AM

Was This Post Helpful? 1
  • +
  • -

#3 dday9   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 495
  • Joined: 17-April 13

Re: Trying to change a specific line in a text file from one button

Posted 10 March 2016 - 09:06 AM

Does it need to be a Text file? In this case I think that a DataTable would be a better suit. Take a look at this example:
Option Strict On

Imports System
Imports System.Data
Imports System.Data.DataSetExtensions
Imports System.Linq
Imports System.Xml
Public Module Module1
	Public Sub Main()
		'Create a new DataTable to hold the stock
		Dim stock As New DataTable("Stock")
		If IO.File.Exists("stock.xml") Then
			'If the DataTable has been saved in the past, then load it
			'If the DataTable has NOT been saved in the past, then create the columns
			stock.Columns.AddRange({ _
					New DataColumn("GTIN"), _
					New DataColumn("Description"), _
					New DataColumn("Price", GetType(Decimal)), _
					New DataColumn("Stock", GetType(UInt32)) _
			'Also load the DataTable with some default values
			'Please note that these are not actual GTIN-8 codes, they're a combination of arbitrary letters and numbers
			stock.Rows.Add({"A123", "1 Dozen Eggs", 2.15, 50})
			stock.Rows.Add({"A234", "1 Gallon of Milk", 4.5, 30})
			stock.Rows.Add({"A345", "8 Slices of Pepper Jack Cheese", 1.99, 15})
			stock.Rows.Add({"B123", "1/2 Pound of Black Forest Ham Slices", 2.88, 20})
		End If
		'Loop until the user enters a valid GTIN
		Dim product As DataRow = Nothing
			Console.Write("Product Needed: ")
			Dim input As String = Console.ReadLine()
			product = (From r As DataRow In stock.AsEnumerable() Where r.Field(Of String)("GTIN") = input Select r).SingleOrDefault()
			If product Is Nothing Then
				Console.WriteLine("Could not located product with that GTIN. Please try again.")
			End If
		Loop Until product IsNot Nothing
		'Loop until the user enters a valid UInt32 and the value is less than or equal to the amount of stock for the product they chose
		Dim instock As Boolean = False
		Dim quantity As UInt32 = 0
			Console.Write("Quantity Needed: ")
			If UInt32.TryParse(Console.ReadLine(), quantity) Then
				instock = product.Field(Of UInt32)("Stock") >= quantity
				If Not instock Then
					Console.WriteLine("There is not enough {0} in stock. There is only {1} left.", product.Item("Description"), product.Item("Stock"))
				End If
				instock = False
				Console.WriteLine("Please enter a valid number between {0} and {1}", UInt32.MinValue, UInt32.MaxValue)
			End If
		Loop Until instock
		'Subtrack the quantity from the stock
		product.Item("Stock") = product.Field(Of UInt32)("Stock") - quantity
		'Save the DataTable
	End Sub
End Module

Fiddle: https://dotnetfiddle.net/uscJlh

I use LINQ on the DataTable because I'm not in a Windows Form Application, otherwise I would have bound a BindingSource to the DataTable and then filtered the BindingSource.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1