| | |
update but not updated
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 34
Reputation:
Solved Threads: 0
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
please help me..i cannot see where my mistake
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)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strConnection As String = ConfigurationSettings.AppSettings("ConnectionString") Dim sqlConn As New SqlConnection(strConnection) Session("sRespondentID") = Request.QueryString("RespondentID") sqlConn.Open() If Session("sRespondentID") <> "" Then Dim sql As SqlCommand = New SqlCommand("SELECT * FROM Respondent WHERE RespondentID='" & Session("sRespondentID") & "'", sqlConn) Dim dr As SqlDataReader dr = sql.ExecuteReader While dr.Read txtName.Text = dr("Name").ToString txtEmail.Text = dr("Email").ToString End While dr.Close() End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strConnection As String = ConfigurationSettings.AppSettings("ConnectionString") Dim sqlConn As New SqlConnection(strConnection) sqlConn.Open() Dim cmd2 As String = "UPDATE Respondent SET Name='" + txtName.Text + "', Email='" + txtEmail.Text + "' WHERE RespondentID = '" & Session("sRespondentID") & "'" Dim MyCommand2 As SqlCommand = New SqlCommand(cmd2, sqlConn) If MyCommand2.ExecuteNonQuery Then Response.Redirect("maillist.aspx") Else lblMsg.Text = "Record cannot be updated at this moment." End If End Sub
please help me..i cannot see where my mistake
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
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?
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
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)
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
![]() |
Similar Threads
- Plz Help:If IE closed without signout how to update SQL to set user status (ASP.NET)
- Google Update (Search Engine Optimization)
- Refresh Form to Display changes (MySQL)
- Google Backlinks Updated 10/28 !! (Search Engine Optimization)
- This is what i get for using IE. (Viruses, Spyware and other Nasties)
- Windows Media Player messing up since updated (Windows NT / 2000 / XP)
- July PR update? (Search Engine Optimization)
- Annoying Pop-up ads!! (Web Browsers)
- Keep getting http:/// (Web Browsers)
Other Threads in the ASP.NET Forum
- Previous Thread: Asp
- Next Thread: Generate Html from VB.net
Views: 1054 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 activexcontrol advice ajax asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child class click compatible confirmationcodegeneration content contenttype countryselector courier css database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdown dropdownmenu edit feedback flash flv folder form forms google grid gridview homeedition hosting identity iframe iis index javascript jquery languages list maps menu mono mssql multistepregistration nameisnotdeclared object objects order problem profile ratings refer rotatepage save search security serializesmo.table session silverlight smartcard sql suse textbox tracking typeof unauthorized update validation vb vb.net video view virtualdirectory vista visual-studio visualstudio vs2008 web webarchitecture webdevelopemnt webdevelopment wizard xml





