Problem updating Database

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 9
Reputation: Kristofferson is an unknown quantity at this point 
Solved Threads: 0
Kristofferson Kristofferson is offline Offline
Newbie Poster
 
0
  #11
Oct 15th, 2009
I like the ease of the parameters I will try to implement it more when I code, but I still get the same error when I run it. "Syntax error in the UPDATE statement." Could it be a problem with the parameters in this case or is there still a problem with the update statement only.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
0
  #12
Oct 16th, 2009
You have to turn on "write" permission to the Users group for the database file.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: Kristofferson is an unknown quantity at this point 
Solved Threads: 0
Kristofferson Kristofferson is offline Offline
Newbie Poster
 
0
  #13
Oct 16th, 2009
Well what I did earlier is set my UPDATE statement to

  1. Update Employee Set Phone = ""

Just to see if the the update statment syntax was correct. It worked it effectively erased the entire Phone column. Wouldn't that mean that I can write to the database?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
0
  #14
Oct 16th, 2009
Can I see the update statement you wrote?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
0
  #15
Oct 16th, 2009
Relooking at my example, my syntax was incorrect and I hope you caught this mistake. I didnt use a comma between each of the columns & values and instead used the key word "And". Thats what I get for attempting to code in this webpage textbox... lol

strSQL = "Update [Employee] Set First_Name = @FName, Last_Name = @LName  Where Employee_ID =  @Id"

Originally Posted by TomW View Post
Parameters are pretty easy, I dont know why they dont push it more then the concatenated strings. Below is a quick example and here is a link for some more detailed info Configuring Parameters and Parameter Data Types (ADO.NET)

strSQL = "Update [Employee] Set First_Name = @FName And Last_Name = @LName  Where Employee_ID =  @Id"

command.Transaction = myTransaction       
command.CommandType = CommandType.Text            
command.CommandText = strSQL

Command.Parameters.AddWithValue("@FName", txtFName.Text)
Command.Parameters.AddWithValue("@LName", txtLName.Text)
Command.Parameters.AddWithValue("@Id", cint(txtStaffId.Text))

command.ExecuteNonQuery()
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #16
Oct 16th, 2009
Originally Posted by Kristofferson View Post
Well what I did earlier is set my UPDATE statement to

  1. Update Employee Set Phone = ""

Just to see if the the update statment syntax was correct. It worked it effectively erased the entire Phone column. Wouldn't that mean that I can write to the database?
Yes that does indicate you can write to the database. Follow the suggestion TomW posted. Always use parameterized SQL. Dynamically build parameterized SQL statements if you need to for search screens or reports but never put user input in your raw TSQL command text.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 9
Reputation: Kristofferson is an unknown quantity at this point 
Solved Threads: 0
Kristofferson Kristofferson is offline Offline
Newbie Poster
 
0
  #17
Oct 16th, 2009
So here is my code as it stands now. The update statement is setup according to the way TomW recommended. But I still get the error "Syntax error in the UPDATE statement."

  1. Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  2. Dim myTransaction As System.Data.OleDb.OleDbTransaction
  3.  
  4. Try
  5.  
  6. ' Declares connection and variables and opens connection to database
  7. Dim conn As New OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" & _
  8. "Data Source=C:\Users\Christopher Carr\Documents\Visual Studio 2008\WebSites\MYTPSS new\App_Data\tpsDatabase.accdb")
  9. conn.Open()
  10. Dim command As OleDbCommand = conn.CreateCommand()
  11. Dim strSQL As String
  12.  
  13. ' Adds and New Transaction
  14. myTransaction = conn.BeginTransaction
  15.  
  16. ' Sets what strSQL display value is
  17. strSQL = "Update Employee Set First_Name = @FirstName, Last_Name = @LastName, Phone = @Phone, E-mail = @Email, Address = @Address, Availability = @Availability Where Employee_ID = @EmployeeID"
  18.  
  19. ' Gets a value indicating how the CommandText property is to be interpreted and CommandText is set equal to the strSQL value
  20. command.CommandType = CommandType.Text
  21. command.CommandText = strSQL
  22.  
  23. ' Starts the transaction
  24. command.Transaction = myTransaction
  25.  
  26. command.Parameters.AddWithValue("@FirstName", txtFirstName.Text)
  27. command.Parameters.AddWithValue("@LastName", txtLastName.Text)
  28. command.Parameters.AddWithValue("@Phone", txtPhone.Text)
  29. command.Parameters.AddWithValue("@Email", txtEmail.Text)
  30. command.Parameters.AddWithValue("@Address", txtAddress.Text)
  31. command.Parameters.AddWithValue("@Availability", txtAvailability.Text)
  32. command.Parameters.AddWithValue("@EmployeeID", CInt(txtStaffID.Text))
  33.  
  34. ' Performs the cataloging operations for update, insert and delete statements
  35. command.ExecuteNonQuery()
  36.  
  37. ' Commits or saves transaction to the database
  38. myTransaction.Commit()
  39.  
  40. ' Closes the connection to the database
  41. conn.Close()
  42.  
  43. lblError.Text = "Your profile was successfully updated."
  44.  
  45.  
  46.  
  47. Catch ex As Exception
  48.  
  49.  
  50. lblError.Text = ex.Message
  51. myTransaction.Rollback()
  52.  
  53. End Try
  54.  
  55. End Sub
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
0
  #18
Oct 16th, 2009
Try taking out the conn.createcommand

  1. Dim command As OleDbCommand = new oledbcommand
  2. command.Connection = conn
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #19
Oct 16th, 2009
Access gives weird error messages if you type the wrong column name. Move your query over to the MS Access designer and see if you can run it with the substituted parameter values as a test. I'm almost certain you get a syntax error instead of a "column does not exist" error in updates with access.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
0
  #20
Oct 16th, 2009
Originally Posted by sknake View Post
Access gives weird error messages if you type the wrong column name. Move your query over to the MS Access designer and see if you can run it with the substituted parameter values as a test. I'm almost certain you get a syntax error instead of a "column does not exist" error in updates with access.
I'm kinda wondering about the error msg myself. Technically it shouldnt even know its an Update Statement, its not assigned to an update command of a data adapter.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1267 | Replies: 28
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC