Hi,

I am trying to insert a record into my SQL server db table using a stored procedure. Some of the values can be null and have been marked as such on the db, but when I call the procedure I get an error message saying that an expected parameter is missing, these missing params are nulls that I'm trying to pass through.

Can anyone see where I'm going wrong with this?

Error -
"Procedure 'insert_New_Person' expects parameter '@address3', which was not supplied.insert failed"

Any help will be most welcome

Cheers Al

Here is my vb.net function
VB Code:
  1. Private Function Save_Person() As String
  2.  
  3.         ' Open the connection
  4.         myCon.Open()
  5.        
  6.         Dim myCmd As New SqlCommand("insert_New_Person", myCon)
  7.         myCmd.CommandType = CommandType.StoredProcedure
  8.  
  9.         ' Create a SqlParameter object to hold the output parameter value
  10.         Dim retValParam As New SqlParameter("@RETURN_VALUE", SqlDbType.Int)
  11.  
  12.         ' IMPORTANT - must set Direction as ReturnValue
  13.         retValParam.Direction = ParameterDirection.ReturnValue
  14.  
  15.         ' Add the parameters to the Command's Parameters collection
  16.         myCmd.Parameters.Add("@username", LCase(Username.Text))
  17.         myCmd.Parameters.Add("@password", NewPassword1.Text)
  18.         myCmd.Parameters.Add("@prefix", Prefix.Text)
  19.         myCmd.Parameters.Add("@firstname", CSQ(FName.Text)) ' CSQ = Comment Single Quotes
  20.         myCmd.Parameters.Add("@middlename", CSQ(MName.Text))
  21.         myCmd.Parameters.Add("@lastname", CSQ(LName.Text))
  22.         myCmd.Parameters.Add("@department", CSQ(Department.Text))
  23.         myCmd.Parameters.Add("@institution", CSQ(Institution.Text))
  24.         myCmd.Parameters.Add("@address1", CSQ(Address1.Text))
  25.         myCmd.Parameters.Add("@address2", CSQ(Address2.Text))
  26.         myCmd.Parameters.Add("@address3", CSQ(Address3.Text))
  27.         myCmd.Parameters.Add("@address4", CSQ(Address4.Text))
  28.         myCmd.Parameters.Add("@town", CSQ(City.Text))
  29.         myCmd.Parameters.Add("@county", CSQ(State.Text))
  30.         myCmd.Parameters.Add("@postcode", PostalCode.Text)
  31.         myCmd.Parameters.Add("@country", ddlCountry.SelectedValue)
  32.         myCmd.Parameters.Add("@telephone", Phone.Text)
  33.         myCmd.Parameters.Add("@fax", Fax.Text)
  34.         myCmd.Parameters.Add("@email", Email.Text)
  35.         ' Finally, add the output parameter
  36.         myCmd.Parameters.Add(retValParam)
  37.  
  38.         Dim dr As SqlDataReader
  39.         ' Call the SProc...
  40.  
  41.         Try
  42.             dr = myCmd.ExecuteReader()
  43.             ' Now you can grab the output parameter's value...
  44.             Dim retVal As Integer = Convert.ToInt32(retValParam.Value)
  45.             If retVal > 0 Then
  46.                 Save_Person = "OK"
  47.             Else
  48.                 Save_Person = "Error"
  49.             End If
  50.         Catch ex As Exception
  51.             ' Record any exceptions and exit
  52.             Dim myLiteral As New Literal
  53.             myLiteral.Text = "The following exception occurred: <br />" + ex.Message.ToString
  54.             Page.Controls.Add(myLiteral)
  55.             Save_Person = "Error"
  56.         End Try
  57.  
  58.         dr.Close()
  59.         myCon.Close()
  60.     End Function

Here is a copy of my stored Proc
Code:
CREATE Procedure insert_New_Person
(
	@username varchar(255),
	@password varchar(15),
	@prefix varchar(25),
	@firstname nvarchar(50),
	@middlename nvarchar(50),
	@lastname nvarchar(50),
	@department nvarchar(150),
	@institution nvarchar(100),
	@address1 nvarchar(100),
	@address2 nvarchar(100),
	@address3 nvarchar(100),
	@address4 nvarchar(100),
	@town nvarchar(50),
	@county nvarchar(50),
	@postcode nvarchar(25),
	@country nvarchar(100),
	@telephone varchar(50),
	@fax varchar(50),
	@email varchar(255)
)
As
	-- INSERT the New record
	INSERT INTO myDB.People
		(
		User_Name, 
		Password, 
		Prefix, 
		First_Name, 
		Middle_Name, 
		Last_Name,
		Department,
		Institution,
		Address1,
		Address2,
		Address3,
		Address4,
		Town,
		County,
		Postcode,
		Country,
		Telephone,
		Fax,
		Email
		)
	VALUES
		(
		@username, 
		@password, 
		@prefix, 
		@firstname, 
		@middlename, 
		@lastname,
		@department,
		@institution,
		@address1,
		@address2,
		@address3,
		@address4,
		@town,
		@county,
		@postcode,
		@country,
		@telephone,
		@fax,
		@email
		)
	-- Now return the User_ID of the newly inserted record
	RETURN SCOPE_IDENTITY()
Go