2012-01-28 46 views
1
  • 我有2個表,產品&產品分類是編程數據綁定到產品分類
  • 我工作的Visual Studio 2008中,ASP
  • 1下拉列表。網絡形式
  • 用戶可以創建新的ProductCategory,即。 product_category_id自動遞增
  • 我需要做一個INSERT語句
  • 我有以下代碼
  • 問題是,如何我可以保證產品分類的「product_category_id」被選擇後插入產品的product_category_id /在下拉列表中沒有選擇,同時顯示ProductCategory的product_category_name? (FKS)如何插入一個表的數據綁定下拉列表的ID到另一個表

    昏暗SQL2作爲字符串=「INSERT INTO產品(product_category_id,PRODUCT_NAME,PRODUCT_TITLE,product_desc,product_author,product_author_age,product_author_desc,product_other_detail,product_dimension1,product_dimension2,PRODUCT_PRICE,product_institution,product_status,product_delivery_time)VALUES(@product_category_id, @product_name,@product_title,@product_desc,@product_author,@product_author_age,@product_author_desc,@product_other_detail,@ product_dimension1,@ product_dimension2,@product_price,@product_institution,@product_status,@product_delivery_time)」

    cmd.CommandText = SQL2 cmd.CommandType = CommandType.Text

'我相信下面的陳述是不正確的?

cmd.Parameters.Add(New SqlParameter("@product_category_id", (ddlProductCategoryName2.selectedValue))) 
         cmd.Parameters.Add(New SqlParameter("@product_category_name", (ddlProductCategoryName2.SelectedValue))) 
         cmd.Parameters.Add(New SqlParameter("@product_name", (txtProductName2.Text))) 
         cmd.Parameters.Add(New SqlParameter("@product_title", (txtProductTitle2.Text))) 
         cmd.Parameters.Add(New SqlParameter("@product_desc", (txtProductDescription2.Text))) 
         cmd.Parameters.Add(New SqlParameter("@product_author", (txtProductAuthor2.Text))) 
         cmd.Parameters.Add(New SqlParameter("@product_author_age", (ddlProductAuthorAge2.SelectedValue))) 
         cmd.Parameters.Add(New SqlParameter("@product_author_desc", (txtProductAuthorDesc2.Text))) 

回答

1

據我可以告訴有沒有理由你的SQL /所需VB將無法正常工作,你只需要稍微改變的下拉列表中的數據綁定。在頁面加載方法調用一個類似於以下的(我不得不假設您的列名):

dim adapter as new SqlDataAdapter("SELECT * FROM ProductCategory", [connectionstring]) 
dim table as new DataTable() 
adapter.fill(table) 
ddlProductCategoryName2.DataSource = table 
ddlProductCategoryName2.DataValueField = "Product_Category_ID" 
ddlProductCategoryName2.DataTextField = "Product_Category_Name" 

這將意味着

ddlProductCategoryName2.selectedValue 

將返回product_category_ID而不是顯示的名稱下拉列表。

0

由於某種原因,您不希望將product_Category_ID數據綁定到下拉列表中,因此我發佈了另一個替代方案。將以下函數添加到頁面後面的代碼中。

Private Function GetProductCategoryID(ByVal productCategoryName As String) As Int32 

    Dim id As Int32 
    Dim connectionString As String = "" 'your connection string 
    Using connection As New SqlConnection(connectionString) 
     connection.Open() 
     Using Adapter As New SqlDataAdapter("SELECT Product_Category_ID FROM ProductCategory WHERE Product_Category_Name = @Name", connection) 
      Dim table As New DataTable() 
      Adapter.SelectCommand.Parameters.AddWithValue("@Name", productCategoryName) 
      Adapter.Fill(table) 
      If (table.Rows.Count = 0) Then 
       Using command As New SqlCommand("INSERT ProductCategory (Proudct_Category_Name) VALUES (@Name) SELECT SCOPE_IDENITTY()", connection) 
        command.Parameters.AddWithValue("@Name", productCategoryName) 
        id = Convert.ToInt32(command.ExecuteScalar()) 
       End Using 
      Else 
       id = Convert.ToInt32(table.Rows(0).Item(0)) 
      End If 
     End Using 

     connection.Close() 
    End Using 

    Return id 
End Function 

然後你只需要改變一個行當前的代碼:

cmd.Parameters.Add(New SqlParameter("@product_category_id", (ddlProductCategoryName2.selectedValue))) 

cmd.Parameters.Add(New SqlParameter("@product_category_id", GetProductCategoryID(ddlProductCategoryName2.selectedValue))) 

沒有理由這個功能背後的邏輯不能做完全在SQL中(即如果表中存在所選字符串的條目,則使用該ID,否則插入並獲取ID),但我已經給出SQL解決方案here,所以想到我wo這次給VB解決方案。

相關問題