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.
Bilal A. Khan