943,718 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1092
  • ASP.NET RSS
Dec 16th, 2008
0

Update record Problem.

Expand Post »
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,

ASP.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
preethi_ga is offline Offline
38 posts
since Jun 2008
Dec 16th, 2008
0

Re: Update record Problem.

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.
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Dec 17th, 2008
0

Re: Update record Problem.

hi,
i gave Update Sql Command, but its not working.

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

ASP.NET Syntax (Toggle Plain Text)
  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]
Reputation Points: 10
Solved Threads: 0
Light Poster
preethi_ga is offline Offline
38 posts
since Jun 2008
Dec 17th, 2008
0

Re: Update record Problem.

Wrap you code between tags to keep the formatting. Post you existing query again.
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Dec 17th, 2008
0

Re: Update record Problem.

hi,
i gave Update Sql Command, but its not working.

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


ASP.NET Syntax (Toggle Plain Text)
  1. uptext = "update cooldrinks set drinkname = @drinkname ,color = @color, price = @price where price = " & TextBox3.Text & " "
Reputation Points: 10
Solved Threads: 0
Light Poster
preethi_ga is offline Offline
38 posts
since Jun 2008
Dec 17th, 2008
0

Re: Update record Problem.

Click to Expand / Collapse  Quote originally posted by preethi_ga ...
hi,
i gave Update Sql Command, but its not working.

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


ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: How to create Check Box Controls list in Repeater/Datalist/Listview ??
Next Thread in ASP.NET Forum Timeline: IIS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC