Updating user in database

Posted by Jopito under VB.NET on 1/24/2014 | Points: 10 | Views : 1174 | Status : [Member] | Replies : 3
Hello pals,am tring to update my users table but cannot it cannot update.Can someone please tell me whats wrong with this code below.I cannot update

Dim con3 As New SqlConnection("Data Source=shah-PC\SQLEXPRESS;Initial Catalog=College;Integrated Security=True")
con3.Open()

Dim com3 As New SqlCommand("Update Registration set Firstname=@fnme,Lastname=@lnme,Age=@age,Password=@password,Campus=@campus,Telephone=@telephone from Registration where Username='" & Application("Username") & "'", con3)
com3.Parameters.AddWithValue("@fnme", Tfnme4.Text)
com3.Parameters.AddWithValue("@lnme", Tlnme4.Text)
com3.Parameters.AddWithValue("@age", Tage4.Text)
com3.Parameters.AddWithValue("@password", Tpassword4.Text)
com3.Parameters.AddWithValue("@campus", Tcampus4.Text)
com3.Parameters.AddWithValue("@telephone", Tphone4.Text)
If (com3.ExecuteNonQuery) Then

Labeluser.Text = "Profile Updated Succesfully"
Else
Labeluser.Text = "Profile Update Unsuccesfull"
End If
End Sub


Mark as answer if satisfied


Responses

Posted by: vishalneeraj-24503 on: 1/24/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You are doing wrong in update query
Just remove from clause and do not check executenonquery only returns numeric value,so check,

your query will be like below:
Dim com3 As New SqlCommand("Update Registration set Firstname=@fnme,Lastname=@lnme,Age=@age,Password=@password,Campus=@campus,Telephone=@telephone where Username='" & Application("Username") & "'", con3)


com3.Parameters.AddWithValue("@fnme", Tfnme4.Text)

com3.Parameters.AddWithValue("@lnme", Tlnme4.Text)

com3.Parameters.AddWithValue("@age", Tage4.Text)

com3.Parameters.AddWithValue("@password", Tpassword4.Text)

com3.Parameters.AddWithValue("@campus", Tcampus4.Text)

com3.Parameters.AddWithValue("@telephone", Tphone4.Text)

If (com3.ExecuteNonQuery>0) Then



Labeluser.Text = "Profile Updated Succesfully"

Else

Labeluser.Text = "Profile Update Unsuccesfull"

End If



Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Learningtorise on: 1/24/2014 [Member] Starter | Points: 25

Up
0
Down
Vishal is right about using ExecuteNonQuery in IF condition.

ExecuteNonQuery returns the number of rows affected - if it's 0, that means there were no matching rows to update. Also use try catch statement in your function to check sql exception.

http://hashtagakash.wordpress.com/

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Snaveen on: 2/13/2014 [Member] Starter | Points: 25

Up
0
Down
Did you get any error..?
Better to put a breakpoint and check line by line to resovle the issue..

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response