I am trying to get the data from the database into the text boxes and then update the data if something is changed in the database. I have successfully tried data binding with the text boxes and the data is displayed in the text boxes based on the session. The code was written under the Load Procedure:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Check if user is logged in

        If Session("username") > "" Then
            lblwelcomenote.Text = "You are logged in as " & " " & Session("username")
        End If

        'Data binding with textboxes based on session

        Dim conn As New MySqlConnection
        Dim comm As New MySqlCommand
        Dim dreader As MySqlDataReader

        conn.ConnectionString = ("server=localhost; database=medipoint; user id=root; password=ny2dr4;")

        comm.Connection = conn
        comm.CommandText = ("SELECT * FROM registeration WHERE username='" & Session("username") & "'")
        dreader = comm.ExecuteReader()


        txtfirstname.Text = dreader(1).ToString
        txtlastname.Text = dreader(2).ToString
        txtemail.Text = dreader(3).ToString
        txtpassword.Text = dreader(5).ToString


    End Sub

Then I tried to update the data in the database. For this the code is given below which was written in a seperate procedure as compared to the code given above. The code is:

    Protected Sub btnupdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnupdate.Click

        Dim sqlconnection As New MySqlConnection
        Dim sqlcommand As New MySqlCommand

        sqlconnection.ConnectionString = ("server=localhost; database=medipoint; user id=root; password=ny2dr4;")

        sqlcommand.Connection = sqlconnection
        sqlcommand.CommandText = ("UPDATE registeration SET firstname='" & txtfirstname.Text & "', lastname='" & txtlastname.Text & "', email='" & txtemail.Text & "', password='" & txtpassword.Text & "'")



    End Sub

The procedure updating the database does not work if I run it with the databinding procedure. If I comment the databinding procedure the update code runs correctly.

Can anyone please tell me how can I bind the data in text box and update the fields in database simultaneously?

Any help will be highly appreciated.


Bilal A. Khan

5 Years
Discussion Span
Last Post by BilalAKhan

move the code that sets your textbox values into its own method. Call that method from the page_load event but put it inside an "If IsPostBack" so that it's only called when the page initially loads and not on each postback after that. Also call that method at the end of your update button's click event.

Basically what is happening right now is that that the page load event is being called before the button's click event executes so it is refreshing the data in the textboxes to their original values and updating the data in the database with those values rather than the data that was entered into the textboxes but the user.

If you google "msdn ASP.NET page event life cycle" you should be able to find some more in depth information on how the page event life cycle works for asp.net.

Edited by nakor77: clarification

Votes + Comments
good response
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.