Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hy everyone
I need help for my vb project
last day i created stock database
the database have 2 coloums 1st"Item Name" 2nd "Quantity"
In my form i Have two textboxes same as database and a button
when button press
if my "Itemname textbox" match with database coloum{itemname} then "Quantity Textbox" subtract with "Quantitycoloum"
if match with database "itemname coloum"[2] the coloum2 quantity subtract with "Quantitytextbox"
here My Code its work but So Lengthy:-

VB
If itemname.Text = Me.DataGridView1.Rows(0).Cells(0).Value Then
            Me.DataGridView1.Rows(0).Cells(1).Value = CDbl(Me.DataGridView1.Rows(0).Cells(1).Value) - CDbl(quantity.Text)
            MsgBox("Data Saved SuccessFully")
            quantity.Text = ""
            itemname.Text = ""

        ElseIf itemname.text = Me.DataGridView1.Rows(1).Cells(0).Value Then
            Me.DataGridView1.Rows(1).Cells(1).Value = CDbl(quantity.Text) - CDbl(Me.DataGridView1.Rows(1).Cells(1).Value)
            MsgBox("Data Saved SuccessFully")
            quantity = ""
            itemname.Text = ""

        ElseIf itemname.Text = Me.DataGridView1.Rows(2).Cells(0).Value Then
            Me.DataGridView1.Rows(2).Cells(1).Value = CDbl(quantity.Text) - CDbl(Me.DataGridView1.Rows(2).Cells(1).Value)
            MsgBox("Data Saved SuccessFully")
            quantity = ""
            itemname.Text = ""


        ElseIf itemname.Text = Me.DataGridView1.Rows(3).Cells(0).Value Then
            Me.DataGridView1.Rows(3).Cells(1).Value = CDbl(quantity.Text) - CDbl(Me.DataGridView1.Rows(3).Cells(1).Value)
            MsgBox("Data Saved SuccessFully")
            quantity.Text = ""
            itemname.Text = ""

          endif
end sub
And So on.......
PLEASE HELP ....
Posted
Updated 1-May-14 13:35pm
v7
Comments
[no name] 1-May-14 20:50pm    
Well the first thing that you need to do is to delete all of your duplicated code. Do you really need to show a messagebox and clear the textboxes in every "if" block? Do it once and be done with it.
As for the other confusing bit. Looks to me that you would probably benefit from learning how to write a "for" loop.
Maciej Los 2-May-14 2:07am    
My virtual 5!

1 solution

VB
For Each row As DataRow In Me.DataGridView1.Rows
	If itemname.Text = row.Cells(0).Value Then
		row.Cells(1).Value = Convert.ToDouble(row.Cells(1).Value) - Convert.ToDouble(quantity.Text)
		Interaction.MsgBox("Data Saved SuccessFully")
		quantity.Text = ""
		itemname.Text = ""
		Exit For
	End If
Next


read For Each...Next Statement (Visual Basic)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900