Problem updating Database

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

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: 502
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
0
  #21
Oct 17th, 2009
Change SQL Text,
  1. sql="Update Employee Set First_Name = @FirstName, Last_Name = @LastName, Phone = @Phone, [E-mail] = @Email, Address = @Address, Availability = @Availability Where Employee_ID = @EmployeeID"
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
  #22
Oct 17th, 2009
adatapost.... That seemed to get it to run the code without an exception error, so it displays "Your profile was successfully updated!" The problem is that it is not updating the values in the database.
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
  #23
Oct 17th, 2009
ahhh keyword, good catch...

As far as the missing data, make sure your not using two different versions of access, one that is your original and another that is just a copy of the original in your project.
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
  #24
Oct 18th, 2009
I just wanted to say the way you guys told me to setup the UPDATE statement with the parameters was correct the problem was is that I wasn't requesting the new values from the text box so I did so and it works now.

Thank you so much for all the help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: musaprodhan is an unknown quantity at this point 
Solved Threads: 0
musaprodhan musaprodhan is offline Offline
Newbie Poster
 
0
  #25
Oct 18th, 2009
Originally Posted by Kristofferson View Post
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 can not edit DataGridView1. How can i Edit DataGridView1.plz send me now.
Thanks
Musa Prodhan
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: cbadchris is an unknown quantity at this point 
Solved Threads: 0
cbadchris cbadchris is offline Offline
Newbie Poster
 
0
  #26
Oct 20th, 2009
This code does not produce errors but it does not execute any update.

  1. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim con As New SqlConnection
  3. Dim cmd As New SqlCommand
  4. con.ConnectionString = "Data Source=XXXXXX;Initial Catalog=XXXXX;Persist Security Info=True;User ID=XXXX;Password=XXXX"
  5. con.Open()
  6. cmd.Connection = con
  7. cmd.CommandText = "Update services set service_name = @sName, cost = @sCost, service_desc = @sDesc, cat_rel = @sCatRel Where service_ID = @sID"
  8. 'Response.Write(cmd.CommandText)
  9. cmd.Parameters.Add(New SqlParameter("@sName", SqlDbType.VarChar, 50))
  10. cmd.Parameters("@sName").Value = txtSername.Text
  11. cmd.Parameters.Add(New SqlParameter("@sCost", SqlDbType.VarChar, 50))
  12. cmd.Parameters("@sCost").Value = txtCost.Text
  13. cmd.Parameters.Add(New SqlParameter("@sDesc", SqlDbType.Text))
  14. cmd.Parameters("@sDesc").Value = ServDescr.Value
  15. cmd.Parameters.Add(New SqlParameter("@sCatRel", SqlDbType.Int))
  16. cmd.Parameters("@sCatRel").Value = CatRel.SelectedValue
  17. cmd.Parameters.Add(New SqlParameter("@sID", SqlDbType.Int))
  18. cmd.Parameters("@sID").Value = Request.QueryString("id")
  19. cmd.ExecuteNonQuery()
  20. Updated.Text = "Your service data was updated successfully!"
  21.  
  22. End Sub

Any ideas?

Thanks
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
  #27
Oct 21st, 2009
Originally Posted by cbadchris View Post
This code does not produce errors but it does not execute any update.
Chris are you checking the actual database or just your in-memory dataset. Since the coding is directly updating the database with values from textboxes rather then first adding a new datarow to the dataset/datatable, you will not see the changes in the dataset/datatable until the next time you perform a fill operation.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: cbadchris is an unknown quantity at this point 
Solved Threads: 0
cbadchris cbadchris is offline Offline
Newbie Poster

THis is the code in it's entirety

 
0
  #28
Oct 21st, 2009
So below I am populating the form with the original data on the load
event then updating the database and the form is reloading on postback.

I just thought to myself that maybe the problem is with the load event. Maybe I am updating the DB with the original data and that is why I am not getting errors yet noithing is being updated?

  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3.  
  4. Partial Class admin_service_edit
  5. Inherits System.Web.UI.Page
  6. 'write database data to form on initial load
  7. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  8. Dim con As String = "Data Source=xxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx"
  9. Dim cmd As New SqlCommand
  10. Dim pId As Integer
  11. pId = Request.QueryString("id")
  12. cmd.CommandText = "Select * from services where service_ID = '" & pId & "'"
  13. cmd.Connection = New SqlConnection(con)
  14. cmd.Connection.Open()
  15. Dim dr As SqlDataReader = cmd.ExecuteReader()
  16. While dr.Read()
  17. txtSername.Text = dr("service_name")
  18. txtCost.Text = dr("cost")
  19. ServDescr.Value = dr("service_desc")
  20. CatRel.SelectedValue = dr("cat_rel")
  21. End While
  22. cmd.Connection.Close()
  23. End Sub
  24. 'on button click update the database record
  25. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  26. Dim mycon As New SqlConnection
  27. Dim mycommand As New SqlCommand
  28. mycon.ConnectionString = "Data Source=xxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Passwordxxx"
  29. mycon.Open()
  30. mycommand.Connection = mycon
  31. Try
  32. mycommand.CommandText = "Update services set service_name = @sName, cost = @sCost, service_desc = @sDesc, cat_rel = @sCatRel Where service_ID = @sID"
  33. 'Response.Write(mycommand.CommandText)
  34. mycommand.Parameters.AddWithValue("@sName", txtSername.Text)
  35. mycommand.Parameters.AddWithValue("@sCost", txtCost.Text)
  36. mycommand.Parameters.AddWithValue("@sDesc", ServDescr.Value)
  37. mycommand.Parameters.AddWithValue("@sCatRel", CatRel.SelectedValue)
  38. mycommand.Parameters.AddWithValue("@sID", Request.QueryString("id"))
  39. 'mycommand.Parameters.Add(New SqlParameter("@sName", SqlDbType.VarChar, 50))
  40. 'mycommand.Parameters("@sName").Value = txtSername.Text
  41. 'mycommand.Parameters.Add(New SqlParameter("@sCost", SqlDbType.VarChar, 50))
  42. 'mycommand.Parameters("@sCost").Value = txtCost.Text
  43. 'mycommand.Parameters.Add(New SqlParameter("@sDesc", SqlDbType.Text))
  44. 'mycommand.Parameters("@sDesc").Value = ServDescr.Value
  45. 'mycommand.Parameters.Add(New SqlParameter("@sCatRel", SqlDbType.Int))
  46. 'mycommand.Parameters("@sCatRel").Value = CatRel.SelectedValue
  47. 'mycommand.Parameters.Add(New SqlParameter("@sID", SqlDbType.Int))
  48. 'mycommand.Parameters("@sID").Value = Request.QueryString("id")
  49. mycommand.ExecuteNonQuery()
  50. Updated.Text = "Your service data was updated successfully!"
  51. Catch ex As Exception
  52. Updated.Text = ex.Message
  53. MsgBox(ex.Message)
  54. End Try
  55. mycommand.Connection.Close()
  56. End Sub
  57. End Class
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
  #29
Oct 21st, 2009
Again I would ask are you sure the record is not being updated in the actual database and your just not seeing it on the front end? I dont see where its doing a refill of the new data except in the page load. Also have you stepped thru the code to see the exact values that are in the parameter variables at the time it is executed; it might be something unexpected?

I would change your fill in the page load, to fill a form/global scope level dataset rather then using a reader to store the values in your controls. Then in your update button, I would make the update to the existing record in your dataset. Then call the update method of the dataadapter to update the database on the entire dataset.
Reply With Quote Quick reply to this message  
Reply

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




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