update but not updated

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

Join Date: Mar 2007
Posts: 34
Reputation: shy_wani is an unknown quantity at this point 
Solved Threads: 0
shy_wani shy_wani is offline Offline
Light Poster

update but not updated

 
0
  #1
May 2nd, 2007
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

  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: update but not updated

 
0
  #2
May 2nd, 2007
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?
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 34
Reputation: shy_wani is an unknown quantity at this point 
Solved Threads: 0
shy_wani shy_wani is offline Offline
Light Poster

Re: update but not updated

 
0
  #3
May 2nd, 2007
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
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the ASP.NET Forum


Views: 1054 | Replies: 2
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC