2012-01-28 47 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解决方案。

相关问题