保存ID而不是名称

本文关键字:ID 保存 | 更新日期: 2023-09-27 18:15:43

我有其中存储的国家名称。但我想存储的是国家ID。

下面是我的代码:-
if (!string.IsNullOrEmpty(mkey))
        {  
          insertUpdateQuery = "UPDATE B_Order_new SET ShipName = :ShipName, ShipCity = :ShipCity, ShipAddress = :ShipAddress, " +
                                 "ShipCountry = :ShipCountry, OrderDate = :OrderDate, Sent = :Sent where MKEY = :MKEY";
        }
        else
        {
            insertUpdateQuery = "INSERT INTO B_Order_new (MKEY, ShipName, ShipCity, ShipAddress, ShipCountry, OrderDate, Sent) " +
                                 "VALUES(:MKEY, :ShipName, :ShipCity, :ShipAddress, :ShipCountry, :OrderDate, :Sent)";
        }
        OracleCommand cmd = new OracleCommand(insertUpdateQuery, myConn);
        var orderedOn = DateTime.ParseExact(orderDate, "dd/MM/yyyy", null);
        cmd.Parameters.Add("MKEY", OracleType.Number).Value = decimal.Parse(mkey).ToString();
        cmd.Parameters.Add("ShipName", OracleType.VarChar).Value = shipName;
        cmd.Parameters.Add("ShipCity", OracleType.VarChar).Value = shipCity;
        cmd.Parameters.Add("ShipAddress", OracleType.VarChar).Value = shipAddress;
        cmd.Parameters.Add("ShipCountry", OracleType.VarChar).Value = shipCountry;
        cmd.Parameters.Add("OrderDate", OracleType.DateTime).Value = orderedOn;
        cmd.Parameters.Add("Sent", OracleType.Char).Value = true;
}

也看我的select查询,我是如何绑定它的。

<asp:SqlDataSource ID="sdCombo" runat="server" ConnectionString="<%$ ConnectionStrings:OracleConn %>"
    ProviderName="<%$ ConnectionStrings:OracleConn.ProviderName %>" SelectCommand="SELECT COUNTRY_ID, COUNTRY_NAME from country_test">
</asp:SqlDataSource>

我要绑定下拉列表的gridview

<obout:ComboBox runat="server" ID="ComboBoxEditor" FolderStyle="ComboBox/styles/premiere_blue"
            Width="100%" Height="150" MenuWidth="175" DataSourceID="sdCombo" DataTextField="COUNTRY_NAME"
            DataValueField="COUNTRY_NAME" OpenOnFocus="false">
            <ClientSideEvents OnBlur="persistFieldValue" OnOpen="ComboBox_Open" />
        </obout:ComboBox>

保存ID而不是名称

将ComboBox的DataValueField="COUNTRY_NAME"替换为DataValueField="COUNTRY_ID",这将加载country_Id到ComboBox,然后您可以将ComboBox的SelectedValue分配给shipCountry变量,用于存储国家Id。

编辑:您可以将其分配为:

shipCountry=ComboBoxEditor.SelectedValue;
cmd.Parameters.Add("ShipCountry", OracleType.VarChar).Value = shipCountry;

把你的ComboBox的脚本改成如下:

<obout:ComboBox runat="server" ID="ComboBoxEditor" FolderStyle="ComboBox/styles/premiere_blue" Width="100%" Height="150" MenuWidth="175" DataSourceID="sdCombo" 
DataTextField="COUNTRY_NAME" DataValueField="COUNTRY_ID" OpenOnFocus="false"> 
<ClientSideEvents OnBlur="persistFieldValue" OnOpen="ComboBox_Open" /> </obout:ComboBox>