简体   繁体   English

下拉列表值Int到数据库-捕获到无效的强制转换异常

[英]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." SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受SqlCommand对象。”


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. 注意:在SQL Server中直接执行存储过程时,我成功更新了表中的行。

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. *注意:我在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. 注意:ShippingRegionID列为INT。

那是你的问题

comm.Parameters.Add(comm);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM