Solved

Oledb   command text

Posted on 2013-06-07
9
322 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 M 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 M
ID: 39229731
Sorry... I forgot a single quote
TXTCUT.TEXT & "' where
0
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

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 M
ID: 39229883
Then you must have non-numeric characters in the textboxes?

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

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now