| | |
Problem updating Database
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
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:
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:
VB.NET Syntax (Toggle Plain Text)
Dim MyCmd As String Sub Page_Load(ByVal s As Object, ByVal e As EventArgs) If Not Page.IsPostBack Then End If ' Create our Variables Dim MyConn As OleDbConnection Dim MyDA As OleDbDataAdapter Dim DS As DataSet Dim dvwEmployees As DataView ' Set up the DataView cache dvwEmployees = Cache("Employee") If dvwEmployees Is Nothing Then 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;") MyDA = New OleDbDataAdapter("Select * From Employee Where Employee_ID = " & Session.Item("userName") & " ", MyConn) DS = New DataSet() MyDA.Fill(DS, "Employee") dvwEmployees = DS.Tables("Employee").DefaultView() Cache("Employee") = dvwEmployees End If ' Display the information using labels lblWelcomeFName.Text = dvwEmployees(0).Row("First_Name") lblWelcomeLName.Text = dvwEmployees(0).Row("Last_Name") txtStaffID.Text = dvwEmployees(0).Row("Employee_ID") txtFirstName.Text = dvwEmployees(0).Row("First_Name") txtLastName.Text = dvwEmployees(0).Row("Last_Name") txtPhone.Text = dvwEmployees(0).Row("Phone") txtEmail.Text = dvwEmployees(0).Row("E-mail") txtAddress.Text = dvwEmployees(0).Row("Address") txtAvailability.Text = dvwEmployees(0).Row("Availability") End Sub Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim myTransaction As System.Data.OleDb.OleDbTransaction Try ' Declares connection and variables and opens connection to database Dim conn As New OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=C:\Users\Christopher Carr\Documents\Visual Studio 2008\WebSites\MYTPSS new\tpsDatabase.accdb") conn.Open() Dim command As OleDbCommand = conn.CreateCommand() Dim strSQL As String ' Adds and New Transaction myTransaction = conn.BeginTransaction ' Sets what strSQL display value is strSQL = "Update [Employee] Set First_Name = " & txtFirstName.Text & ", Last_Name = " & txtLastName.Text & ", Phone = " & txtPhone.Text & ", E-mail = " & txtEmail.Text & _ ", Address = " & txtAddress.Text & ", Availability = " & txtAvailability.Text & " Where Employee_ID = " & txtStaffID.Text & "" ' Gets a value indicating how the CommandText property is to be interpreted and CommandText is set equal to the strSQL value command.CommandType = CommandType.Text command.CommandText = strSQL ' Starts the transaction command.Transaction = myTransaction ' Performs the cataloging operations for update, insert and delete statements command.ExecuteNonQuery() ' Commits or saves transaction to the database myTransaction.Commit() ' Closes the connection to the database conn.Close() lblError.Text = "Your profile was successfully updated." Catch ex As Exception lblError.Text = "Your profile was unable to be updated." End Try End Sub
Last edited by Kristofferson; Oct 13th, 2009 at 12:27 pm. Reason: Mistake in one of Data Sources
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Sep 2009
Posts: 321
Reputation:
Solved Threads: 45
-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.
•
•
Join Date: Oct 2009
Posts: 9
Reputation:
Solved Threads: 0
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.
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
•
•
Join Date: Sep 2009
Posts: 321
Reputation:
Solved Threads: 45
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)
VB.NET Syntax (Toggle Plain Text)
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()
Last edited by TomW; Oct 15th, 2009 at 9:00 pm.
![]() |
Similar Threads
- Database not being updated after DataAdapter.Update (C#)
- Problem with updating database after delete or edit (VB.NET)
- NEED HELP complicated problem with the design of a database (Database Design)
- I am New In MySQL. Problem With MySQL Database Plz Help. (MySQL)
- Updating database with related tables and queries?? (VB.NET)
- Error connecting to database (ASP.NET)
- Re: Problem in updating project for Microsoft Project 2002 and 2003 (Windows Software)
Other Threads in the VB.NET Forum
- Previous Thread: WM Browser
- Next Thread: cmd.executenonquery not accepting nz function
Views: 1268 | Replies: 28
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
"crystal .net .net2005 2008 access add application array assignment basic box button buttons center class click code combo convert cpu data database datagrid datagridview design designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees error excel exists firewall function image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net opacity page pan picturebox port print printing printpreview problem record refresh regex reports" reuse right-to-left save savedialog search serial socket sorting sql sqldatbase storedprocedure string structures studio temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet vista visual visualbasic visualbasic.net visualstudio2008 web wpf xml





