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

Problem updating Database

 
1
  #1
Oct 13th, 2009
Alright, so my issue is that I have a staff profile. When you press the button it brings you to the Edit Profile page with text boxes with the original information in them so they can be edited. Now I ask for the edited information to be updated when the update button is clicked but it won't update. I know that I need to call new versions of the information from the text boxes but I'm not exactly sure how to go about this. I have use the "Request.Item" elsewhere in my code but you usually have to define a "Server.MapPath" which I'm not sure I can do in the confines of the update statement. Any help, hints, or ideas would be appreciated.

Also this is my first thread so if I do something wrong with the code tags please forgive me, I will learn quickly.

VB Code for the entire Staff Member Edit Profile Page below:

  1. Dim MyCmd As String
  2.  
  3. Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
  4.  
  5. If Not Page.IsPostBack Then
  6.  
  7. End If
  8.  
  9. ' Create our Variables
  10.  
  11. Dim MyConn As OleDbConnection
  12. Dim MyDA As OleDbDataAdapter
  13.  
  14. Dim DS As DataSet
  15. Dim dvwEmployees As DataView
  16.  
  17. ' Set up the DataView cache
  18.  
  19. dvwEmployees = Cache("Employee")
  20. If dvwEmployees Is Nothing Then
  21. MyConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Christopher Carr\Documents\Visual Studio 2008\WebSites\MYTPSS new\tpsDatabase.accdb; Persist Security Info=False;")
  22. MyDA = New OleDbDataAdapter("Select * From Employee Where Employee_ID = " & Session.Item("userName") & " ", MyConn)
  23.  
  24. DS = New DataSet()
  25. MyDA.Fill(DS, "Employee")
  26.  
  27. dvwEmployees = DS.Tables("Employee").DefaultView()
  28. Cache("Employee") = dvwEmployees
  29. End If
  30.  
  31. ' Display the information using labels
  32.  
  33. lblWelcomeFName.Text = dvwEmployees(0).Row("First_Name")
  34. lblWelcomeLName.Text = dvwEmployees(0).Row("Last_Name")
  35. txtStaffID.Text = dvwEmployees(0).Row("Employee_ID")
  36. txtFirstName.Text = dvwEmployees(0).Row("First_Name")
  37. txtLastName.Text = dvwEmployees(0).Row("Last_Name")
  38. txtPhone.Text = dvwEmployees(0).Row("Phone")
  39. txtEmail.Text = dvwEmployees(0).Row("E-mail")
  40. txtAddress.Text = dvwEmployees(0).Row("Address")
  41. txtAvailability.Text = dvwEmployees(0).Row("Availability")
  42.  
  43. End Sub
  44.  
  45. Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  46. Dim myTransaction As System.Data.OleDb.OleDbTransaction
  47.  
  48. Try
  49.  
  50. ' Declares connection and variables and opens connection to database
  51. Dim conn As New OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" & _
  52. "Data Source=C:\Users\Christopher Carr\Documents\Visual Studio 2008\WebSites\MYTPSS new\tpsDatabase.accdb")
  53. conn.Open()
  54. Dim command As OleDbCommand = conn.CreateCommand()
  55. Dim strSQL As String
  56.  
  57.  
  58. ' Adds and New Transaction
  59. myTransaction = conn.BeginTransaction
  60.  
  61. ' Sets what strSQL display value is
  62. strSQL = "Update [Employee] Set First_Name = " & txtFirstName.Text & ", Last_Name = " & txtLastName.Text & ", Phone = " & txtPhone.Text & ", E-mail = " & txtEmail.Text & _
  63. ", Address = " & txtAddress.Text & ", Availability = " & txtAvailability.Text & " Where Employee_ID = " & txtStaffID.Text & ""
  64.  
  65. ' Gets a value indicating how the CommandText property is to be interpreted and CommandText is set equal to the strSQL value
  66. command.CommandType = CommandType.Text
  67. command.CommandText = strSQL
  68.  
  69. ' Starts the transaction
  70. command.Transaction = myTransaction
  71.  
  72. ' Performs the cataloging operations for update, insert and delete statements
  73. command.ExecuteNonQuery()
  74.  
  75. ' Commits or saves transaction to the database
  76. myTransaction.Commit()
  77.  
  78. ' Closes the connection to the database
  79. conn.Close()
  80.  
  81. lblError.Text = "Your profile was successfully updated."
  82.  
  83.  
  84. Catch ex As Exception
  85.  
  86.  
  87. lblError.Text = "Your profile was unable to be updated."
  88.  
  89. End Try
  90.  
  91. End Sub
Last edited by Kristofferson; Oct 13th, 2009 at 12:27 pm. Reason: Mistake in one of Data Sources
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 271
Reputation: GeekByChoiCe is on a distinguished road 
Solved Threads: 56
GeekByChoiCe GeekByChoiCe is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 14th, 2009
do you get an error or its just not updating?
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
  #3
Oct 14th, 2009
It just not updating no error. The label message in the Catch ex As Exception comes up.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 271
Reputation: GeekByChoiCe is on a distinguished road 
Solved Threads: 56
GeekByChoiCe GeekByChoiCe is offline Offline
Posting Whiz in Training
 
0
  #4
Oct 15th, 2009
change the
lblError.Text = "Your profile was unable to be updated."
to
lblError.Text = ex.message

then you see the error.
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
  #5
Oct 15th, 2009
It comes up with "Syntax error in UPDATE statement." And when I put a breakpoint on my Update statement all the values aren't the new values but the old ones. I keep looking at the update statement but I don't see anything wrong. Of course I have inexperienced eyes, only been coding a total of like 16 to 20 weeks over the past year.
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
 
-1
  #6
Oct 15th, 2009
Each of the string/text field values should be surrounded by single quotes in your update statement. However I would suggest changing this concatenated string to use parameters instead;. it will take care of not having to surrond the values in quotes and the database wont have to parse your statement multiple times times to format it for missing parameters.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 143
Reputation: babbu has a little shameless behaviour in the past 
Solved Threads: 15
babbu babbu is offline Offline
Junior Poster
 
-1
  #7
Oct 15th, 2009
i think there are extra inverted commas at the end of the update statement
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
 
-1
  #8
Oct 15th, 2009
Comma's look fine although the double set of quotes at the end of the where clause is not needed (it shouldnt hurt anything). Also in your catch block you should include a myTransaction.Rollback
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
  #9
Oct 15th, 2009
Alright I added the myTransaction.Rollback which was giving me errors but works now for some reason.

I have not used parameters at all before, so can you point me to a good resource to learn how to setup and use parameters. I have already attempted to create my parameters which seems straight forward but how do I actually implement them in the Update Statement?

I haven't said it yet but thanks for the help so far.
Last edited by Kristofferson; Oct 15th, 2009 at 2:01 pm. Reason: misspelling
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
 
1
  #10
Oct 15th, 2009
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)

  1. strSQL = "Update [Employee] Set First_Name = @FName And Last_Name = @LName Where Employee_ID = @Id"
  2.  
  3. command.Transaction = myTransaction
  4. command.CommandType = CommandType.Text
  5. command.CommandText = strSQL
  6.  
  7. Command.Parameters.AddWithValue("@FName", txtFName.Text)
  8. Command.Parameters.AddWithValue("@LName", txtLName.Text)
  9. Command.Parameters.AddWithValue("@Id", cint(txtStaffId.Text))
  10.  
  11. command.ExecuteNonQuery()
Last edited by TomW; Oct 15th, 2009 at 9:00 pm.
Reply With Quote Quick reply to this message  
Reply

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




Views: 1268 | 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