ASP.Net VB Page to update a users profile not updating.

Reply

Join Date: Jun 2005
Posts: 4
Reputation: jturlington is an unknown quantity at this point 
Solved Threads: 0
jturlington jturlington is offline Offline
Newbie Poster

ASP.Net VB Page to update a users profile not updating.

 
0
  #1
Jul 7th, 2005
My page displays the info in the text boxes, but when we run the click sub for the update button it returns the info that it has been updated sucessfully. Here is the page code and below that is the stored procedure it uses, but not update in the database.
  1. <%@ Page LANGUAGE="VB" MasterPageFile="~/MasterPage.master" Title="The Car Masseuse - Profile Update" %>
  2. <%@Import Namespace="System.Web.Security" %>
  3. <%@ Import Namespace="System.Data.SqlClient" %>
  4. <%@ Import Namespace="System.Data" %>
  5. <script runat="server">
  6. Protected Sub Page_Load(ByVal sender AS Object, ByVal e AS System.EventArgs)
  7. Dim myConnection AS New SqlConnection("server=localhost;uid=###;pwd=###;database=###;")
  8. Dim myCommand AS New SqlDataAdapter("select * from users where username='" & Request.Cookies("username").Value & "'", myConnection)
  9. Dim ds AS New DataSet()
  10. myCommand.Fill(ds, "users")
  11. lblID.Text = ds.TABLES(0).Rows(0)("id").ToString()
  12. txtName.Text = ds.TABLES(0).Rows(0)("name").ToString()
  13. txtAdd.Text = ds.TABLES(0).Rows(0)("address").ToString()
  14. txtCity.Text = ds.TABLES(0).Rows(0)("City").ToString()
  15. txtSt.Text = ds.TABLES(0).Rows(0)("st").ToString()
  16. txtZIP.Text = ds.TABLES(0).Rows(0)("zip").ToString()
  17. txtPhone.Text = ds.TABLES(0).Rows(0)("phone").ToString()
  18. txtEmail.Text = ds.TABLES(0).Rows(0)("email").ToString()
  19. End Sub
  20. Protected Sub butUpdate_Click(ByVal sender AS Object, ByVal e AS System.EventArgs)
  21. Dim sqlConn AS SqlConnection = New SqlConnection("server=localhost;uid=###;pwd=###;database=###;")
  22. Dim sqlSel AS SqlCommand
  23. sqlSel = New SqlCommand
  24. sqlSel.Connection = sqlConn
  25. sqlSel.CommandText = "userUpdate"
  26. sqlSel.CommandType = DATA.CommandType.StoredProcedure
  27. sqlSel.Parameters.AddWithValue("@id", lblID.Text)
  28. sqlSel.Parameters.AddWithValue("@name", txtName.Text)
  29. sqlSel.Parameters.AddWithValue("@address", txtAdd.Text)
  30. sqlSel.Parameters.AddWithValue("@city", txtCity.Text)
  31. sqlSel.Parameters.AddWithValue("@st", txtSt.Text)
  32. sqlSel.Parameters.AddWithValue("@zip", txtZIP.Text)
  33. sqlSel.Parameters.AddWithValue("@phone", txtPhone.Text)
  34. sqlSel.Parameters.AddWithValue("@email", txtEmail.Text)
  35. Dim selParm AS SqlParameter = sqlSel.Parameters.ADD("ReturnValue", SqlDbType.Int, 1)
  36. selParm.Direction = ParameterDirection.ReturnValue
  37. sqlConn.Open()
  38. sqlSel.ExecuteNonQuery()
  39. IF selParm.Value = 0 Then
  40. lblstatus.Text = "Update Sucessful!"
  41. Panel1.Visible = False
  42. End IF
  43. sqlConn.Close()
  44. End Sub
  45. </script>
  46. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  47. <table align="center" width="545px"><tr><td align="right" class="texttitle1">Update Profile</td></tr><tr><td><hr color="#F53167" /></td></tr>
  48. <tr><td>
  49. <asp:Label ID="lblstatus" runat="server">Here you can UPDATE your contact information.</asp:Label>
  50. <asp:Panel ID="Panel1" runat="server">
  51.  
  52. <asp:Label runat="server" ID="lblUserId">User ID #:</asp:Label><asp:Label runat="server" ID="lblID"></asp:Label><br />
  53. <asp:Label runat="server" ID="lblName">Name: </asp:Label><asp:TextBox ID="txtName" runat="server" cssclass="text"></asp:TextBox><br />
  54. <asp:Label runat="server" ID="lblAddress">Address: </asp:Label><asp:TextBox ID="txtAdd" runat="server" cssclass="text"></asp:TextBox><br />
  55. <asp:Label runat="server" ID="lblCity">City: </asp:Label><asp:TextBox ID="txtCity" runat="server" cssclass="text"></asp:TextBox><br />
  56. <asp:Label runat="server" ID="lblState">State: </asp:Label><asp:TextBox ID="txtSt" runat="server" cssclass="text"></asp:TextBox><br />
  57. <asp:Label runat="server" ID="lblZip">Zip Code: </asp:Label><asp:TextBox ID="txtZIP" runat="server" cssclass="text"></asp:TextBox><br />
  58. <asp:Label runat="server" ID="lblPhone">Phone Number: </asp:Label><asp:TextBox ID="txtPhone" runat="server" cssclass="text"></asp:TextBox><br />
  59. <asp:Label runat="server" ID="lblEmail">Email: </asp:Label><asp:TextBox ID="txtEmail" runat="server" cssclass="text"></asp:TextBox><br />
  60. <asp:Button ID="butUpdate" runat="server" Text="Update Profile" OnClick="butUpdate_Click" /></asp:Panel></td></tr></table>
  61. </asp:Content>

  1. CREATE PROCEDURE userUpdate
  2. (
  3. @id bigint,
  4. @name varchar(50),
  5. @address varchar(50),
  6. @city varchar(50),
  7. @st varchar(2),
  8. @zip varchar(5),
  9. @phone varchar(10),
  10. @email varchar(75)
  11. )
  12. AS
  13. UPDATE users SET [name]=@name,address=@address,city=@city,st=@st,zip=@zip,phone=@phone,email=@email WHERE [id] = @id
  14. RETURN (0)
  15. GO

