I have both an Add and an Update function and stored procedure. I am using the same code for both functions with the exception of the parameters (Update has 2 Add doesn't have). The Add function works fine, the Update doesn't work at all.
VB Code:
Private Sub Update()
Dim cmd As New SqlCommand("pContact_Update", cn)
Try
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = UserId
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text
cmd.Parameters.Add("@MiddleName", SqlDbType.VarChar).Value = txtMiddleName.Text
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text
...etc more parameters... cmd.Parameters.Add("@Id", SqlDbType.VarChar).Value = ContactId
cn.Open()
cmd.ExecuteScalar()
lblMessage.Text = "SUCCESS"
lblNoProfile.Visible = False
Catch ex As Exception
lblMessage.Text = ex.Message
End Try
End Sub
When I run the program, I receive the SUCCESS message, however the record is not in fact updated.
The add function looks the same but it actually adds a record. Could it be my stored procedures?
CREATE PROCEDURE [dbo].[pContact_Update]
@Id int,
@UserId int,
@FirstName varchar(20),
@MiddleName varchar(20),
@LastName varchar(20),
...etc
AS
UPDATE tContacts SET
UserId = @UserId, FirstName = @FirstName,
MiddleName = @MiddleName, LastName = @LastName,
BusinessName = @BusinessName,
...etc...
EMail = @EMail
WHERE Id = @Id
GO
I would appreciate any advice. I can not seem to find the difference, why one is working and the other is not...
Thanks!
VB Code:
Private Sub Update()
Dim cmd As New SqlCommand("pContact_Update", cn)
Try
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = UserId
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text
cmd.Parameters.Add("@MiddleName", SqlDbType.VarChar).Value = txtMiddleName.Text
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text
...etc more parameters... cmd.Parameters.Add("@Id", SqlDbType.VarChar).Value = ContactId
cn.Open()
cmd.ExecuteScalar()
lblMessage.Text = "SUCCESS"
lblNoProfile.Visible = False
Catch ex As Exception
lblMessage.Text = ex.Message
End Try
End Sub
When I run the program, I receive the SUCCESS message, however the record is not in fact updated.
The add function looks the same but it actually adds a record. Could it be my stored procedures?
CREATE PROCEDURE [dbo].[pContact_Update]
@Id int,
@UserId int,
@FirstName varchar(20),
@MiddleName varchar(20),
@LastName varchar(20),
...etc
AS
UPDATE tContacts SET
UserId = @UserId, FirstName = @FirstName,
MiddleName = @MiddleName, LastName = @LastName,
BusinessName = @BusinessName,
...etc...
EMail = @EMail
WHERE Id = @Id
GO
I would appreciate any advice. I can not seem to find the difference, why one is working and the other is not...
Thanks!