0

我有一个下拉列表。在按钮单击事件中,我获取所选值,将其转换为整数并将其保存到数据库中现有行中。我收到以下错误:

“捕获了无效的演员表异常。

SqlParameterCollection 只接受非空的 SqlParameter 类型对象,不接受 SqlCommand 对象。”


注意:当我使用断点进行调试时,似乎我正在尝试发送一个整数作为参数。在显示所选下拉值的变量内容的小弹出窗口中,我看不到值的引号。

注意:当我直接在 SQL Server 中执行存储过程时,我成功更新了表中的行。

这是页面源的下拉列表:

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

这是获取值并将其转换为整数的按钮单击事件:(这可能是我第三次或第四次尝试转换。我猜转换过程中有问题。我尝试过解析和转换。)

    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.";
    }
}

*注意:我在 CATCH 上收到错误消息。

这是创建参数以发送到调用存储过程的其他现有代码的代码。(我不想包含其他代码以节省空间,因为它已经工作了很长时间。但是,如果有人想看到它,我很乐意粘贴它。)

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

}

这是存储过程:

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

注意: ShippingRegionID 列是一个 INT。

4

1 回答 1

6

那是你的问题

comm.Parameters.Add(comm);
于 2013-01-29T16:49:04.260 回答