Update record Problem.

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 38
Reputation: preethi_ga is an unknown quantity at this point 
Solved Threads: 0
preethi_ga preethi_ga is offline Offline
Light Poster

Update record Problem.

 
0
  #1
Dec 16th, 2008
Hi Everyone,
Im using ASP.NET and SQL Server. I have Insert , delete data working well. but if i update a particular record it just all the records are updated with the same value.

for example if i have 3 records with 3 different values, if i display the second record, and edit it, and update it. then all the 3 values are updated with the same value what i have entered. i donno what is the error in my code. can anyone enlighten me?

here is the code,

  1. Imports System
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Imports System.Data.OleDb
  5.  
  6. Partial Class Default7
  7. Inherits System.Web.UI.Page
  8. Dim con As SqlConnection
  9. Dim cmd As SqlCommand
  10. Dim cmb As SqlCommandBuilder
  11. Dim adp As SqlDataAdapter
  12. Dim data As DataSet3
  13. Dim dr As DataRow
  14. Dim selct, uptext As String
  15.  
  16.  
  17. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  18. con = New SqlConnection("Data Source=ACER;Initial Catalog=preethi;Integrated Security=True")
  19. con.Open()
  20. End Sub
  21.  
  22. Private Sub fillcontrols()
  23. TextBox1.Text = data.Tables(0).Rows(0).Item(0)
  24. TextBox2.Text = data.Tables(0).Rows(0).Item(1)
  25. TextBox3.Text = data.Tables(0).Rows(0).Item(2)
  26. End Sub
  27.  
  28. Protected Sub Update_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  29. Try
  30. uptext = "update cooldrinks set drinkname = @drinkname ,color = @color, price = @price"
  31. cmd = New SqlCommand(uptext, con)
  32. cmd.Parameters.AddWithValue("@drinkname", TextBox1.Text)
  33. cmd.Parameters.AddWithValue("@color", TextBox2.Text)
  34. cmd.Parameters.AddWithValue("@price", TextBox3.Text)
  35. cmd.ExecuteNonQuery()
  36. Response.Write("Data Updated Successfully")
  37.  
  38. Catch ex As Exception
  39. Response.Write(ex.Message)
  40. End Try
  41.  
  42.  
  43. End Sub
  44.  
  45. Protected Sub AddNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  46. clearcontrols()
  47. End Sub
  48. Private Sub clearcontrols()
  49. TextBox1.Text = " "
  50. TextBox2.Text = " "
  51. TextBox3.Text = " "
  52. TextBox1.Focus()
  53.  
  54. End Sub
  55.  
  56. Protected Sub Insert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
  57. Dim Ssql As String
  58. Try
  59. Ssql = "insert into cooldrinks(drinkname,color,price)values(@drinkname,@color,@price)"
  60. cmd = New SqlCommand(Ssql, con)
  61. cmd.Parameters.AddWithValue("@drinkname", TextBox1.Text)
  62. cmd.Parameters.AddWithValue("@color", TextBox2.Text)
  63. cmd.Parameters.AddWithValue("@price", TextBox3.Text)
  64. cmd.ExecuteNonQuery()
  65. clearcontrols()
  66.  
  67. Catch ex As Exception
  68. Response.Write(ex.Message)
  69. End Try
  70. Response.Write("One Record inserted Successfully")
  71. End Sub
  72.  
  73. Protected Sub Delete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
  74. Dim Ssql As String
  75. Ssql = "delete from cooldrinks where price = @price "
  76. cmd = New SqlCommand(Ssql, con)
  77. cmd.Parameters.AddWithValue("@price", TextBox3.Text)
  78. cmd.ExecuteNonQuery()
  79. Response.Write("Deleted")
  80. End Sub
  81.  
  82. Protected Sub LoadTable_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
  83. Try
  84.  
  85. selct = "select * from cooldrinks"
  86. adp = New SqlDataAdapter(selct, con)
  87. data = New DataSet3()
  88. adp.Fill(data, "cooldrinks")
  89. fillcontrols()
  90.  
  91. Catch ex As Exception
  92. Response.Write(ex.Message)
  93. End Try
  94. End Sub
  95. End Class
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 645
Reputation: binoj_daniel is an unknown quantity at this point 
Solved Threads: 17
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: Update record Problem.

 
0
  #2
Dec 16th, 2008
It is so obvious that you dont have any where condition in the update statement.

uptext = "update cooldrinks set drinkname = @drinkname ,color = @color, price = @price" --> You need to add the where clause here.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 38
Reputation: preethi_ga is an unknown quantity at this point 
Solved Threads: 0
preethi_ga preethi_ga is offline Offline
Light Poster

Re: Update record Problem.

 
0
  #3
Dec 17th, 2008
hi,
i gave Update Sql Command, but its not working.

this is the code i have given.....

  1. uptext = "update cooldrinks set drinkname = @drinkname ,color = @color, price = @price where price = " & TextBox3.Text & " "
Last edited by peter_budo; Dec 17th, 2008 at 8:12 pm. Reason: Use [code] tag not [tex]
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 645
Reputation: binoj_daniel is an unknown quantity at this point 
Solved Threads: 17
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: Update record Problem.

 
0
  #4
Dec 17th, 2008
Wrap you code between tags to keep the formatting. Post you existing query again.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 38
Reputation: preethi_ga is an unknown quantity at this point 
Solved Threads: 0
preethi_ga preethi_ga is offline Offline
Light Poster

Re: Update record Problem.

 
0
  #5
Dec 17th, 2008
hi,
i gave Update Sql Command, but its not working.

this is the code i have given.....


  1. uptext = "update cooldrinks set drinkname = @drinkname ,color = @color, price = @price where price = " & TextBox3.Text & " "
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 645
Reputation: binoj_daniel is an unknown quantity at this point 
Solved Threads: 17
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: Update record Problem.

 
0
  #6
Dec 17th, 2008
Originally Posted by preethi_ga View Post
hi,
i gave Update Sql Command, but its not working.

this is the code i have given.....


  1. uptext = "update cooldrinks set drinkname = @drinkname ,color = @color, price = @price where price = " & TextBox3.Text & " "
You need to check your records to see the prices are same for all records.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC