当dropdownlist将字符串显示为datavaluefield时插入整数 [英] Insert integer when dropdownlist displays strings as datavaluefield

查看:50
本文介绍了当dropdownlist将字符串显示为datavaluefield时插入整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我正在Visual Studio 2008上开发我的应用程序
  • 我有4张桌子
  • 我有一个下拉列表(显示product_category_names)
  • 我需要执行一条插入语句
  • 我有以下代码:

  • i am developing my app on visual studio 2008
  • i have 4 tables
  • i have a dropdownlist (displays product_category_names)
  • i need to do an insert statement
  • i have the following code:

Dim sql2 As String =将产品(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_status,product_status,product_status @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)"

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.Parameters.Add(新SqlParameter("@ product_name",(txtProductName2.Text)))cmd.Parameters.Add(新SqlParameter("@ product_title",(txtProductTitle2.Text)))

cmd.Parameters.Add(New SqlParameter("@product_name", (txtProductName2.Text))) cmd.Parameters.Add(New SqlParameter("@product_title", (txtProductTitle2.Text)))

问题是,当下拉列表显示"product_category_names"而不是"product_category_id"时,如何将"product_category_id"(整数)插入表"Product"中?

Question is, how may I insert 'product_category_id' (an Integer) into the table 'Product', when the dropdownlist displays 'product_category_names' instead of 'product_category_id'?

有人可以给我看光吗?谢谢.

Can someone show me the light? Thanks.

推荐答案

假设您要在页面加载事件上绑定下拉列表,并使用product_Catogry表,那么最简单的方法是将product_category_ID绑定到下拉列表的value属性.这样,您可以在用户看到名称时以编程方式访问ID.

Assuming you are binding your dropdownlist on the page load event and using the product_Catogry table then The easiest way would be to bind product_category_ID to the value property of the dropdownlist. This way you can programatically access the ID while the user sees the name.

DropDownList.DataSource = [your product table]
DropDownList.DataTextField = "Proudct_Category_Name"
DropDownList.DataValueField = "Product_category_ID"

如果由于某种原因这不是一个选择,那么下面的SQL将在当前命令中起作用,只需确保将@Product_Category_Name(而不是@product_Category_Name)作为参数添加到SqlCommand中即可.

If this is not an option for whatever reason then the below SQL will work in your current command, just make sure you add @Product_Category_Name as a parameter to the SqlCommand instead of @product_Category_Name.

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
)    
SELECT  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
FROM    product_category
WHERE   product_category_Name = @Product_Category_Name

或者您可以简单地将其添加到当前SQL的开头:

Or you could simply add this to the start of your current SQL:

DECLARE  @Product_Category_ID INT
SELECT   @Product_Category_ID = Product_Category_ID
FROM     Product_Category
WHERE    Product_Category_Name = @Product_Category_Name

并继续插入...值().

And continue with the insert ... values ().

这篇关于当dropdownlist将字符串显示为datavaluefield时插入整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