teogos
asked on
Oledb command text
Visual Studio 2010 Vb.net
I want to write these values to the database
using this but I am not sure of the sintax
cn = New OleDb.OleDbConnection
cn.ConnectionString = (My.Settings.cnStringAdo1)
command = New OleDb.OleDbCommand
command.Connection = cn
command.Connection.Open()
command.CommandText = ("UPDATE [3320Monitor].MASTERSCHEDU LE (BOXES,CUT,CODE) where CODE='" & txtcode.Text + "'")
command.ExecuteNonQuery()
command.Connection.Close()
I want to overwrite the records based on teh TXTCODE.TEXT
The fields I want to update BOXES, CUT, CODE their values come from a TXTBOXES.TEXT and TXTCUT.TEXT and TXTCODE.TeXT
AGAIN is WINDOWS FORMS 2010 VB
I want to write these values to the database
using this but I am not sure of the sintax
cn = New OleDb.OleDbConnection
cn.ConnectionString = (My.Settings.cnStringAdo1)
command = New OleDb.OleDbCommand
command.Connection = cn
command.Connection.Open()
command.CommandText = ("UPDATE [3320Monitor].MASTERSCHEDU
command.ExecuteNonQuery()
command.Connection.Close()
I want to overwrite the records based on teh TXTCODE.TEXT
The fields I want to update BOXES, CUT, CODE their values come from a TXTBOXES.TEXT and TXTCUT.TEXT and TXTCODE.TeXT
AGAIN is WINDOWS FORMS 2010 VB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry... I forgot a single quote
TXTCUT.TEXT & "' where
TXTCUT.TEXT & "' where
ASKER
Error converting data type varchar to numeric BOXES AND CUT ARE numeric on the database I can't change that
You would be better served if you used parameterized queries.
e.g.
There's no error handling in the above, so if either text box does not contain a valid integer, then you will encounter an exception. You would want to account for this, of course. Also, it's not clear from my example above, but the order that you add the parameters to the command object must be the same as how they appear in the query (for future reference).
e.g.
command.CommandText = ("UPDATE [3320Monitor].MASTERSCHEDULE SET BOXES = ?, CUT = ? where CODE= ?")
command.Parameters.AddWithValue("?", Convert.ToInt32(TXTBOXES.TEXT))
command.Parameters.AddWithValue("?", Convert.ToInt32(TXTCUT.TEXT))
command.Parameters.AddWithValue("?", Convert.ToInt32(TXTCODE.TeXT))
There's no error handling in the above, so if either text box does not contain a valid integer, then you will encounter an exception. You would want to account for this, of course. Also, it's not clear from my example above, but the order that you add the parameters to the command object must be the same as how they appear in the query (for future reference).
Then you must have non-numeric characters in the textboxes?
You'll need to check for that in code.
You'll need to check for that in code.
@xuserx2000
No, you need to remove the single quotes you added in the query from around the numeric values ; )
No, you need to remove the single quotes you added in the query from around the numeric values ; )
ahhh... you're right sir. But I agree with using the parameratized method though, which you posted.
ASKER
I got to it work using this
command.CommandText = "UPDATE [3320Monitor].MASTERSCHEDU LE SET BOXES='" & Val(txtboxes.Text) & "',CUT='" & Val(txtcut.Text) & "' where CODE='" & txtcode.Text & "'"
command.CommandText = "UPDATE [3320Monitor].MASTERSCHEDU
ASKER