When I run the stored procedure manually, it works fine. However, whenever I try to call it with my code below, nothing is updating. One thing I’ve noticed is that in my stored procedure, I change the “Modified Date” field to the current time/date and that IS updating; however, none of my other values are.
Here’s my method I’m using:
public void EditProduct(int productID, string productName, string productNumber, string productColor, string productSubcategory)
{
ConfigureDataAccess();
myCommand = new SqlCommand("[dbo].[UpdateProductInfo]", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@ProductID", productID);
myCommand.Parameters.AddWithValue("@ProductName", String.IsNullOrWhiteSpace(productName) ? (object)DBNull.Value : (object)productName);
myCommand.Parameters.AddWithValue("@ProductNumber", String.IsNullOrWhiteSpace(productNumber) ? (object)DBNull.Value : (object)productNumber);
myCommand.Parameters.AddWithValue("@ProductColor", String.IsNullOrWhiteSpace(productColor) ? (object)DBNull.Value : (object)productColor);
myCommand.Parameters.AddWithValue("@ProductSubcategory", String.IsNullOrWhiteSpace(productSubcategory) ? (object)DBNull.Value : (object)productSubcategory);
try
{
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch (SqlException exception)
{
exception.Data.Add("cstring", myConfiguration);
throw;
}
catch (InvalidOperationException ex)
{
ex.Data.Add("cStatus", myConnection.State);
throw;
}
}
finally
{
myConnection.Close();
}
I don’t believe ConfigureDataAccess() is an issue because other methods are using it and they are running fine. Is there anything that immediately sticks out?
Edit: Here’s my stored procedure:
ALTER PROCEDURE [dbo].[UpdateProductInfo]
@ProductID as int,
@ProductName as varchar(50),
@ProductNumber as varchar(25),
@ProductColor as varchar(15),
@ProductSubcategory as varchar(50)
AS
BEGIN
SET NOCOUNT ON;
UPDATE P
SET P.Name = @ProductName, P.ProductNumber = @ProductNumber, P.Color = @ProductColor, P.ModifiedDate = GetDate(),
P.ProductSubcategoryID = (SELECT PS.ProductSubcategoryID FROM Production.ProductSubcategory PS WHERE PS.Name = @ProductSubcategory)
FROM Production.Product as P
LEFT OUTER JOIN Production.ProductSubcategory as PS ON P.ProductSubcategoryID = PS.ProductSubcategoryID
WHERE P.ProductID = @ProductID
END
Here’s my ConfigureDataAccess() method:
private string myConfiguration;
private SqlConnection myConnection;
private SqlCommand myCommand;
private SqlDataReader myReader;
private List<Product> myProducts;
public void ConfigureDataAccess()
{
myConfiguration = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
myConnection = new SqlConnection(myConfiguration);
myReader = null;
myProducts = new List<Product>();
}
I figured out the problem. I needed to include
if (!IsPostBack)in myPage_Loadevent. I didn’t realize that when I was clicking the “Save” button on my webform that it was posting back and setting my textboxes (on which the parameters in theEditProduct()method are getting their values) back to their original values.