Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing parameters to SQL Stored Procedure

Status
Not open for further replies.

Halliarse

IS-IT--Management
Jan 8, 2007
213
GB
Good Afternoon Smart People

I'm hoping that someone can shed some light on whether I am being a fool or not! I have an application that throughout it's structure has various web-based forms, most of which pass parameters to stored procedures to either insert new or update existing records. Everyone of these works perfectly with the exception of this one and I cannot work out why!

The form has an update button which call runs the following code:

Private Sub butUpdateParameters_Click(sender As Object, e As EventArgs) Handles butUpdateParameters.Click

Dim ParamID As Integer = 0
Dim constr As String = ConfigurationManager.ConnectionStrings("SnipITConnectionString").ConnectionString
Dim SQL_SP As String
Dim message As String = String.Empty

If Session("ParamExists") = 0 Then
SQL_SP = "Insert_Parameter"
Else
SQL_SP = "Update_Parameter"
End If

Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(SQL_SP)
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.AddWithValue("@ParamID", 1)
cmd.Parameters.AddWithValue("@BackgroundColour", ASPxColorEdit1.Color.ToArgb)

If chkSMS.Checked = True Then
cmd.Parameters.AddWithValue("@SMS", 1)
cmd.Parameters.AddWithValue("@SMSUsername", txtSMSUserName.Text.Trim())
cmd.Parameters.AddWithValue("@SMSPassword", txtSMSPassword.Text.Trim())
cmd.Parameters.AddWithValue("@SMSReminder", txtSMSReminder.Text.Trim())
cmd.Parameters.AddWithValue("@SMSCredits", txtSMSCredits.Text.Trim())
Else
cmd.Parameters.AddWithValue("@SMS", 0)
cmd.Parameters.AddWithValue("@SMSUsername", vbNull)
cmd.Parameters.AddWithValue("@SMSPassword", vbNull)
cmd.Parameters.AddWithValue("@SMSReminder", vbNull)
cmd.Parameters.AddWithValue("@SMSCredits", vbNull)
End If

If Len(txtImagePath.Text) = 0 Then
cmd.Parameters.AddWithValue("@ImagePath", vbNull)
Else
cmd.Parameters.AddWithValue("@ImagePath", txtImagePath.Text.Trim())
End If


cmd.Connection = con
con.Open()
ParamID = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using

Select Case ParamID
Case -1
message = "Something has gone wrong!"
Session("Image") = "~/Images/cross.jpg"
Session("Message") = message
Session("PrevPage") = "~/SystemParameters.aspx"
Response.Redirect("~/SnipMessage.aspx", False)
Exit Select
Case Else
message = "System Parameters have been recorded."
Session("Image") = "~/Images/tick.jpg"
Session("Message") = message
Session("PrevPage") = "~/default.aspx"
Response.Redirect("~/SnipMessage.aspx", False)
Exit Select
End Select

End Using

End Sub

When I run the form and it runs the Insert_Parameter, it works perfectly, the Update_Parameter is the issue. The code is below:

USE [Snip-IT]
GO
/****** Object: StoredProcedure [dbo].[Update_Parameter] Script Date: 12/12/2014 15:09:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Update_Parameter]
@ParamID INT,
@BackgroundColour INT,
@SMS INT,
@SMSUsername NVARCHAR(20),
@SMSPassword NVARCHAR(20),
@SMSReminder INT,
@SMSCredits INT,
@ImagePath NVARCHAR(MAX)

AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT ParamID FROM SystemParameters WHERE ParamID = @ParamID)
BEGIN
Update SystemParameters
Set BackgroundColour = @BackgroundColour,
SMS = @SMS,
SMSUsername = @SMSUsername,
SMSPassword = @SMSPassword,
SMSReminder = @SMSReminder,
SMSCredits = @SMSCredits,
ImagePath = @ImagePath
Where ParamID = @ParamID

SELECT @ParamID -- ParamID
END
Else
BEGIN
SELECT -1 -- Parameter does not exists.
END

END

If I execute the the stored procedure from MSSMS it updates the table fine, however through the form it says that tha database is updated, returning a zero, but when I review the table, nothing has changed!

Can anyone offer some incite into where I may be going wrong?

Many thanks

Steve

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top