Updating database at runtime - vb.net

I can't update even if my code is correct.
I tried to change the data type from String to Integer but still nothing happens.
Private Sub UpdateQuanLoop()
For x As Integer = 0 To cartidentifier - 1
UpdateQuantity(ID(x), Quantity(x))
Next
End Sub
Private Sub UpdateQuantity(S_ID As String, S_Quan As String)
Dim finder As Integer
access.AddParam("#proID", S_ID)
access.ExecQuery("SELECT product_quantity FROM product WHERE product_ID = #proID;")
Dim Z As DataRow = access.DBDT.Rows(0)
finder = Z("product_quantity").ToString - S_Quan
access.AddParam("#ID", S_ID)
access.AddParam("#quan", finder)
access.ExecQuery("UPDATE product SET product_quantity = #quan WHERE product_ID = #ID;")
If NoErrors(True) = False OrElse access.RecordCount < 1 Then Exit Sub
End Sub
I don't have any error but the database is not updated.

Related

CATVBA - CATIA recursive loop throught Product Tree

Sorry but I'm a total newbie in CATScript.
But I'm looking for a solution that will provide me to check every node in my Product structure recursively.
I try to adopt the Fibonacci procedure:
Function Fib(n As Long) As Long
Dim first As Long
Dim second As Long
Dim sum As Long
Dim i As Long
first = 0
second = 1
sum = 0
If n = 0 Then
Fib = first
ElseIf n = 1 Then
Fib = second
Else
For i = 2 To n
sum = first + second
first = second
second = sum
Next i
Fib = sum
End If
End Function
with this:
Private Sub TestMain
Dim searchName As String
searchName = "SearchName"
' Start with the selected object
Dim doc As Document
Set doc = CATIA.ActiveDocument
Dim prod As Product
Set prod = doc.Product
Dim foundItem As Object
foundItem = TestMainChildren(doc.Selection.Item(1).Value, searchName)
MsgBox "Found: " & foundItem.Name
End Sub
Private Function TestMainChildren(ByRef catiaObject As Object, ByVal searchName As String) As Object
Dim item As Object
For Each item In catiaObject.Items
If item.Name = "SearchName" then
Set TestMainChildren = item
Exit For
End if
Dim catiaType As String
catiaType = TypeName(item)
If catiaType = "Product" Then
TestMainChildren item, searchName
End If
Next
End Sub
but I have no idea how to do this. Can anybody help here?
It depends on what you want, but it is often very useless to check all the instances whith a recursive loop.
what is your end goal?
i suggest you to check every instance opened :
Sub main()
Dim d As Document
For Each d In CATIA.Documents
Dim p As Product
Set p = d.Product
MsgBox (p.Name)
Next
End Sub
If you insist and really want a recursive loop :
Sub main()
Dim d As Document
Set d = CATIA.ActiveDocument
Dim p As Product
Set p = d.Product
Call RecursiveAllProducts(p) 'here your recursive starts
End Sub
Sub RecursiveAllProducts(p As Product) 'your recursive
MsgBox (p.PartNumber)
If p.Products.Count > 0 Then
For i = 1 To p.Products.Count
Dim p_ As Product
Set p_ = p.Products.Item(i)
Call RecursiveAllProducts(p_) 'you call your recursive again
Next i
End If
End Sub

Visual basic ListView: How to disable auto select in first row?

