Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project Users can edit their profile information which is already displayed in text boxes from the database and they can save changes.But the problem is though I edit the data in the textbox, while updating it takes only the older value in that textbox and I can't understand the reason. Can anyone know how to solve this?

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim con As New SqlConnection("Data Source=CSC32\SQLEXPRESS;Initial Catalog=Data1;Integrated Security=True;")
        Dim cmd As SqlCommand
        Dim name As String = Session("User")
        con.Open()
        cmd = New SqlCommand("SELECT * from Logs", con)
        Dim reader As Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
        Dim f As Integer = 0
        If (reader.HasRows()) Then
            While (reader.Read())
                If (name.CompareTo(reader("Username")) = 0) Then
                    f = 1
                    GoTo l1
                Else
                    f = 0
                End If
            End While
l1:         If f = 1 Then
                eidtext.Text = reader("Email")
                dob.Text = reader("DOB")
                sqtext.SelectedItem.Text = reader("Secques")
                satext.Text = reader("Secans")
            End If
        End If
        con.Close()
    End Sub




Protected Sub Savechanges_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Savechanges.Click
        Dim con As New SqlConnection("Data Source=CSC32\SQLEXPRESS;Initial Catalog=Data1;Integrated Security=True;")
        con.Open()
        Dim adp As New SqlDataAdapter
        Dim cmd As New SqlCommand("update Logs set Email='" + eidtext.Text + "', DOB='" + dob.Text + "', Secques='" + sqtext.Text + "',Secans='" + satext.Text + "' where Username='" + Session("User") + "';", con)
        adp.UpdateCommand = cmd
        adp.UpdateCommand.ExecuteNonQuery()
        con.Close()
    End Sub
Posted
Updated 16-Feb-15 3:02am
v2
Comments
ZurdoDev 16-Feb-15 8:43am    
No one can help see what you did wrong unless you post the relevant code. Click on Improve question and post the relevant code.

First of all, do not ever concatenate values directly to the SQL statement. This leaves you wide open to SQL injections. Instead use SqlParameter[^].

Then you don't need the adapter. Simply:
VB
Dim con As New SqlConnection("Data Source=CSC32\SQLEXPRESS;Initial Catalog=Data1;Integrated Security=True;")
con.Open()
Dim cmd As New SqlCommand("update Logs set Email=@email, DOB=@dob, Secques=@Secques,Secans=@Secans where Username=@username", con)
' SET PARAMETER VALUES OVER HERE
...
cmd.Parameters.AddWithValue("@Secques", sqtext.Text)
...

cmd.ExecuteNonQuery()
con.Close()


Also
- using debugger check that you really use the latest values when assigning the text into parameters.
- it's advisable to use using[^] statement for the connection
- and better yet wrap the DML operations inside a SqlTransaction[^]
 
Share this answer
 
v5
I can guess it untill you provide some code.
Check if you have assigned value in some variable while inserting data and the same variable is not assigned with updated value in case if editing data.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900