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

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

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?

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 :)

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
:D

This article has been dead for over six months. Start a new discussion instead.