Any help would be greatly appreciated.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: ASP.Net VB Page to update a users profile not updating.

 
0
  #2
Jul 8th, 2005
Ok first off, have you tested your Stored Procedure?

Here is what it should look like :

CREATE  PROCEDURE userUpdate
(
  @id bigint,
  @name varchar(50),
  @address varchar(50),
  @city varchar(50),
  @st varchar(2),
  @zip varchar(5),
  @phone varchar(10),
  @email varchar(75)
)
AS
	UPDATE users 
	SET name=@name,address=@address,city=@city,st=@st,zip=@zip,phone=@phone,email=@email 
	WHERE id = @id
Return 

And with your ASP.Net Codebehind I found some issues:

1. No Input direction set for your parameters going into the stored procedure.
2. No parameter created to store those values in the parameters collection.
3. AddWithValue is not a member of the parameters collection

4. You are requesting a return value called ReturnValue which you haven't declared in your Stored Procedure.

Here is what it should be like:

 Protected Sub butUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
		Dim sqlConn As SqlConnection = New SqlConnection("server=localhost;uid=###;pwd=###;database=###;")
		Dim sqlSel As New SqlCommand
		sqlSel.Connection = sqlConn
		sqlSel.CommandText = "userUpdate"
		sqlSel.CommandType = Data.CommandType.StoredProcedure

		Dim objParam1, objParam2, objParam3, objParam4, objParam5, objParam6, objParam7, objParam8 As SqlParameter
		objParam1 = sqlSel.Parameters.Add("@id", lblID.Text)
		objParam2 = sqlSel.Parameters.Add("@name", txtName.Text)
		objParam3 = sqlSel.Parameters.Add("@address", txtAdd.Text)
		objParam4 = sqlSel.Parameters.Add("@city", txtCity.Text)
		objParam5 = sqlSel.Parameters.Add("@st", txtSt.Text)
		objParam6 = sqlSel.Parameters.Add("@zip", txtZIP.Text)
		objParam7 = sqlSel.Parameters.Add("@phone", txtPhone.Text)
		objParam8 = sqlSel.Parameters.Add("@email", txtEmail.Text)
		objParam1.Direction = ParameterDirection.Input
		objParam2.Direction = ParameterDirection.Input
		objParam3.Direction = ParameterDirection.Input
		objParam4.Direction = ParameterDirection.Input
		objParam5.Direction = ParameterDirection.Input
		objParam6.Direction = ParameterDirection.Input
		objParam7.Direction = ParameterDirection.Input
		objParam8.Direction = ParameterDirection.Input

		'Dim selParm As SqlParameter = sqlSel.Parameters.Add("ReturnValue", SqlDbType.Int, 1)
		'selParm.Direction = ParameterDirection.ReturnValue
		Dim intNumAff As Integer
		sqlConn.Open()
		intNumAff = sqlSel.ExecuteNonQuery()
		If intNumAff > 0 Then
			lblstatus.Text = "Update Sucessful!"
			Panel1.Visible = False
		Else
			lblstatus.Text = "Update Failed!"
		End If
		sqlConn.Close()

Again I can't stress this enough in the world of programming / information technology. There are standards set, and they should be followed.
  • Standards like prefixing stored procedures with sp_ i.e. sp_updateUser<
  • Naming conventions in code : int , obj , str to prefix an INTEGER, and OBJECT, and a STRING as examples. <
Hope this helps.

Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1
Reputation: AusClansViper is an unknown quantity at this point 
Solved Threads: 0
AusClansViper AusClansViper is offline Offline
Newbie Poster

Re: ASP.Net VB Page to update a users profile not updating.

 
0
  #3
May 28th, 2006
Guess what.. I HAD the same problem..

after about 3-4 hours searching the net for a solution i gave up..

I even notice you posted on another site..

To correct the guy above AddWithValue Exists in ASP.NET v2

TO FIX the problem.. add a if statement
if(!IsPostBack)
{
// Bind Data Here..
}

OnClick Event Handler
{
// UPDATE SP Routine
Bind Data..
}

If you have a problem drop me an email ckolenko@ausclans.com
any emails are welcome..
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 71
Reputation: Kate Albany is an unknown quantity at this point 
Solved Threads: 1
Kate Albany Kate Albany is offline Offline
Junior Poster in Training

Re: ASP.Net VB Page to update a users profile not updating.

 
0
  #4
May 29th, 2006
Hello,

I use Java & Oracle, not .NET & SQL Server. Nevertheless, unless I am missing something about .NET, you need a 'commit' after the update and before you close the connection. Otherwise after the connection is colsed all your updates will be rolled back by the database by default.

Kate
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC