【问题标题】:how to insert id of a databound dropdownlist of one table to another table如何将一个表的数据绑定下拉列表的 id 插入另一个表
【发布时间】:2012-01-29 23:08:55
【问题描述】:
  • 我有 2 个表,Product 和 ProductCategory
  • 1 个以编程方式将数据绑定到 ProductCategory 的下拉列表
  • 我正在开发 Visual Studio 2008,ASP.Net 表单
  • 用户可以创建新的 ProductCategory,即。 product_category_id 是自动递增的
  • 我需要做一个插入语句
  • 我有以下代码
  • 问题是,在显示 ProductCategory 的 product_category_name 时,如何确保在下拉列表中选择/未选择后将 ProductCategory 的“product_category_id”插入到 Product 的 product_category_id 中? (FK)

    Dim sql2 As String = "INSERT INTO Product (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)))

【问题讨论】:

    标签: .net sql-server visual-studio-2008 drop-down-menu


    【解决方案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 而不是下拉列表中显示的名称。

    【讨论】:

      【解决方案2】:

      如果由于某种原因您不想将 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,所以这次我想我会给出VB解决方案。

      【讨论】:

        猜你喜欢
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-19
        相关资源
        最近更新 更多