Hi,

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")
        Else
            Response.Redirect("Default.aspx")
        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;")
        conn.Open()

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

        dreader.Read()

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

        dreader.Close()
        conn.Close()


    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;")

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

        sqlcommand.ExecuteNonQuery()

        sqlconnection.Close()

    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.

Regards,

Bilal A. Khan

Recommended Answers

All 2 Replies

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.

commented: good response +6

Thanx a lot, it really helped me a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.