"Specified cast is not valid." ERROR

Posted on 2006-05-22
Last Modified: 2012-08-13
[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;
Question by:JaimeJegonia
    LVL 39

    Expert Comment

    can you post whats the type of ModelNumber  in ProductDetails ?
    is it is string type?

    Author Comment

    ModelNumber is nvarchar(25) in my table
    LVL 5

    Accepted Solution

    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.


    Author Comment

    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.
    LVL 5

    Expert Comment

    Fine with me.

    LVL 39

    Expert Comment

    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

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now