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

  • 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)

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?

      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)";


            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;

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

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

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

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

                  // Open the connection and execute the Command

                  // 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;
  • 2
  • 2
  • 2
1 Solution
can you post whats the type of ModelNumber  in ProductDetails ?
is it is string type?
JaimeJegoniaAuthor Commented:
ModelNumber is nvarchar(25) in my table
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.

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.
Fine with me.

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.

Featured Post


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