Thierry Savard Saucier
Thierry Savard Saucier

Reputation: 449

MS-Access and VB.NET Insert work half the time

This is NOT asp, just a regular local db program I'm trying to make.

i have this edit sub :

Protected Sub update()

    Dim sqlString As String
    Dim pair_0f_attribute As ArrayList = buildSqlUpdate()

    sqlString = "UPDATE " & getSharedTableName() & " SET "
    sqlString = sqlString & String.Join(", ", pair_0f_attribute.ToArray)
    sqlString = sqlString & " WHERE UID = " & id

    cmd = New OleDbCommand(sqlString, conn)
    cmd.CommandType = CommandType.Text

    ExecuteNonQuery(cmd)

End Sub

which gives me error on INSERT command. I've break down this huge insert( I got 70+ column, I dont wanna do it manually for all the 2000+ row on my db ) to only get 1 value at a time and check where it fails.

Public Sub editrow(ByVal ID As Integer, ByVal ColumnName As String, ByVal value As Object)

    Dim SqlString As String = "Update " & getSharedTableName() & " Set " & ColumnName & " = ? Where UID = " & ID

    Dim cmd As New OleDbCommand(SqlString, conn)

    cmd.CommandType = CommandType.Text

    cmd.Parameters.AddWithValue(ColumnName, value)

    ExecuteNonQuery(cmd)

End Sub

this work for username, which is a string, level which is an integer, xp which is an integer etc ... for a couple of insert, its good, than suddenly on the 8th I get an error on Insert statement.

my ColumnName is "Size", I tried to change it to "MSize" and I still get an error, and the value is "Medium".

why would this one fails when columnname "UserName" which use a string wokrs too ?

I've check my db to be sure, and the field Size (now MSize) is set at Text, there's no default value, it can be null, and have 255 max lenght. theres no format or validation or regex used on any field in the DB ...

Anyone have any hints on how I should proceed to repair this ?

** Edit

heres to buildSqlUpdate() I used :

 Private Function buildSqlUpdate() As ArrayList
    Dim pair_arraryList As New ArrayList
    For Each element As KeyValuePair(Of String, Object) In attributes
        pair_arraryList.Add("[" & element.Key & "] = '" & element.Value & "'")
    Next
    Return pair_arraryList
End Function

and before you ask ;) attributes is the DB column names stored in a dictionnary with their value filled in at run time when I build a new instance of an object, it works well, and the string before my insert ( even if way too long ) seems well built when I check it.

***** EDIT *****

I added the bracket as Remou suggested, and now I dont get the same error ... if I use the 1st insert ( the fully constructed one ) I get this error :

(syntax error (absent operator) in the following expressions : .... some string value ....

if I use the editrow I dont have any error ( but its long ... took about 2 minutes and only went up to 125 item out of 2k )

Upvotes: 0

Views: 338

Answers (2)

Thierry Savard Saucier
Thierry Savard Saucier

Reputation: 449

god, the problem was in the pre-made db, theres was errors in column names ( organisatoin instead of organisation ) ...

cant beleive it took me over a week to find that ...

Upvotes: 0

Fionnuala
Fionnuala

Reputation: 91326

Size is certainly a reserved word* and should be bracketed [size]. When creating code such as yours where you do not know the name of the fields (columns), I reckon it is always safer to assume you will need brackets.

* Reserved words in Jet and ACE

Upvotes: 2

Related Questions