Solved

Oledb   command text

Posted on 2013-06-07
9
333 Views
Last Modified: 2013-06-08
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].MASTERSCHEDULE (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
0
Comment
Question by:teogos
  • 4
  • 3
  • 2
9 Comments
 
LVL 25

Accepted Solution

by:
Ron Malmstead earned 500 total points
ID: 39229609
command.CommandText = "UPDATE [3320Monitor].MASTERSCHEDULE SET BOXES='" & TXTBOXES.TEXT & "',CUT='" & TXTCUT.TEXT & " where CODE='" & txtcode.Text & "'"

..also, if you do

Dim RecordsCount as integer = command.ExecuteNonQuery()

You'll get the number of records affected.
0
 

Author Comment

by:teogos
ID: 39229643
command.ExecuteNonQuery()  error here unclosed quotation mark after the character string
0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39229731
Sorry... I forgot a single quote
TXTCUT.TEXT & "' where
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:teogos
ID: 39229835
Error converting data type varchar to numeric    BOXES AND CUT ARE numeric on the database I can't change that
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39229879
You would be better served if you used parameterized queries.

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))

Open in new window


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).
0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39229883
Then you must have non-numeric characters in the textboxes?

You'll need to check for that in code.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39229885
@xuserx2000

No, you need to remove the single quotes you added in the query from around the numeric values  ; )
0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39230077
ahhh... you're right sir.  But I agree with using the parameratized method though, which you posted.
0
 

Author Comment

by:teogos
ID: 39231855
I got to it work using this
command.CommandText = "UPDATE [3320Monitor].MASTERSCHEDULE SET BOXES='" & Val(txtboxes.Text) & "',CUT='" & Val(txtcut.Text) & "' where CODE='" & txtcode.Text & "'"
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question