Skip to content Skip to sidebar Skip to footer

Insert Integer When Dropdownlist Displays Strings As Datavaluefield

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: Di

Solution 1:

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"

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.

INSERTINTO 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

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

DECLARE@Product_Category_ID INTSELECT@Product_Category_ID = Product_Category_ID
FROM     Product_Category
WHERE    Product_Category_Name =@Product_Category_Name

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

Post a Comment for "Insert Integer When Dropdownlist Displays Strings As Datavaluefield"