string - VB.net- Data type mismatch in criteria expression -


i trying add vote count after selected user ddl. don't see did wrong.

 dim str string         str = "update [vote] set [voteweight] = [voteweight]+1 [userid] = 'dropdownlist4.selectedvalue'"      dim conn new oledbconnection("provider=microsoft.jet.oledb.4.0;; data source=" & server.mappath("app_data/final.mdb"))     dim cmd new oledbcommand(str, conn)     conn.open()     cmd.executenonquery()     conn.close() 

the correct way pass value sql command through parameterized query. otherwise errors embedded in string numerous , subtle.

in code, single quotes around control value, transform in literal string and, of course, userid field numeric field cannot compared against string

   if dropdownlist4.selectedvalue = nothing        return    end if         dim str = "update [vote] set [voteweight] = [voteweight]+1 " & _               "where [userid] = @userid"    using conn = new oledbconnection(.....)    using cmd new oledbcommand(str, conn)        conn.open()        cmd.parameters.add("@userid", oledbtype.int).value = convert.toint32(dropdownlist4.selectedvalue)        cmd.executenonquery()    end using    end using  

other things keep present are:

using statement around disposable object connection , command (thus avoiding possibility leak resources if exceptions triggers , forget dispose them)

a check around selectedvalue null measure. control loose selection , should absolutely sure there no way enter code selectedvalue = nothing.


Popular posts from this blog