I have a stored procedure that has been working just fine, but I recently added three new params to it:
**@HasObits**, **@AlternateName**, and **@Notes**.
CREATE PROCEDURE dbo.sp_InsertNewRecord
(
@CountryID int,
@StateID int,
@CountyID int,
@RecordsOnline bit = 0,
**@HasObits bit = 0,**
@Name varchar(100),
**@AlternateName varchar(100) = null,**
@Address varchar(50) = null,
@Address2 varchar(50) = null,
@City varchar(50) = null,
@ZipCode varchar(10) = null,
@Phone char(10) = null,
@Fax char(10) = null,
@Email varchar(50) = null,
@Website varchar(100) = null,
@SearchURI varchar(150) = null,
**@Notes text = null,**
@AddedBy uniqueidentifier = null
)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO MyTable (CountryID, StateID, CountyID, RecordsOnline, HasObituaries, [Name], AlternateName, [Address], Address2, City, ZipCode, Phone, Fax, Email, Website, SearchURI, Notes, AddedBy)
VALUES (@CountryID, @StateID, @CountyID, @RecordsOnline, @HasObits, @Name, @AlternateName, @Address, @Address2, @City, @ZipCode, @Phone, @Fax, @Email, @Website, @SearchURI, @Notes, @AddedBy)
END
GO
This code is being called by my data access layer in this method:
public void Insert(Company company)
{
// create connection
SqlConnection conn = new SqlConnection(_connectionString);
// create command
SqlCommand cmd = new SqlCommand(COMPANY_INSERT, conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
// init command params
cmd.Parameters.AddWithValue("CountryID", company.CountryID);
cmd.Parameters.AddWithValue("StateID", company.StateID);
cmd.Parameters.AddWithValue("CountyID", company.CountyID);
**cmd.Parameters.AddWithValue("HasObituaries", company.HasObituaries);**
cmd.Parameters.AddWithValue("Name", company.Name);
**cmd.Parameters.AddWithValue("AlternateName", (string.IsNullOrEmpty(company.AlternateName)) ? null : company.AlternateName);**
cmd.Parameters.AddWithValue("Address", (company.Address.Length == 0) ? null : company.Address);
cmd.Parameters.AddWithValue("City", (company.City.Length == 0) ? null : company.City);
cmd.Parameters.AddWithValue("ZipCode", (company.ZipCode.Length == 0) ? null : company.ZipCode);
cmd.Parameters.AddWithValue("Phone", (company.Phone.Length == 0) ? null : company.Phone);
cmd.Parameters.AddWithValue("Fax", (company.Fax.Length == 0) ? null : company.Fax);
cmd.Parameters.AddWithValue("Website", (company.Website.Length == 0) ? null : company.Website);
cmd.Parameters.AddWithValue("SearchURI", (company.SearchURI.Length == 0) ? null : company.SearchURI);
cmd.Parameters.AddWithValue("AddedBy", (company.AddedBy == Guid.Empty) ? null : company.AddedBy.ToString());
**cmd.Parameters.AddWithValue("Notes", company.Notes);**
using (conn)
{
conn.Open();
cmd.ExecuteNonQuery();
}
}
This same method has been working just fine, until I added the three new params. Now when this method runs it keeps giving me the error: “@HasObits is not a parameter for procedure sp_InsertNewRecord” If I comment that line out, it says the same error only with @AlternateName, then I comment that out and it says the same thing about @Notes, so I comment that out and then it inserts the record successfully.
If I had to guess, I would say that the SqlCommand is not registering the param for some reason. I can run the following SQL command in SQL Server 2008 and it works exactly how I expect:
exec sp_InsertNewRecord @CountryID = 1, @StateID = 13, @CountyID = 1, @Name = 'TESTING', @HasObits = 1, @Notes = 'testing notes', @AlternateName = 'alternate name'
I’m at a loss at this point. I’ve never had a problem like this and have spent about 5 hours now trying to find an answer to this, so any help would be greatly appreciated!
The DataTypes for the params being passed in (company.HasObits is a bool, company.AlternateName is a string, and company.Notes is a string).
Thank you in advance for your help! I’m very very frustrated by this and hoping someone can shed some light on this.
This problem was down to the hosting company moving database servers as I was testing the application and it was no longer pointing to the correct database anymore.
All the code was/is correct (which makes me feel better), but it was just pointing to the wrong db server. I got an email about 5 hours later from the company telling me it was switched and that I need to update any references to the old server.