2012-01-24 8 views
0

商品を追加するためのドロップダウンリストにカテゴリを表示しています。製品を追加する場合は、カテゴリを選択して製品を作成するか、以前の製品を更新する必要があります。DropDownListでのマッピングエラー

しかし、私の問題は、私は次のエラーを取得ことです:

No mapping exists from object type System.Web.UI.WebControls.DropDownList to a known managed provider native type.

データベースダイアグラム: 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

と交換してください? Response.Redirect(Request.Url.AbsolutePath); – Lloyd

+0

@Lloydは、すべての製品がリストされたページにリダイレクトされます。しかし、「更新」または「作成」をクリックしたときに上に表示されるエラーのために、動作しているかどうかはわかりません。 –

+0

同じページにリダイレクトしていますか? – Lloyd

答えて

0

チェックこのライン
dataAccess.AddParameter("@Category_ID", DDCategories); とは、あなたがこの声明を達成するためにアミングされている何
dataAccess.AddParameter("@Category_ID", DDCategories.SelectedValue)

+0

上記のコードを置き換えると、エラーが消えます! (すばらしいニュース)、しかし、それは私の中に私のクエリに間違ったことがあると私に言います//挿入クエリ "キーワード 'INNER'の近くの構文が間違っています。" –

+1

@MikeBertelsen - 挿入物のどこに+が並んでいて、奇妙に見えるか?それはコピー貼り間違いではありませんか? – PHeiberg

+0

@PHeiberg Haha、私は実際にそれを書こうとしましたが、INSERTおよびUPDATEクエリで何かをジョインしたことはありません。正直なところ私はそれを本当に理解できません。 –