I've made a listview command where I press a button and the item automatically appears in listview. The listview has 3 columns: Order list, Price list, and Quantity. I've added a button where it removes the quantity and decreases the price according to its information. I've succeeded in removing the specific quantity by selecting it and it works only with one item, but whenever more items are added, it doesn't work anymore. Thanks in advance for the help!
The program : Removing the quantity of the order : More items added : The error occurs here
Private Sub Button15_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
If olqlistview.SelectedItems.Count = 0 Then
Beep()
MessageBox.Show("Please select an item.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Dim totale As Integer, pt As Integer, tp As Integer
Dim x As Integer = olqlistview.SelectedItems.Item(0).SubItems(2).Text, y As Integer = olqlistview.SelectedItems.Item(1).SubItems(1).Text
For i = 0 To olqlistview.SelectedItems.Count
If i = 1 Then
Dim lve As New ListViewItem
totale = Val(olqlistview.SelectedItems.Item(0).SubItems(2).Text) - 1
olqlistview.Items(0).SubItems(2).Text = CStr(totale)
tp = Val(y) / Val(x)
pt = Val(y) - Val(tp)
olqlistview.SelectedItems.Item(0).SubItems(1).Text = Val(pt)
End If
If x = 1 And i = 1 Then
olqlistview.Items.Remove(olqlistview.SelectedItems(0))
End If
Next
Dim lv As ListViewItem
Dim total As Long
Dim quantitytotal As Long
For Each lv In olqlistview.Items
total = total + CStr(lv.SubItems(1).Text)
quantitytotal = quantitytotal + CStr(lv.SubItems(2).Text)
Next
amtdue.Text = total
Label1.Text = total
tq.Text = quantitytotal
End If
End Sub
If olqlistview.SelectedItems.Count = 0 Then
Beep()
MessageBox.Show("Please select an item.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Dim totale As Integer, pt As Integer, tp As Integer
Dim x As Integer = olqlistview.SelectedItems.Item(0).SubItems(2).Text, y As Integer = olqlistview.SelectedItems.Item(1).SubItems(1).Text
For i = 0 To olqlistview.SelectedItems.Count
If i = 1 Then
Dim lve As New ListViewItem
totale = Val(olqlistview.SelectedItems.Item(0).SubItems(2).Text) - 1
olqlistview.Items(0).SubItems(2).Text = CStr(totale)
tp = Val(y) / Val(x)
pt = Val(y) - Val(tp)
olqlistview.SelectedItems.Item(0).SubItems(1).Text = Val(pt)
End If
If x = 1 And i = 1 Then
olqlistview.Items.Remove(olqlistview.SelectedItems(0))
End If
Next
Dim lv As ListViewItem
Dim total As Long
Dim quantitytotal As Long
For Each lv In olqlistview.Items
total = total + CStr(lv.SubItems(1).Text)
quantitytotal = quantitytotal + CStr(lv.SubItems(2).Text)
Next
amtdue.Text = total
Label1.Text = total
tq.Text = quantitytotal
End If
End Sub
I have a hard time following the steps, and determining the problem vs the desired results (which are never stated). This is what I see as the sequence
Is line 8 supposed to revert from line 4 back to line 1? or is line 8 a typo?
What was attempted in line 10? What are the expected results? Just saying "it doesn't work anymore" is not clear.

Add quantity if the DataGridView item already exists

I have created a cart using a DataGridView and on the add to cart button I have this code:
Dim a As Integer
Dim total As Integer
Dim b As Integer
a = TextBox2.Text
b = NumericUpDown56.Value
total = a * b
ShoppingCart.DataGridView1.Rows.Add(Me.Label11.Text, Me.TextBox2.Text, Me.NumericUpDown56.Value, total)
Me.Hide()
ShoppingCart.Show()
How can I add quantity if I re-added the same item?
It is a little hard to answer this without knowing quite what the textbox and updowncounter do... but assuming you only expect the updown counter to change...
You can iterate through the data and hunt for the same text in the first column
Dim a As Integer
Dim total As Integer
Dim b As Integer
a = TextBox2.Text
b = NumericUpDown56.Value
total = a * b
Dim Updated As Boolean
For Each row As DataGridViewRow In ShoppingCart.DataGridView1.Rows
If CType(row.Cells(0).Value, String) = Me.Label11.text Then
row.Cells(2).Value = CType(row.Cells(0).Value, Integer) + b
row.Cells(3).Value = CType(row.Cells(3).Value, Integer) + total
Updated = True
Exit For
End If
Next
If Not Updated Then ShoppingCart.DataGridView1.Rows.Add(Me.Label11.Text, Me.TextBox2.Text, Me.NumericUpDown56.Value, total)
Me.Hide()
ShoppingCart.Show()
Though personally I'd bind the datagridview to a of List(of T) (t = a class that retains your properties) and search / modify that instead, then rebind the list.
Something along these lines...
Private Class cls_Cart_Item
Public Property Item_Name As String
Public Property Whatever_TExtbox2_IS_SUpposed_to_be As Integer
Public Quantity As Integer
Public Sub New(wName As String, wWhatever_TExtbox2_IS_SUpposed_to_be As Integer, wQuantity As Integer)
Item_Name = wName
Whatever_TExtbox2_IS_SUpposed_to_be = wWhatever_TExtbox2_IS_SUpposed_to_be
Quantity = wQuantity
End Sub
Public ReadOnly Property Total As Integer
Get
Return Whatever_TExtbox2_IS_SUpposed_to_be * Quantity
End Get
End Property
End Class
Private Cart As New List(Of cls_Cart_Item)
Private Sub Add_Or_Update()
ShoppingCart.datagridview1.datasource = Nothing
Dim a As Integer
Dim total As Integer
Dim b As Integer
a = TextBox2.Text
b = NumericUpDown56.Value
Dim Item As New cls_Cart_Item = Cart.Find(Function(x) x.Item_Name = Me.Label11.Text)
If Item Is Nothing Then
Cart.Add(New cls_Cart_Item(Me.Label11.Text, a, b))
Else
Item.Quantity += b
End If
Me.Hide()
ShoppingCart.datagridview1.datasource = Cart
End Sub
Though it's hard to tell from your question where this code is coming from relative to the daraviewgrid.

How can I put an extra value?

I have a table (DataGridView) like this :
Col1 | Col2 | Col3
3 | Mars | Regular
Here is my code:
For a As Integer = 0 To Form3.DataGridView1.Rows.Count - 1
For b As Integer = 0 To Form3.DataGridView1.Rows.Count - 1
For c As Integer = 0 To Form3.DataGridView1.Rows.Count - 1
If Form3.DataGridView1.Rows(c).Cells(2).Value = "Regular" Then
If Form3.DataGridView1.Rows(b).Cells(1).Value = Form3.MetroComboBox7.Items(0) Then
fair = 7 * Form3.DataGridView1.Rows(a).Cells(0).Value
Label1.Text += fair
End If
End If
Next
Next
Next
I want to set that if Regular is selected on Col3 and Mars on Col2 then the value is 7 and it will multiply by row Col1 and it will be the same every row.
I think you should use the event dtgv.CellMouseClick.
Then you create conditions that you want. I give you an example here :
Public Sub event_select() Handles dtgv.CellMouseClick
Dim row As Integer = dtgv.CurrentRow.Index()
' If the column 2 and 3 are selected
If dtgv.Rows(row).Cells(1).Selected = True And dtgv.Rows(row).Cells(2).Selected = True Then
' If the value of the 2nd column is Mars and the value of the 3rd column is Regular
If dtgv.Rows(row).Cells(1).Value = "Mars" And dtgv.Rows(row).Cells(2).Value = "Regular" Then
Label1.Text = 7 * dtgv.Rows(row).Cells(0).Value
End If
End If
End Sub
You should also check that no other rows have cells selected.
One loop for all rows is enough to calculate "fair" for all rows
Const REGULAR_VALUE As String = "Regular"
Const FAIR_COEFFICENT As Integer = 7
Dim fairSum As Integer = 0
For Each row As DataGridViewRow in DataGridView1.Rows
If REGULAR_VALUE.Equals(row.Cells(2).Value.ToString()) = False Then Continue For
If Equals(MetroComboBox7.Items(0), row.Cells(1).Value) = False Then Continue For
Dim col1Value As Integer = Integer.Parse(row.Cells(1).Value)
Dim fair As Integer = col1Value * FAIR_COEFFICENT
fairSum += fair
Next
Label1.Text = fairSum.ToString()
And set Option Strict On in your project or at least in the code file(first line of file).
This will save you time by giving fast feedback about possible type converting errors during compile time.
Create class which represent your data in strongly typed manner
Public Class Ticket
Public Property Passenger As Integer
Public Property Destination As String
Public Property Status As String
End Class
Then you can add rows to the DataGridView in easy way in your form
Public Class YourForm
Private _tickets As New BindigList(Of Ticket)()
Public Sub New()
InitializeComponent() ' Forms required method
DataGridView1.DataSource = _tickets
End Sub
Private Sub Populate(passenger As Integer, destination As String, Status As String)
Dim newTicket As New Ticket With
{
.Passenger = passenger,
.Destination = destination,
.Status = Status,
}
_ticket.Add(newTicket)
End Sub
'Then you can loop all rows with correct types
Private Sub Calculate()
Dim fairSum As Integer = 0
For Each ticket As Ticket in _tickets
If REGULAR_VALUE.Equals(ticket.Status) = False Then Continue For
If ticket.Destination.Equals(MetroComboBox7.Items(0)) = False Then Continue For
Dim fair As Integer = ticket.Passenger * FAIR_COEFFICENT
fairSum += fair
Next
Label1.Text = fairSum.ToString()
End Sub
End Class

Visual Basic: How can i display the prime numbers between 1 and the inputted number

Hello everyone so i'm trying to find the prime numbers of any input. I want them to be in a listbox and the input in a text box. I would like to use two arguments but i don't know how to. this is the code i have i need dire help. I am not the best at visual basic i just need some guidance. My code isn't working but display a drop down box when i press display.
Public Class Form1
Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
Dim prim As Integer
Dim test As Integer
Dim imPrime As Boolean = False
prim = CInt(txtNum.Text)
test = prim
If prim = 1 Then
imPrime = False
MessageBox.Show("Enter a number greater than one please")
Else
Do While prim >= 2
For i As Integer = 2 To prim
If prim Mod i = 0 Then
imPrime = False
Exit For
Else
imPrime = True
lstPrime.Items.Add(prim)
End If
Next
Loop
End If
If imPrime = True Then
lstPrime.Items.Add(prim)
End If
End Sub
End Class
This is my fastest VBA code to generate prime numbers between two numbers.
The generated prime numbers are put in clipboard. You will need to open
your ms office word and type Ctrl+V to view all the generated prime numbers.
Sub generateprimenumbersbetween()
Dim starting_number As Long
Dim last_number As Long
Dim primenumbers As Variant
Dim a As Long
Dim b As Long
Dim c As Long
starting_number = 1 'input value here
last_number = 1000000 'input value here
primenumbers = ""
For a = starting_number To last_number
c = Round(Sqr(a)) + 1
For b = 2 To c
If a = 1 Or (a Mod b = 0 And c <> b) Then
Exit For
Else
If b = c Then
primenumbers = primenumbers & " " & a
Exit For
End If
End If
Next b
Next a
Dim answer As DataObject
Set answer = New DataObject
answer.SetText primenumbers
answer.PutInClipboard
End Sub
I think the while loop is not working as you intend. You need two loops, the first one counting up to the possible prime, and an inner one counting up to the counter in the outer loop.
You can find examples everywhere... here's one implemented in C#, but since your question was specifically about a listbox, I've translated it to VB.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
calculatePrimes()
End Sub
Private Sub calculatePrimes()
Dim prim As Integer
Dim count As Integer = 0
prim = CInt(Me.TextBox1.Text)
If prim < 3 Then
MsgBox("Please enter a bigger number")
Return
End If
Me.ListBox1.Items.Clear()
For i As Integer = 1 To prim
Dim isPrime As Boolean = True
For j As Integer = 2 To i
If (i Mod j <> 0) Then count = count + 1
Next
If count = (i - 2) Then Me.ListBox1.Items.Add(i)
count = 0
Next
End Sub
End Class
(This assumes you have a textbox for input called TextBox1 and a listbox for display called ListBox1)