如何获取组合框所选值以转到数据库

时间:2018-05-01 13:17:46

标签: c# visual-studio-2015 combobox

我有

            if (Text == "Add Client") // TEXT IS THE FORM.TEXT
            {
                  newClient = new Client();
                stateList = StateDB.GetStateList();
                stateComboBox.DataSource = stateList;
                clientBindingSource.Clear();
                clientBindingSource.Add(newClient);

            }

这是我的属性

enter image description here

这里是插入语句

        SqlConnection connection = PRG299DB.GetConnection();
        string insertStatement =
            "INSERT INTO Client " +
              "(FirstName, LastName, BirthDate, StreetName, " +
            "City, State, ZipCode, CellPhone) " +
            "VALUES (@FirstName, @LastName, @BirthDate, @StreetName, " +
            "@City, @State, @ZipCode, @CellPhone);";
        SqlCommand insertCommand = new SqlCommand(insertStatement, 
        connection);
        insertCommand.Parameters.AddWithValue("@FirstName", 
        client.FirstName);
        insertCommand.Parameters.AddWithValue("@LastName", client.LastName);
        insertCommand.Parameters.AddWithValue("@BirthDate", client.BirthDate);
        insertCommand.Parameters["@BirthDate"].SqlDbType = SqlDbType.DateTime;
        insertCommand.Parameters.AddWithValue("@StreetName", client.StreetName);
        insertCommand.Parameters.AddWithValue("@City", client.City );
        insertCommand.Parameters.AddWithValue("@State", client.State);
        insertCommand.Parameters.AddWithValue("@ZipCode", client.ZipCode);
        if (client.CellPhone == null)
            insertCommand.Parameters.AddWithValue("@CellPhone", DBNull.Value);
        else
            insertCommand.Parameters.AddWithValue("@CellPhone", client.CellPhone);

        try
        {
            connection.Open();
            insertCommand.ExecuteNonQuery();
            string selectStatement =
                "SELECT IDENT_CURRENT('Client') FROM Client";
            SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
            int vendorID = Convert.ToInt32(selectCommand.ExecuteScalar());
            return vendorID;
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }

更新声明

        SqlConnection connection = PRG299DB.GetConnection();
        string updateStatement =
            "UPDATE Client SET " +
              "FirstName = @NewFirstName, " +
              "LastName = @NewLastName, " +
              "BirthDate = @NewBirthDate, " +
              "StreetName = @NewStreetName, " +
              "City = @NewCity, " +
              "State = @NewState, " +
              "ZipCode = @NewZipCode, " +
              "CellPhone = @NewCellPhone " +
            "WHERE ClientID = @OldClientID " +
              "AND FirstName = @OldFirstName " +
              "AND LastName = @OldLastName " +
              "AND Birthdate = @OldBirthDate " +
              "AND StreetName = @OldStreetName " +
              "AND City = @OldCity " +
              "AND State = @OldState " +
              "AND ZipCode = @OldZipCode " +
              "AND (CellPhone = @OldCellPhone " +
                  "OR CellPhone IS NULL AND @OldCellPhone IS NULL)";
        SqlCommand updateCommand = new SqlCommand(updateStatement, connection);
        updateCommand.Parameters.AddWithValue("@NewFirstName", newClient.FirstName);
        updateCommand.Parameters.AddWithValue("@NewLastName", newClient.LastName);
        updateCommand.Parameters.AddWithValue("@NewBirthDate", newClient.BirthDate);
        updateCommand.Parameters.AddWithValue("@NewStreetName", newClient.StreetName);
        updateCommand.Parameters.AddWithValue("@NewCity", newClient.City);
        updateCommand.Parameters.AddWithValue("@NewState", newClient.State);
        updateCommand.Parameters.AddWithValue("@NewZipCode", newClient.ZipCode);
        if (newClient.CellPhone == "")
            updateCommand.Parameters.AddWithValue("@NewCellPhone", DBNull.Value);
        else
            updateCommand.Parameters.AddWithValue("@NewCellPhone", newClient.CellPhone);

        updateCommand.Parameters.AddWithValue("@OldClientID", oldClient.ClientID);
        updateCommand.Parameters.AddWithValue("@OldFirstName", oldClient.FirstName);
        updateCommand.Parameters.AddWithValue("@OldLastName", oldClient.LastName);
        updateCommand.Parameters.AddWithValue("@OldBirthDate", oldClient.BirthDate);
        updateCommand.Parameters.AddWithValue("@OldStreetName", oldClient.StreetName);
        updateCommand.Parameters.AddWithValue("@OldCity", oldClient.City);
        updateCommand.Parameters.AddWithValue("@OldState", oldClient.State);
        updateCommand.Parameters.AddWithValue("@OldZipCode", oldClient.ZipCode);
        if (oldClient.CellPhone == "")
            updateCommand.Parameters.AddWithValue("@OldCellPhone", DBNull.Value);
        else
            updateCommand.Parameters.AddWithValue("@OldCellPhone", oldClient.CellPhone);

        try
        {
            connection.Open();
            int count = updateCommand.ExecuteNonQuery();
            if (count > 0)
                return true;
            else
                return false;
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }

我希望能够显示stateName的显示值,并将所选值设置为stateCode
   例如:stateName:South Carolina和stateCode:sc

错误是当我点击保存时。它不会使用所选值将其保存到数据库中:" sc"它保存了显示价值"南卡罗来纳州"

我将非常感谢提前感谢任何提示或提示

如果您需要更多代码,请参阅此链接

https://github.com/andakap99/ProjectPRG299DB/blob/master/WindowsFormsApplication1/frmAUI.cs

1 个答案:

答案 0 :(得分:0)

我需要添加一个看起来像这样的选定索引更改事件

    private void stateComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (newClient != null || newCompany != null || newSchool != null)
        {
            if (cliLVVisible)
            {
                newClient.State = (string)stateComboBox.SelectedValue;
            }
            else if (comLVVisible)
            {
                newCompany.State = (string)stateComboBox1.SelectedValue;
            }
            else if (schLVVisible)
            {
                newSchool.State= (string)stateComboBox2.SelectedValue;
            }
        }
    }
相关问题