Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
- 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)
)


- 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?
Posted
Updated 2-Jul-18 5:51am
Comments
F-ES Sitecore 28-Jun-18 5:17am    
What happens when you step through in the debugger? How far does the code get? What path does it take? Are you getting exceptions thrown?
Dungg Lee 28-Jun-18 5:38am    
When I run it, it always go to catch(ex) that mean the data in combobox is null, because when I delete this line:

data.divId = Int32.Parse(cbbDivision.GetItemText(cbbDivision.SelectedItem));

It can add new component.
F-ES Sitecore 28-Jun-18 5:45am    
You don't know why the exception is being thrown because you're ignoring the exception details. cboDivision might be null, SelectedItem might be null, or maybe the Parse failed because the item text can't be converted to an int. It is pointless guessing and making assumptions, you need to learn to debug your code so you can know for sure what the problem is and go from there.

1 solution

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.
C#
// cast selected item as Division and insert id
data.divId = ((Division)cbbDivisions.getSelectedItem()).dId
HTH, Mike
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900