如何将一个表的数据绑定DropDownList中的ID到另一个表 [英] how to insert id of a databound dropdownlist of one table to another table
问题描述
- 在我有2个表,产品和放大器;产品分类
- 1的DropDownList被编程数据绑定到产品分类
- 在我工作的Visual Studio 2008中,ASP.Net形式
- 用户可以创建新的产品分类,即。 product_category_id自动递增
- 我需要做一个INSERT语句
- 我有以下的code
-
问题是,我怎么可能保证产品分类的product_category_id'插入产品product_category_id选择/不选择在DropDownList中后,在显示产品分类的product_category_name? (FKS)
昏暗SQL2的String =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(新的SqlParameter(@ product_category_id,(ddlProductCategoryName2.selectedValue)))
cmd.Parameters.Add(新的SqlParameter(@ product_category_name,(ddlProductCategoryName2.SelectedValue)))
cmd.Parameters.Add(新的SqlParameter(@ PRODUCT_NAME,(txtProductName2.Text)))
cmd.Parameters.Add(新的SqlParameter(@ PRODUCT_TITLE,(txtProductTitle2.Text)))
cmd.Parameters.Add(新的SqlParameter(@ product_desc,(txtProductDescription2.Text)))
cmd.Parameters.Add(新的SqlParameter(@ product_author,(txtProductAuthor2.Text)))
cmd.Parameters.Add(新的SqlParameter(@ product_author_age,(ddlProductAuthorAge2.SelectedValue)))
cmd.Parameters.Add(新的SqlParameter(@ product_author_desc,(txtProductAuthorDesc2.Text)))
据我可以告诉没有理由你的SQL / VB将无法正常工作,需要的,你只需要更改绑定的下拉列表略。在页面加载方法调用一个类似于下面的(我不得不假设你的列名):
昏暗的适配器作为新的SqlDataAdapter(SELECT * FROM产品分类,[的ConnectionString])
暗淡表作为新的DataTable()
adapter.fill(表)
ddlProductCategoryName2.DataSource =表
ddlProductCategoryName2.DataValueField =Product_Category_ID
ddlProductCategoryName2.DataTextField =Product_Category_Name
这将意味着
ddlProductCategoryName2.selectedValue
将返回product_category_ID而非在下拉列表中显示的名称。
- I have 2 tables, Product & ProductCategory
- 1 dropdownlist that is databound programmatically to ProductCategory
- Am working on Visual Studio 2008, ASP.Net Forms
- Users may create new ProductCategory, ie. product_category_id is auto-incremented
- I need to do an insert statement
- I have the following code
Question is, how may i ensure that the 'product_category_id' of ProductCategory is inserted into product_category_id of Product after a selection/no selection at the dropdownlist, while displaying product_category_name of ProductCategory? (FKs)
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
'the following statement is incorrect i believe?
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)))
As far as I can tell there is no reason your SQL/VB won't work as required, you just need to change the databinding on the drop down list slightly. In the page load method call something akin to the following (I have had to assume your column names):
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"
This will mean that
ddlProductCategoryName2.selectedValue
will return the product_category_ID rather than the name displayed in the drop down list.
这篇关于如何将一个表的数据绑定DropDownList中的ID到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!