views:

99

answers:

1

I am using a data grid view to display information from a database and I can't get it to update correctly. I can see the data from the database but when I change information and save it it changes the entire column's information. My rows need to have different values in the same column so this is a problem.

-Here is my code so far, what the program does is makes a list of transactions that are entered.

Public Class Form1

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
     Handles Button3.Click

    If MsgBox("Add new transaction?", MsgBoxStyle.YesNo, " ") <> 7 Then
        If IsNumeric(in_cost.Text) Then
            Dim x As Int16 = Database1DataSet.Table1.Count + 1      '//gets # value
            Me.Table1TableAdapter.Insert(Format(Now, "dddd, M/dd/yy"), x, in_cost.Text, in_prod.Text, in_type.Text, in_note.Text, 9999.99)   '//Writes new index to DB table
            Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1)
        Else
            MsgBox("Cost is non-numeric!")
        End If
    End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Table1TableAdapter.Update(Me.Database1DataSet.Table1)
End Sub
End Class

-SQL UPDATE command:

UPDATE Table1

SET        Balance = @p1, Note = @p2, Type = @p3, Product = @p4, Cost = @p5, # = @p6, Date = @p7

Thanks in advance for any help.

+4  A: 

It changes every row because your update statement has no where clause. Without a where clause, there's no predicate, so it matches every row, and so every row gets updated.

You need a where clause (a predicate) on your update statement. Probably "where id = @id" at the end of your update statement.

tpdi
This worked, thank you very much!
No prob, go ahead and mark it as best answer! ;)
tpdi
+1 For being blatantly asking to mark the *best* answer. Yes. This is the great answer still ;)
Sung Meister