【问题标题】:Mapping Error With DropDownListDropDownList 的映射错误
【发布时间】:2012-01-24 16:44:42
【问题描述】:

我在用于添加产品的下拉列表中显示类别。添加产品时,您必须选择一个类别并创建产品或更新以前的产品。

但我的问题是我收到以下错误:

对象类型不存在映射 System.Web.UI.WebControls.DropDownList 到已知的托管提供程序 本机类型。

数据库图:

ASPX.:

    <p>Kategori</p>
    <asp:DropDownList ID="DDCategories" runat="server" AutoPostBack="True">
    </asp:DropDownList>

ASPX.CS.:

       protected void Page_Load(object sender, EventArgs e)
        {
            //Dropdown Category Names From DB
            if (!IsPostBack)
            {
                string sConstr = ConfigurationManager.ConnectionStrings["LLcateringConnectionString"].ConnectionString;
                SqlConnection Conn = new SqlConnection(sConstr);
                DataTable dt = new DataTable("tbl");

                using (Conn)
                {
                    Conn.Open();
                    SqlCommand comm = new SqlCommand("SELECT Name FROM Category", Conn);
                    SqlDataAdapter da = new SqlDataAdapter(comm);
                    da.Fill(dt);
                }

                DDCategories.DataSource = dt;
                DDCategories.DataTextField = "Name";
                DDCategories.DataBind();
            }
    }

 protected void BtnUpdateOrCreate_Click(object sender, EventArgs e)
    {
        // Text in fields has to exist, if they are requierd
        if (!string.IsNullOrWhiteSpace(TxtName.Text) /*&&
            !string.IsNullOrWhiteSpace(TxtDescription.Text)*/)
        {
            // New the DataAccess and have all the parameteres here
            DataAccess dataAccess = new DataAccess();
            dataAccess.AddParameter("@Name", TxtName.Text);
            dataAccess.AddParameter("@Category_ID", DDCategories);
            dataAccess.AddParameter("@Description", TxtDescription.Text.ToNewline(false));
            dataAccess.AddParameter("@UnitPrice", TxtPrice.Text);
            dataAccess.AddParameter("@DiscountUnitPrice", TxtUnitDiscount.Text);

            if (isCreate)
            {
                // Insert query
                dataAccess.Execute(@"INSERT INTO [Product] ([Name], [Category_ID], [UnitPrice], [DiscountUnitPrice], [Description])
                                     VALUES (@Name, @Category_ID, @UnitPrice, @DiscountUnitPrice, @Description)
                                    INNER JOIN dbo.Product ON dbo.Category.ID = dbo.Product.Category_ID 
                                    ORDER BY [Name]
                                    WHERE id = @id");
            }
            else
            {
                // Update query
                dataAccess.AddParameter("@id", MenuID);
                dataAccess.Execute(@"UPDATE [Product]
                                    SET [Name] = @Name, [Category_ID] = @Category_ID, [UnitPrice] = @UnitPrice, [DiscountUnitPrice] = @DiscountUnitPrice, [Description] = @Description
                                    WHERE id = @id");
                                    //UPDATE [Product]
                                    //SET [Name] = @Name, [Category_ID] = @Category_ID, [UnitPrice] = @UnitPrice, [DiscountUnitPrice] = @DiscountUnitPrice, [Description] = @Description
                                    //INNER JOIN dbo.Product ON dbo.Category.ID = dbo.Product.Category_ID 
                                    //ORDER BY [Name]
                                    //WHERE id = @id");
            }

            // Redirects to list
            Response.Redirect(Request.Url.AbsolutePath);
        }
        else
            LitStatus.Text = "Hey så indtast da noget!";
    }

【问题讨论】:

  • 您打算通过此声明实现什么目标? Response.Redirect(Request.Url.AbsolutePath);
  • @Lloyd,将被重定向回列出所有产品的页面。但由于单击“更新”或“创建”时出现上述错误,我看不到它是否有效。
  • 您正在重定向回同一页面?

标签: c# asp.net sql


【解决方案1】:

检查这一行
dataAccess.AddParameter("@Category_ID", DDCategories); 并替换为
dataAccess.AddParameter("@Category_ID", DDCategories.SelectedValue)

【讨论】:

  • 替换上面的代码确实可以让错误消失! (好消息),但它告诉我在 // Insert query "Incorrect syntax near the keyword 'INNER'." 中的查询有问题。
  • @MikeBertelsen - 插入中的 join+order by+where 看起来很奇怪。这不是复制粘贴错误吗?
  • @PHeiberg 哈哈,不,不,我实际上尝试过写,但我从未在 INSERT 和 UPDATE 查询中加入某些内容。老实说,我真的无法理解。
  • @MikeBertelsen - 为了插入新产品,您不需要类别中的任何数据,类别 ID 来自用户选择。在尝试在您的程序代码中使用它之前,摆脱连接并尝试查询分析器中的语句。这将为您节省大量时间。您不能以这种方式加入插入。如果您确实必须为 INSERT 连接数据,则必须先选择数据并将该数据传递给 INSERT。在 ORDER BY 之后也不能有 WHERE 子句。
  • @PHeiberg 哦,我明白了,我删除了 JOIN,它使错误消失了,尽管我现在得到“将 nvarchar 值 'Konfirmation' 转换为数据类型 int 时转换失败。”,其中 Konfirmation是 DropDownList 中的一个类别。附言。我喜欢你解释事情的简单易行的方式,赞叹。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
相关资源
最近更新 更多