Database won't update

Killgore

New member
Joined
Apr 9, 2008
Messages
3
Programming Experience
1-3
Hi I have this little problem here where I try to modify some infos from my database trough a form but it just wont update. Here is the code that I have right now
VB.NET:
 Private Sub btnModifyAssetInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifyAssetInfo.Click
        Dim conn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim Asset As String
        Dim Query As String
        Dim AssetBuilding As String


        Asset = frmAssetSearch.AssetNo
        AssetBuilding = txtBuilding.Text

        Query = "UPDATE Assettest Set Building AS '" + txtBuilding.Text + "', Rm AS '" + txtRoom.Text + "' WHERE NDecal ='" + Asset + "'"

        gbInfos.Text = "Asset No :  " + Asset

        conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=N:\IT Equipment Asset Management.mdb")
        cmd = New OleDbCommand(Query, conn)


        conn.Close()
        MsgBox("Asset updated successfully", MsgBoxStyle.Information, "Update")
    End Sub

And when I put a breakpoint here is what I get for the Query Variable :

UPDATE Assettest Set Building AS '250', Rm AS '1011' WHERE NDecal ='N0317168'"
 
...but it just wont update

Are you getting an error message? Does it appear to work correctly but the data doesn't get changed in the database?

I would look into using parameterized queries and using & to concatenate rather than +.

VB.NET:
...
Query = "UPDATE Assettext SET Building = ?, Rm = ? WHERE NDecal = ?"
...
cmd = New OleDbCommand(Query, conn)
conn.CommandType = CommandType.Text
conn.Parameters.AddWithValue("Building", txtBuilding.Text)
conn.Parameters.AddWithValue("Room", txtRoom.Text)
conn.Parameters.AddWithValue("AssetNumber", frmAssetSearch.AssetNo)

conn.Open()
cmd.ExecuteNonQuery()
...

Using a parameterized query would make it much easier to spot that instead of setting Building = value you're using Building AS value.
 

Latest posts

Back
Top