3

我面临一个关于数据库和组合框的问题。我有一个表名“站”,表内是列、站名和座位。列席设置为“BIT”。我使用组合框让用户选择“是”或“否”,但如何将它们转换为 BIT 并存储到数据库中。这是我的创建代码。

private void btnCreate_Click(object sender, EventArgs e)
    {
        using (testEntities Setupctx = new testEntities())
        {
            string[] stations = StationNameList();
            station creStation = new station();
            creStation.Station1 = txtStation.Text;
            creStation.Seat = cbSeats.SelectedItem();
            if (stations.Contains(txtStation.Text))
            {
                MessageBox.Show("This Station is already been created. Please enter a new Station.");
            }
            else
            {
                Setupctx.stations.AddObject(creStation);
                Setupctx.SaveChanges();
                txtStation.Text = "";
                cbSeats.SelectedIndex = -1;
                MessageBox.Show("New Station Has Been Created.");
            }
        }
    }

错误在这里:

creStation.Seat = cbSeats.SelectedItem();

他们提示我无法从字符串转换为布尔值。有什么帮助吗?

4

2 回答 2

6

如同

creStation.Seat = cbSeats.SelectedValue=="Yes";
于 2012-07-18T06:54:50.993 回答
0
bool.TryParse(cbSeats.SelectedValue, out creStation.Seat);
于 2012-07-18T07:11:12.550 回答