删除GridView控件中的图像

我正在设计一个GridView控件,它允许编辑和删除包含图像的行。每当我尝试使用自动生成的DELETE方法时,我都会收到以下错误: “操作数类型冲突:varbinary(max)与sql_variant不兼容” 这是我的代码:
<div id="content">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
            ConnectionString="<%$ ConnectionStrings:InventoryConnectionString %>" DeleteCommand="DELETE FROM [Inventory] WHERE [id] = @original_id AND [stockNum] = @original_stockNum AND [make] = @original_make AND [model] = @original_model AND [year] = @original_year AND (([miles] = @original_miles) OR ([miles] IS NULL AND @original_miles IS NULL)) AND (([engine] = @original_engine) OR ([engine] IS NULL AND @original_engine IS NULL)) AND (([drive] = @original_drive) OR ([drive] IS NULL AND @original_drive IS NULL)) AND (([color] = @original_color) OR ([color] IS NULL AND @original_color IS NULL)) AND (([description] = @original_description) OR ([description] IS NULL AND @original_description IS NULL)) AND (([image1] = @original_image1) OR ([image1] IS NULL AND @original_image1 IS NULL)) AND (([image2] = @original_image2) OR ([image2] IS NULL AND @original_image2 IS NULL)) AND (([image3] = @original_image3) OR ([image3] IS NULL AND @original_image3 IS NULL)) AND (([image4] = @original_image4) OR ([image4] IS NULL AND @original_image4 IS NULL)) AND (([image5] = @original_image5) OR ([image5] IS NULL AND @original_image5 IS NULL)) AND (([added] = @original_added) OR ([added] IS NULL AND @original_added IS NULL))"
            InsertCommand="INSERT INTO [Inventory] ([stockNum], [make], [model], [year], [miles], [engine], [drive], [color], [description], [image1], [image2], [image3], [image4], [image5], [added]) VALUES (@stockNum, @make, @model, @year, @miles, @engine, @drive, @color, @description, @image1, @image2, @image3, @image4, @image5, @added)"
            OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Inventory]"

            UpdateCommand="UPDATE [Inventory] SET [stockNum] = @stockNum, [make] = @make, [model] = @model, [year] = @year, [miles] = @miles, [engine] = @engine, [drive] = @drive, [color] = @color, [description] = @description, [image1] = @image1, [image2] = @image2, [image3] = @image3, [image4] = @image4, [image5] = @image5, [added] = @added WHERE [id] = @original_id AND [stockNum] = @original_stockNum AND [make] = @original_make AND [model] = @original_model AND [year] = @original_year AND (([miles] = @original_miles) OR ([miles] IS NULL AND @original_miles IS NULL)) AND (([engine] = @original_engine) OR ([engine] IS NULL AND @original_engine IS NULL)) AND (([drive] = @original_drive) OR ([drive] IS NULL AND @original_drive IS NULL)) AND (([color] = @original_color) OR ([color] IS NULL AND @original_color IS NULL)) AND (([description] = @original_description) OR ([description] IS NULL AND @original_description IS NULL)) AND (([image1] = @original_image1) OR ([image1] IS NULL AND @original_image1 IS NULL)) AND (([image2] = @original_image2) OR ([image2] IS NULL AND @original_image2 IS NULL)) AND (([image3] = @original_image3) OR ([image3] IS NULL AND @original_image3 IS NULL)) AND (([image4] = @original_image4) OR ([image4] IS NULL AND @original_image4 IS NULL)) AND (([image5] = @original_image5) OR ([image5] IS NULL AND @original_image5 IS NULL)) AND (([added] = @original_added) OR ([added] IS NULL AND @original_added IS NULL))">
            <DeleteParameters>
                <asp:Parameter Name="original_id" Type="Int32" />
                <asp:Parameter Name="original_stockNum" Type="String" />
                <asp:Parameter Name="original_make" Type="String" />
                <asp:Parameter Name="original_model" Type="String" />
                <asp:Parameter Name="original_year" Type="Int32" />
                <asp:Parameter Name="original_miles" Type="Int32" />
                <asp:Parameter Name="original_engine" Type="String" />
                <asp:Parameter Name="original_drive" Type="String" />
                <asp:Parameter Name="original_color" Type="String" />
                <asp:Parameter Name="original_description" Type="String" />
                <asp:Parameter Name="original_image1" Type="Object" />
                <asp:Parameter Name="original_image2" Type="Object" />
                <asp:Parameter Name="original_image3" Type="Object" />
                <asp:Parameter Name="original_image4" Type="Object" />
                <asp:Parameter Name="original_image5" Type="Object" />
                <asp:Parameter DbType="Date" Name="original_added" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="stockNum" Type="String" />
                <asp:Parameter Name="make" Type="String" />
                <asp:Parameter Name="model" Type="String" />
                <asp:Parameter Name="year" Type="Int32" />
                <asp:Parameter Name="miles" Type="Int32" />
                <asp:Parameter Name="engine" Type="String" />
                <asp:Parameter Name="drive" Type="String" />
                <asp:Parameter Name="color" Type="String" />
                <asp:Parameter Name="description" Type="String" />
                <asp:Parameter Name="image1" Type="Object" />
                <asp:Parameter Name="image2" Type="Object" />
                <asp:Parameter Name="image3" Type="Object" />
                <asp:Parameter Name="image4" Type="Object" />
                <asp:Parameter Name="image5" Type="Object" />
                <asp:Parameter DbType="Date" Name="added" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="stockNum" Type="String" />
                <asp:Parameter Name="make" Type="String" />
                <asp:Parameter Name="model" Type="String" />
                <asp:Parameter Name="year" Type="Int32" />
                <asp:Parameter Name="miles" Type="Int32" />
                <asp:Parameter Name="engine" Type="String" />
                <asp:Parameter Name="drive" Type="String" />
                <asp:Parameter Name="color" Type="String" />
                <asp:Parameter Name="description" Type="String" />
                <asp:Parameter Name="image1" Type="Object" />
                <asp:Parameter Name="image2" Type="Object" />
                <asp:Parameter Name="image3" Type="Object" />
                <asp:Parameter Name="image4" Type="Object" />
                <asp:Parameter Name="image5" Type="Object" />
                <asp:Parameter Name="added" DbType="Date"></asp:Parameter>
                <asp:Parameter Name="original_id" Type="Int32" />
                <asp:Parameter Name="original_stockNum" Type="String" />
                <asp:Parameter Name="original_make" Type="String" />
                <asp:Parameter Name="original_model" Type="String" />
                <asp:Parameter Name="original_year" Type="Int32" />
                <asp:Parameter Name="original_miles" Type="Int32" />
                <asp:Parameter Name="original_engine" Type="String" />
                <asp:Parameter Name="original_drive" Type="String" />
                <asp:Parameter Name="original_color" Type="String" />
                <asp:Parameter Name="original_description" Type="String" />
                <asp:Parameter Name="original_image1" Type="Object" />
                <asp:Parameter Name="original_image2" Type="Object" />
                <asp:Parameter Name="original_image3" Type="Object" />
                <asp:Parameter Name="original_image4" Type="Object" />
                <asp:Parameter Name="original_image5" Type="Object" />
                <asp:Parameter DbType="Date" Name="original_added" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" DataKeyNames="id" 
            DataSourceID="SqlDataSource1" CssClass="GridViewStyle">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True"
                    HeaderText="Options" />
                <asp:BoundField DataField="stockNum" HeaderText="Stock #" SortExpression="stockNum" />
                <asp:BoundField DataField="make" HeaderText="Make" SortExpression="make" />
                <asp:BoundField DataField="model" HeaderText="Model" SortExpression="model" />
                <asp:BoundField DataField="year" HeaderText="Year" SortExpression="year" />
                <asp:BoundField DataField="miles" HeaderText="Miles" SortExpression="miles" />
                <asp:BoundField DataField="engine" HeaderText="Engine" SortExpression="engine" />
                <asp:BoundField DataField="drive" HeaderText="Drive" SortExpression="drive" />
                <asp:BoundField DataField="color" HeaderText="Color" SortExpression="color" />
                <asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
                <asp:ImageField DataImageUrlField="id" DataImageUrlFormatString="ImageHandler.aspx?ImageID={0}"
                    ControlStyle-Width="200" ControlStyle-Height="120" HeaderText="Preview Image">
                    <ControlStyle Height="120px" Width="200px"></ControlStyle>
                </asp:ImageField>
            </Columns>
            <RowStyle CssClass="RowStyle" />
            <EmptyDataRowStyle CssClass="EmptyRowStyle" />
            <PagerStyle CssClass="PagerStyle" />
            <SelectedRowStyle CssClass="SelectedRowStyle" />
            <HeaderStyle CssClass="HeaderStyle" />
            <EditRowStyle CssClass="EditRowStyle" />
            <AlternatingRowStyle CssClass="AltRowStyle" />
        </asp:GridView>
    </div>
代码隐藏:
using System;
using System.Configuration;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Account_EditInventory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.
            ConnectionStrings["InventoryConnectionString"].ConnectionString;
        string strQuery = "select id, image1 from Inventory";
        SqlCommand cmd = new SqlCommand(strQuery);
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            GridView1.DataBind();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
            dt.Dispose();
        }
    }
}
    
已邀请:
您不需要尝试将图像作为参数传递给删除查询,并重写它只是将密钥传递给它们图像。     

要回复问题请先登录注册