Search code examples
databasevb.netms-accessoledb

Save in database didn't work ?! (VB.NET | OLE)


I used the following code to save what the user editing , the code don't give me any error code , but in the same time it don't do any thing , the data still without any new changes :

 Dim a As String
        a = comb3.Text & "/" & comb2.Text & "/" & comb1.Text
        Dim SavInto As New OleDb.OleDbCommand
        Dim ConStr As String = _
           "Provider=Microsoft.Jet.OLEDB.4.0; Data Source =" & _
          Application.StartupPath & "\base.mdb"
        Dim Conn As New OleDbConnection(ConStr)
        Conn.Open()
        SavInto.Connection = Conn
        SavInto.CommandType = CommandType.Text
        SavInto.CommandText = "UPDATE tb SET Tasalsol = '" & _
       Trim(frm.tx1.Text) & _
           "' , anomber = '" & Trim(frm.TextBox2.Text) & _
         "' , nam= '" & Trim(frm.tx3.Text) & _
        "' , rotba= '" & Trim(frm.tx4.Text) & _
          "' , spesh= '" & Trim(frm.tx5.Text) & _
         "' , lastvstart= '" & a & _
           "'  WHERE Notes ='" & Trim(frm.tx8.Text) & "'"

        SavInto.ExecuteNonQuery()
        Conn.Close()

        With frm
            .tx1.Text = Me.tx1.Text
            .TextBox2.Text = Me.tx2.Text
            .tx3.Text = Me.tx3.Text
            .tx4.Text = Me.tx4.Text
            .tx5.Text = Me.tx5.Text
            .tx6.Text = a
            .tx8.Text = Me.tx8.Text

        End With
        frm.Show()
        Me.Close()

I wrote the whole code , so what is the wrong exactly ?! and thank you

Note:There is some columns that may have many same data in its cells .


Solution

  • Aside from Mehrdad's very wise advice about using a parameterised query instead, your WHERE clause looks very odd. Are you really sure you want to be using a "notes" field to determine which row to update? And is tx8 an editable field? Why are you trimming it? It seems very unlikely that the best way of finding the row to update is via a field called "notes" which appears to be plain text which might require trimming.

    It would be much better to use the ID of the row (or rows) in question to update it.

    Note that you're not looking at the return value of ExecuteNonQuery - I suspect it's returning 0, to indicate that it's not updated any rows. You should be checking that.

    Also you should be using Using statements for both the connection and the command, so that they will be disposed appropriately even an exception is thrown.

    Just to reiterate though: please, please use parameterised queries instead of embedding values in SQL. Queries like that make DBAs cry.