Results 1 to 9 of 9

Thread: [RESOLVED] Posting Nulls to a SQL Server Stored Procedure

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [RESOLVED] Posting Nulls to a SQL Server Stored Procedure

    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

  2. #2
    Fanatic Member -TPM-'s Avatar
    Join Date
    Jul 2005
    Posts
    850

    Re: Posting Nulls to a SQL Server Stored Procedure

    Try passing a dbnull for the null parameters.
    TPM

    Add yourself to the VBForums Frappr Map!!

  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Posting Nulls to a SQL Server Stored Procedure

    TPM,

    Thanks for your reply

    I've changed all params that could be null to the following -

    VB Code:
    1. myCmd.Parameters.Add("@middlename", IIf(MName.Text = vbNullString, CSQ(MName.Text), DBNull.Value))

    Now got the following error -

    Object reference not set to an instance of an object.

    Here is a list of my imported namespaces and my CSQ function just incase there is something glaringly obviously wrong there
    VB Code:
    1. Imports System
    2. Imports System.DBNull
    3. Imports System.IO
    4. Imports System.Web
    5. Imports System.Web.UI
    6. Imports System.Web.UI.HtmlControls
    7. Imports System.Web.UI.WebControls
    8. Imports System.Data.SqlClient
    9.  
    10. Private Function CSQ(ByVal str As String) As String
    11.         If str = Nothing Then str = vbNullString
    12.         CSQ = Replace(str, "'", "''")
    13. End Function

  4. #4

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Posting Nulls to a SQL Server Stored Procedure

    I've changed my proc to the following and it's worked, the object reference error must be caused by the call to IIF??

    VB Code:
    1. 'myCmd.Parameters.Add("@middlename", IIf(MName.Text = vbNullString, CSQ(MName.Text), DBNull.Value))
    2. myCmd.Parameters.Add("@middlename", DBNull.Value)
    Does anyone know what namespace I need to reference to use IIF? Or do I have to write the function myself as we used to have to do for vbScript?
    Last edited by aconybeare; Sep 18th, 2005 at 07:30 AM.

  5. #5
    Fanatic Member -TPM-'s Avatar
    Join Date
    Jul 2005
    Posts
    850

    Re: Posting Nulls to a SQL Server Stored Procedure

    Not sure about iif (maybe someone else can help on that), but for now why not just use an if, then, else? ie it textbow.text > nothing add param with textbox.text else add it with dbnull.
    TPM

    Add yourself to the VBForums Frappr Map!!

  6. #6

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Posting Nulls to a SQL Server Stored Procedure

    Hhmmm, I had my IIF condition the wrong way round -

    myCmd.Parameters.Add("@middlename", IIf(MName.Text = vbNullString, CSQ(MName.Text), DBNull.Value))
    should be -
    VB Code:
    1. myCmd.Parameters.Add("@middlename", IIf(MName.Text = vbNullString, [b]DBNull.Value, CSQ(MName.Text)[/b]))

    Just relieved that it was me that spotted it

    Thanks TPM for the DBNull tip.

    Cheers Al

  7. #7
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [RESOLVED] Posting Nulls to a SQL Server Stored Procedure

    Quote Originally Posted by aconybeare
    ...snipped text...
    Code:
    	RETURN SCOPE_IDENTITY()
    I have never seen this before, so I just now went and looked it up.

    I have always used @@IDENTITY. Now, understanding the problem that triggers may cause me by using @@IDENTITY, I will begin using SCOPE_IDENTITY() -- all as a result of some innocuous code that you posted.

    So, thanks! =)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  8. #8
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [RESOLVED] Posting Nulls to a SQL Server Stored Procedure

    Oh yeah, might I mention, code posted five months ago.... =)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  9. #9

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: [RESOLVED] Posting Nulls to a SQL Server Stored Procedure

    No worries my Lord always happy to help even if it was indirectly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width