简体   繁体   中英

Visual Studio Vb.net combo box source for updating

I have a form with a set of text boxes bound to a table data source eg. Cars. I am trying to update the table which works ok. However, one of the text boxes (fields) is a foreign key to CarType (int) and I would like the user to select from car types and then when they click save, it updates this value to the database. So, if they select Ford, the value 1 would be saved, VW, the value 2 would be saved etc. I have tried numerous things but I am just not getting it. can anyone please help

thanks very much

For the Car Type, use a DropDownList or RadioButtonList instead of a TextBox. Bind whichever control you choose to a separate lookup table which has CarTypeID and CarTypeDescription fields. Set the DataTextField property of the control to CarTypeDescription, the DataValueField property to CarTypeID and the SelectedValue property of the control to the field in your main data table where you want to save the ID against car. A bit like this:

<div>
    <asp:DropDownList ID="ddlCarType" runat="server" DataSourceID="SQLCarType" DataTextField="CarTypeDescription" DataValueField="CarTypeID" SelectedValue='<%# Bind("CarType")%>' />
</div>
<asp:SqlDataSource ID="SQLCarType" runat="server" ConnectionString="ConnectionStringHere" 
    SelectCommand="SELECT [CarTypeID], [CarTypeDescription] FROM CarType">
</asp:SqlDataSource>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM