如何将带有组合框值的新组件添加到数据库中? [英] How can I add new component with a combo box values into database?

查看:69
本文介绍了如何将带有组合框值的新组件添加到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- 这是我的添加功能,尝试使用组合框添加新组件,但它总是失败,因为无法在cbbDivision中获取数据,



- This is my add function to try to add new component with a combo box but it always fail because can't get data in cbbDivision,

private bool save()
        {
            try
            {
                if (txtMoney != null && txtSubject != null && txtMemo1 != null
                        && txtTitle != null && txtAmount != null && txtMemo2 != null)
                {
                    DateTime date;
                    string division = "";
                    float money;
                    string subject = "";
                    string memo1 = "";
                    string title = "";
                    int amount;
                    string memo2 = "";

                    date = DateTime.Parse(dtDate.Value.ToString());
                    division = cbbDivision.Text.ToString();
                    money = float.Parse(txtMoney.Text.ToString());
                    subject = txtSubject.Text.ToString();
                    memo1 = txtMemo1.Text.ToString();
                    title = txtTitle.Text.ToString();
                    amount = Int32.Parse(txtAmount.Text.ToString());
                    memo2 = txtMemo2.Text.ToString();

                    HanbiDBDataContext db = new HanbiDBDataContext();
                    DataManage data = new DataManage();

                    data.Date = date;
                    data.divId = Int32.Parse(cbbDivision.GetItemText(cbbDivision.SelectedItem));
                    data.Money = money;
                    data.Subject = subject;
                    data.Memo1 = memo1;
                    data.Title = title;
                    data.Amount = amount;
                    data.Memo2 = memo2;

                    db.DataManages.InsertOnSubmit(data);
                    db.SubmitChanges();

                    XtraMessageBox.Show("Add Success!");
                }
                return true;
            }
            catch (Exception)
            {
                XtraMessageBox.Show("Please insert information!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
        }





- 这是我的数据库:





- This is my DB:

CREATE TABLE DataManage
(
	Id int primary key identity (1, 1), 
	[Date] datetime,
	[Subject] varchar(200),
	Title varchar(200),
	[Money] float,
	Memo1 varchar(200),
	Memo2 varchar(200),
	Amount int,
	divId int foreign key references Division(dId)
)







CREATE TABLE Division
(
	dId int primary key identity(1, 1),
	dName varchar(10)
)





- 我正在使用LINQ连接数据库并通过GridControl显示数据。我通过使用dataBindingSource得到Division表的值





- I'm using LINQ to connect to database and show data by GridControl. And I get values of Division table by using dataBindingSource

private void AddNew_Load(object sender, EventArgs e)
        {
            HanbiDBDataContext db = new HanbiDBDataContext();

            divisionBindingSource.DataSource = db.Divisions.ToList();
        }





我的尝试:



我试图手动在组合框中添加新值并再次测试,但仍然出错。有人可以帮我解决吗?



What I have tried:

I was tried to add new values in combo box by hand and test again but it still error. Some one can help me to solve it pls?

推荐答案

我看到你的cbbDivision有自己的绑定源,这很好。通常,您不希望假设cbb中显示的文本是您要存储的内容。您想要实际存储ID值。设置cbbDivision时,是否将'value'参数设置为除法的dId,将'display'参数设置为dName?如果是这样,那么你想要做的是从cbb获取所选项目并使用它来获取你想要在记录中插入的ID。您不应该使用Int32.parse它,因为ID部分应该已经是数据库中的ID。

I see your cbbDivision has its own binding source, that's good. Typically you would not want to assume that the text showing in the cbb is what you want to store. You want to actually store the ID value. When you set up the cbbDivision did you set the 'value' param to the dId of the division and the 'display' param to dName? If so, then what you want to do is get the selected item from the cbb and use that to get the ID you want to insert in the record. You should not have to Int32.parse it since the ID part should already be the ID from the database.
// cast selected item as Division and insert id
data.divId = ((Division)cbbDivisions.getSelectedItem()).dId

HTH,Mike


这篇关于如何将带有组合框值的新组件添加到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