Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:31:48+00:00 2026-06-17T23:31:48+00:00

I have a dropdown list. On a button-click event I get the selected value,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:31:50+00:00Added an answer on June 17, 2026 at 11:31 pm

    that’s your problem

    comm.Parameters.Add(comm);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dropdown list. I am using onchange event of this dropdown to
I have multiple dropdown list: @Html.DropDownListFor(x => x.HaveColoSpace.SelectedOptions, new SelectList(Model.HaveColoSpace.Options, Value, Text), new {
I have dropdownlist which having records 'All','A','B','C' . On click event of button I
I have the following Dropdown list, which has a Event associated with it, when
I have a dropdown list inside a asp:repeater item template. how can I get
I have (1) dropdown list, (2) listboxes, (1) button on a page. The listboxes
I have a dropdown list with a few options and a group of checkboxes
I have a dropdown list (FK) which I would like to set and display
I have a dropdown list containing the days of the week - Monday to
I have a dropdown list with a piece of code that is run when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.