944,082 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1227
  • ASP.NET RSS
May 2nd, 2007
0

update but not updated

Expand Post »
i have a problem.. i just can't see where i did go wrong..
when my page loads, i want the name and email appear in the textbox so that user can edit their name and email...but then, it doesn't update the record, but continue to save the old value of name and email appear in the textbox when page load

ASP.NET Syntax (Toggle Plain Text)
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. Dim strConnection As String = ConfigurationSettings.AppSettings("ConnectionString")
  3. Dim sqlConn As New SqlConnection(strConnection)
  4. Session("sRespondentID") = Request.QueryString("RespondentID")
  5. sqlConn.Open()
  6. If Session("sRespondentID") <> "" Then
  7. Dim sql As SqlCommand = New SqlCommand("SELECT * FROM Respondent WHERE RespondentID='" & Session("sRespondentID") & "'", sqlConn)
  8. Dim dr As SqlDataReader
  9. dr = sql.ExecuteReader
  10. While dr.Read
  11. txtName.Text = dr("Name").ToString
  12. txtEmail.Text = dr("Email").ToString
  13. End While
  14. dr.Close()
  15. End If
  16. End Sub
  17.  
  18. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  19. Dim strConnection As String = ConfigurationSettings.AppSettings("ConnectionString")
  20. Dim sqlConn As New SqlConnection(strConnection)
  21. sqlConn.Open()
  22.  
  23. Dim cmd2 As String = "UPDATE Respondent SET Name='" + txtName.Text + "', Email='" + txtEmail.Text + "' WHERE RespondentID = '" & Session("sRespondentID") & "'"
  24. Dim MyCommand2 As SqlCommand = New SqlCommand(cmd2, sqlConn)
  25. If MyCommand2.ExecuteNonQuery Then
  26. Response.Redirect("maillist.aspx")
  27. Else
  28. lblMsg.Text = "Record cannot be updated at this moment."
  29. End If
  30.  
  31. End Sub

please help me..i cannot see where my mistake
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
shy_wani is offline Offline
34 posts
since Mar 2007
May 2nd, 2007
0

Re: update but not updated

ahhhhhhh the oldest mistake with webpages that everyone forgets about.

This is web pages NOT windows forms so you have to remember the difference it is VITAL!

See your event handler at the top?
ASP.NET Syntax (Toggle Plain Text)
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

It fires every time the page is loaded. This is NOT the same as a windows form page load event which fires once. Every postback also has to reload a web page and hence the page load event fires and your code fills the textbox again from the database. THEN it fires the click event and saves the old data back.

So the way around it is to only run your code when it is a fresh load and not a postback. How do you know when this is? Ask the Page. you need to wrap your code with an If NOT IsPostback ... End If wrapper.

Lots of people miss it and think of windows forms not web pages.

Also be aware of the opposite - sometimes you need to fire some code on every page load including postbacks so make sure you put that outside of the if end if block. I have seen them spend hours looking for the problem - myself included
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
May 2nd, 2007
0

Re: update but not updated

OMG...
i'm totally forgot about the not ispostback..
thank you f1 fan for your help..

i believe that sometimes, we can't really see what we are actually doing...haha...

anyway, thanks..thank u very much
Reputation Points: 10
Solved Threads: 0
Light Poster
shy_wani is offline Offline
34 posts
since Mar 2007

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: Asp
Next Thread in ASP.NET Forum Timeline: Generate Html from VB.net





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


Follow us on Twitter


© 2011 DaniWeb® LLC