Ken
Ken

Reputation: 45

Dropdown list value to Int to database - Invalid Cast Exception was caught

I have a dropdown list. On a button-click event I get the selected value, convert it to an integer and save it to the database in an existing row. I am getting the following error:

"Invalid Cast Exception was caught.

The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlCommand objects."


NOTE: When I go through debugging with breakpoints, it appears that I am trying to send an integer as a parameter. I don't see quotes around the values in the little pop-up windows that display the variable contents for the selected dropdown value.

NOTE: When I directly execute the stored procedure in SQL Server I successfully update the row in the table.

Here is the dropdown list from the page source:

    <select name="ctl00$ContentPlaceHolder1$CustomerDetailsEdit1$ShippingRegionDropDownList" id="ctl00_ContentPlaceHolder1_CustomerDetailsEdit1_ShippingRegionDropDownList" style="width:200px;">
<option value="1">Please Select</option>
<option value="2">U.S. / Canada</option>
<option value="3">Europe</option>
<option selected="selected" value="4">Rest of World</option>

Here is the button click event which gets the value and converts it to an integer: (This is probably my third or fourth attempt to convert. I am guessing that there is a problem in the conversion process. I have tried parse and convert.)

    protected void Button2_Click(object sender, EventArgs e)
{
    // Get the current user's ID.
    string customerId = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString();
    // Get the values from the text box controls        
    int shippingRegionId = Int32.Parse(ShippingRegionDropDownList.SelectedValue);

    try
    {
        // Execute the update command
        bool success = CustomerDetailsAccess.UpdateShippingRegionID(customerId, shippingRegionId);
        // Display status message
        statusLabel.Text = success ? "Shipping Region update successful." : "Try - Shipping Region update failed";
    }
    catch
    {
        // Display error
        statusLabel.Text = "Catch - Shipping Region update failed.";
    }
}

*NOTE: I am getting the error on the CATCH.

Here is the code that creates parameters to send to other existing code that calls the stored procedure. (I don't want to include the other code to save space and because it has worked for a long time. But, if someone wants to see it, I will be happy to paste it.)

    // Update customer details
public static bool UpdateShippingRegionID(string customerId, int shippingRegionId)
{
    // Get a configured DbCommand object
    DbCommand comm = GenericDataAccess.CreateCommand();

    // Set the stored prodcedure name
    comm.CommandText = "UpdateShippingRegionID";

    // Create a new parameter
    DbParameter param = comm.CreateParameter();
    param.ParameterName = "@CustomerID";
    param.Value = customerId;
    param.DbType = DbType.String;
    param.Size = 50;
    comm.Parameters.Add(param);

    // Create a new parameter
    param = comm.CreateParameter();
    param.ParameterName = "@ShippingRegionID";
    param.Value = shippingRegionId;
    param.DbType = DbType.Int32;
    comm.Parameters.Add(comm);

    // Result will represent the number of changed rows
    int result = -1;
    try
    {
        // Execute the stored procedure
        result = GenericDataAccess.ExecuteNonQuery(comm);
    }
    catch
    {
        // Any errors are logged in the GenericDataAccess. It is not done here.
    }
    // Result will be 1 in case of success
    return (result != -1);

}

Here is the stored procedure:

    CREATE PROCEDURE UpdateShippingRegionID
    (@CustomerID char(50),
    @ShippingRegionID int)
    AS
    UPDATE CustomerDetails
    SET 
    ShippingRegionID = @ShippingRegionID
    WHERE CustomerID = @CustomerID

Note: The ShippingRegionID column is an INT.

Upvotes: 0

Views: 654

Answers (1)

TYY
TYY

Reputation: 2716

that's your problem

comm.Parameters.Add(comm);

Upvotes: 6

Related Questions