[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

"Specified cast is not valid." ERROR

[InvalidCastException: Specified cast is not valid.]
   ClothingLine.ProductsDB.GetProductDetails(String productID) in d:\clothingline\components\productsdb.cs:169
   ClothingLine.ProductDetailsPage.Page_Load(Object sender, EventArgs e) in d:\clothingline\productdetails.aspx.cs:51
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()

---------------------------------------------------------------------------------------------
I have the error above with the following code below: (I indicated the error lines with arrows)
And I counldn't figure out what went wrong... Can anyone offer some assistance?
---------------------------------------------------------------------------------------------

     //********************d:\clothingline\productdetails.aspx.cs*************
      private void Page_Load(object sender, System.EventArgs e)
            {

                  // Obtain ProductID from QueryString
                  string ProductID = Request.Params["ProductID"];

                  // Obtain Product Details
                  ClothingLine.ProductsDB products = new ClothingLine.ProductsDB();
                  ClothingLine.ProductDetails myProductDetails = products.GetProductDetails(ProductID); // <<------

                  // Update Controls with Product Details
                  desc.Text = myProductDetails.Description;
                  UnitCost.Text = String.Format("{0:c}", myProductDetails.UnitCost);
                  ModelName.Text = myProductDetails.ModelName;            
                  ModelNumber.Text = myProductDetails.ModelNumber;
                  addToCart.NavigateUrl = "AddToCart.aspx?ProductID=" + ProductID;
                  back.NavigateUrl = "javascript:history.go(-1)";
            }

            //*****************d:\clothingline\components\productsdb.cs***************

            public ProductDetails GetProductDetails(string productID)
            {

                  // Create Instance of Connection and Command Object
                  SqlConnection myConnection = new          SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
                  SqlCommand myCommand = new SqlCommand("NURI_ProductDetail", myConnection);

                  // Mark the Command as a SPROC
                  myCommand.CommandType = CommandType.StoredProcedure;

                  // Add Parameters to SPROC
                  SqlParameter parameterProductID = new SqlParameter("@ProductID", SqlDbType.NVarChar, 25);
                  parameterProductID.Value = productID;
                  myCommand.Parameters.Add(parameterProductID);

                  SqlParameter parameterModelNumber = new SqlParameter("@ModelNumber", SqlDbType.NVarChar, 25);
                  parameterModelNumber.Direction = ParameterDirection.Output;
                  myCommand.Parameters.Add(parameterModelNumber);

                  SqlParameter parameterModelName = new SqlParameter("@ModelName", SqlDbType.NVarChar, 50);
                  parameterModelName.Direction = ParameterDirection.Output;
                  myCommand.Parameters.Add(parameterModelName);

                  SqlParameter parameterUnitCost = new SqlParameter("@UnitCost", SqlDbType.Money, 8);
                  parameterUnitCost.Direction = ParameterDirection.Output;
                  myCommand.Parameters.Add(parameterUnitCost);

                  SqlParameter parameterDescription = new SqlParameter("@Description", SqlDbType.NVarChar, 1000);
                  parameterDescription.Direction = ParameterDirection.Output;
                  myCommand.Parameters.Add(parameterDescription);

                  // Open the connection and execute the Command
                  myConnection.Open();
                  myCommand.ExecuteNonQuery();
                  myConnection.Close();

                  // Create and Populate ProductDetails Struct using
                  // Output Params from the SPROC
                  ProductDetails myProductDetails = new ProductDetails();
                  myProductDetails.ModelNumber = (string)parameterModelNumber.Value;  // <<----
                  myProductDetails.ModelName = (string)parameterModelName.Value;
                  myProductDetails.UnitCost = (decimal)parameterUnitCost.Value;
                  myProductDetails.Description = ((string)parameterDescription.Value).Trim();

                  return myProductDetails;
            }
 
0
JaimeJegonia
Asked:
JaimeJegonia
  • 2
  • 2
  • 2
1 Solution
 
appariCommented:
can you post whats the type of ModelNumber  in ProductDetails ?
is it is string type?
0
 
JaimeJegoniaAuthor Commented:
ModelNumber is nvarchar(25) in my table
0
 
rmacfadyenCommented:
NULL values can throw this exception. Could the ModelNumber be NULL? Note you'll have to compare to DbNull.Value... not null. I also have found that this is a great spot for a debugger... stop at the line and examine the item being cast... the watch window will show you the underlying type... which general is enough info to determine what's up with the cast.

Rob
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
JaimeJegoniaAuthor Commented:
I already figured it out. I missed some code in my stored proc, wherein it doesn't assign the values it gets
so in turn it doesn't return values at all. So I'm actually casting null values in my code above.

But thanks for the comment, Rob.
0
 
rmacfadyenCommented:
Fine with me.

Rob
0
 
appariCommented:
but i think rmacfadyen deserve the points.

if you can see the last comment from rmacfadyen "NULL values can throw this exception..."
and the askers last comment indicates "So I'm actually casting null values in my code above" it is indeed is the problem.
so i think rmacfadyen deserve the points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now