Link to home
Start Free TrialLog in
Avatar of David Peruso
David Peruso

asked on

Problem with the SQL update command using WHERE in vb.net(15)

I have tried various combinations for the WHERE that I found on the net but didn’t fix the problem
Here is a code snipit

'PcbNumber is Key field

‘-----------------------------------------------------------------------------
oConn = New OleDbConnection(strSQLpath)
            'Open the connection
            oConn.Open()

            oQuery = "UPDATE PcbBom SET pq1= 222222 WHERE  PcbNumber = 2990"

            'oQuery = "INSERT INTO PcbBom (PcbNumber, pq1) VALUES (2875a, 555555)"

            oComm = New OleDbCommand(oQuery, oConn)
            oComm.ExecuteNonQuery()
            'dsDdsMrpDatbaseWorking.Tables("PcbBom").Columns.Add(teststr)

            oConn.Close()

‘--------------------------------------------------------------------------


The insert command works, I can add a new row,  I can update all pq1 columns if I don’t use the WHERE statement,
but when I try to update the field “pq1” using the WHERE I get this error message:


AT SUB/FUNCTION LOCATION: FunSaveBom 0
System.Data.OleDb.OleDbException (0x80040E07): Data type mismatch in criteria expression.
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   at DDS_MRP.ModuleSubs.FunSaveBom(String strQuery) in C:\DDS MRP\DDS MRP\DDS MRP\ModuleSubs.vb:line 625



As a side note I’m connecting to multiple tables in the database and for some reason I can only use the dataadaptor.update on one table, if I try to use the dataadaptor.update method on any other table I get errors so I decided to try using the SQL method, using SQL I can add columns and rows. Now all I need  to do is to be able to edit specific fields.



when I connect to the database:
sqlString = "SELECT * FROM PcbBom"

This is one of the five adaptors I’m using
daPcbBomWorking = New OleDb.OleDbDataAdapter(sqlString, con)
daPcbBomWorking.Fill(dsDdsMrpDatbaseWorking, "PcbBom")
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Given the insert statement pcbnumber is a character field so the data type mismatch leans towards a data type conversion.

Try:
oQuery = "UPDATE PcbBom SET pq1= 222222 WHERE  PcbNumber = '2990'"
Avatar of David Peruso

ASKER

Thanks,
That worked, I thought I tried that before.
Sometimes you just get wrapped around the axial.
You should also convert them to BIND variables.
hmm, sure that

'oQuery = "INSERT INTO PcbBom (PcbNumber, pq1) VALUES (2875a, 555555)"

Open in new window

worked??
Verify the data type of the "pq1" column.  Or just add quotes around the 222222 also, since quotes can't hurt, but could help tremendously if applicable:

oQuery = "UPDATE PcbBom SET pq1= '222222' WHERE  PcbNumber ='2990' "
>>since quotes can't hurt

The "can" for the same reason the update didn't work to begin with.  Some times implicit data type conversions don't go as expected.

You should get in the habit and ALWAYS do explicit type conversions and use strings with strings, numbers with numbers and dates with dates.  Then you can NEVER go wrong (I know I'll regret saying NEVER).
>>since quotes can't hurt

The "can" for the same reason the update didn't work to begin with.  Some times implicit data type conversions don't go as expected.

I don't see how it could hurt, since char/varchar is the LCD format.  How could it hurt SQL performance?  SQL will implicitly convert varchar to a higher precedence data type if/when needed.

What could seriously hurt is specifying a numeric comparison value to a varchar column: then SQL must implicitly convert every column value to numeric before comparing, OUCH!
I was commenting on "pq1= '222222'"  If pq1 is a number column, adding the quotes is unnecessary and silly.

>>then SQL must implicitly convert every column value to numeric before comparing, OUCH!

Yes, if one explicitly converts all the time, no ouch.

I stand by my statement on getting into the habit of always using explicit data type conversions.
And I stand by using varchar for literal value when it's at all possible the column data type will change.  Otherwise, you risk having to do a lot of re-work to avoid errors.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.