使用DropDownList映射错误

时间:2012-01-24 08:08:41

标签: c# asp.net sql

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

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

  

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

数据库图表: Database Diagram Picture

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

1 个答案:

答案 0 :(得分:0)

检查此行
dataAccess.AddParameter("@Category_ID", DDCategories); 并用
替换 dataAccess.AddParameter("@Category_ID", DDCategories.SelectedValue)