Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 790
  • Last Modified:

Error loading parent combo box based on child combobox

I have two comboboxes menusubgroup and menugruop which is loaded during form load. First menusubgroup and then menugroup

At the same time, I am trying to filter mengroup combobox based on menusubgroupcombobox.

Now on form load, it goes to error because user has still not selected menusubgroup.

How do i over come this issue

private void Fillcmbmenugroupid()
        {
            try
            {
               
                var datasourcemenugroup = new EntityCollection(new MenugroupEntityFactory());
                filtermenusubgroup.Clear();
                filtermenusubgroup.Add(MenusubgroupFields.MenusubgroupId ==
                                       int.Parse(cmbmenusubgroupid.SelectedValue.ToString()));
                bucketmengroup.PredicateExpression.Add(filtermenusubgroup);
                bucketmengroup.Relations.Add(
                    MenugroupEntity.Relations.MenusubgroupEntityUsingMenugroupId);
                bucketmengroup.PredicateExpression.Add(MenugroupFields.Flag ==
                                                       StandardFlag.recordvalidflag);
                adaptermengroup.FetchEntityCollection(datasourcemenugroup, bucketmengroup, 0,
                                                      new SortExpression(
                                                          MenugroupFields.Description |
                                                          SortOperator.Ascending));
                cmbmenugroupid.DataSource = datasourcemenugroup;
                cmbmenugroupid.ValueMember = "MENUGROUPID";
                cmbmenugroupid.DisplayMember = "DECRIPTION";
                cmbmenugroupid.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                GlobalErrorHandler.LogMessage(ex.StackTrace);
            }
            finally
            {
                adaptermengroup.CloseConnection();
            }
        }

0
GRChandrashekar
Asked:
GRChandrashekar
  • 12
  • 9
  • 3
1 Solution
 
GRChandrashekarAuthor Commented:
I need to know in C#
0
 
puru1981Commented:
just check the selectedindex of submenugroup for not equals -1 the go for the mengroup binding.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GRChandrashekarAuthor Commented:
Can you give me code example Puru1981
0
 
Éric MoreauSenior .Net ConsultantCommented:
you do not know how to add () and ;
0
 
GRChandrashekarAuthor Commented:
First line after Try i added this

int? index = cmbmenusubgroupid.Items.IndexOf(cmbmenusubgroupid.SelectedValue.ToString());
                if (index <= 0) return;
Now
first time when it comes in loop it works
second time when it comes selected value is null and goes to exception Object reference not set to an instance of an object.
0
 
puru1981Commented:
you have to check
if( cmbmenusubgroupid.SelectedIndex != -1)
0
 
Éric MoreauSenior .Net ConsultantCommented:
>>This forum is meant to help every developer including beginner
I totally agree. I linked you to an article exploring 3 methods of binding 2 combos together. The article may show VB.Net code but the downloadable demo contains C#.
0
 
puru1981Commented:
Rightly said emoreau.

I have visited the link and spotted the same thing which i was trying to explain him because if you can use dataset and datarelation it would be easier but when i see the code written above i considered to solve the problem this way because the code is not letting me know is he using datatable or something else if he is not datatable and dataset the code is of no use for him.
0
 
GRChandrashekarAuthor Commented:
Now I have altered like this. So how do i get the desired output

var adapter = new DataAccessAdapter();
                bucket = menugroupsubgroup.GetRelationInfo();
                bucket.PredicateExpression.Add(MenusubgroupFields.Flag == StandardFlag.recordvalidflag);
                ISortExpression sorter =
                    new SortExpression(MenusubgroupFields.Description | SortOperator.Ascending);
                adapter.FetchTypedList(menugroupsubgroup.GetFieldsInfo(), menugroupsubgroup, bucket, 0,
                                       sorter,false);
                cmbmenusubgroupid.DataSource = menugroupsubgroup;
                cmbmenusubgroupid.ValueMember = "MENUSUBGROUPID";
                cmbmenusubgroupid.DisplayMember = "MENUSUBGROUPDESCRIPTION";
                cmbmenusubgroupid.SelectedIndex = -1;
               

                cmbmenugroupid.BindingContext = new BindingContext();
                cmbmenugroupid.DataSource = menugroupsubgroup;
                cmbmenugroupid.ValueMember = "MENUGROUPID";
                cmbmenugroupid.DisplayMember = "MENUGROUPDESCRIPTION";
                cmbmenugroupid.SelectedIndex = -1;
               
0
 
puru1981Commented:
Please attach the code below i have implemented it in your previous version of code.

sorry, I am not understanding the code you have provided this time so implemented first one.
private void Fillcmbmenugroupid()
        {
            try
            {
               
                var datasourcemenugroup = new EntityCollection(new MenugroupEntityFactory());
                filtermenusubgroup.Clear();
				if(cmbmenusubgroupid.SelectedIndex!=1)
				{
                filtermenusubgroup.Add(MenusubgroupFields.MenusubgroupId ==
                                       int.Parse(cmbmenusubgroupid.SelectedValue.ToString()));
                bucketmengroup.PredicateExpression.Add(filtermenusubgroup);
                bucketmengroup.Relations.Add(
                    MenugroupEntity.Relations.MenusubgroupEntityUsingMenugroupId);
                bucketmengroup.PredicateExpression.Add(MenugroupFields.Flag ==
                                                       StandardFlag.recordvalidflag);
                adaptermengroup.FetchEntityCollection(datasourcemenugroup, bucketmengroup, 0,
                                                      new SortExpression(
                                                          MenugroupFields.Description |
                                                          SortOperator.Ascending));
                cmbmenugroupid.DataSource = datasourcemenugroup;
                cmbmenugroupid.ValueMember = "MENUGROUPID";
                cmbmenugroupid.DisplayMember = "DECRIPTION";
                cmbmenugroupid.SelectedIndex = -1;
				}
            }
            catch (Exception ex)
            {
                GlobalErrorHandler.LogMessage(ex.StackTrace);
            }
            finally
            {
                adaptermengroup.CloseConnection();
            }
        }

Open in new window

0
 
GRChandrashekarAuthor Commented:
Get this error
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
0
 
puru1981Commented:
int.Parse(cmbmenusubgroupid.SelectedValue.ToString())

can you confirm that is cmbmenusubgroupid.SelectedValue of type integer
0
 
GRChandrashekarAuthor Commented:
Well Let me re-explain

In emoreau link, it is shown that first country is selected and then state is displayed. What I am trying to do is other way round. First state is selected based on which country is displayed. In my case it is menusubgroup first and then menugroup. Tables struc is as follows

CREATE TABLE MENUSUBGROUP (
    MENUSUBGROUP_ID NUMBER(5) CONSTRAINT NN_MENSUBGRP_1 NOT NULL,
    DESCRIPTION VARCHAR2(100) CONSTRAINT NN_MENSUBGRP_2 NOT NULL,
    MENUGROUP_ID NUMBER(5) CONSTRAINT NN_MENSUBGRP_3 NOT NULL,
    FLAG NUMBER(1) DEFAULT 0 CONSTRAINT NN_MENSUBGRP_4 NOT NULL,
    CREATEDDATE DATE CONSTRAINT NN_MENSUBGRP_5 NOT NULL,
    CREATEDUSER_ID NUMBER(5) CONSTRAINT NN_MENSUBGRP_6 NOT NULL,
    MODIFIEDDATE DATE,
    MODIFIEDUSER_ID NUMBER(5),
    CONSTRAINT PK_MENSUBGRP_0 PRIMARY KEY (MENUSUBGROUP_ID),
    CONSTRAINT TUC_MENSUBGRP UNIQUE (FLAG, MENUGROUP_ID, DESCRIPTION, MENUSUBGROUP_ID)
);


CREATE TABLE MENUGROUP (
    MENUGROUP_ID NUMBER(5) CONSTRAINT NN_MENGRP_1 NOT NULL,
    DESCRIPTION VARCHAR2(100) CONSTRAINT NN_MENGRP_2 NOT NULL,
    FLAG NUMBER(1) DEFAULT 0 CONSTRAINT NN_MENGRP_3 NOT NULL,
    CREATEDDATE DATE CONSTRAINT NN_MENGRP_4 NOT NULL,
    CREATEDUSER_ID NUMBER(5) CONSTRAINT NN_MENGRP_5 NOT NULL,
    MODIFIEDDATE DATE,
    MODIFIEDUSER_ID NUMBER(5),
    CONSTRAINT PK_MENGRP_0 PRIMARY KEY (MENUGROUP_ID),
    CONSTRAINT TUC_MENGRP UNIQUE (FLAG, DESCRIPTION, MENUGROUP_ID)
);


Let us forget the previous code. Can you please help writing code for this
0
 
GRChandrashekarAuthor Commented:
and with regard to your question
int.Parse(cmbmenusubgroupid.SelectedValue.ToString())

yes it is integer
0
 
puru1981Commented:
please try the code below. i haven't loaded the table with data i think you can do this for me.

        DataSet ds = new DataSet();
        DataTable dt = new DataTable("menugroup");
        dt.Columns.Add(new DataColumn("MENUGROUP_ID",typeof(int)));
        dt.Columns.Add(new DataColumn("DESCRIPTION", typeof(string)));
        dt.Columns.Add(new DataColumn("FLAG", typeof(int)));
        dt.Columns.Add(new DataColumn("CREATEDDATE", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("CREATEDUSER_ID", typeof(int)));
        dt.Columns.Add(new DataColumn("MODIFIEDDATE", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("MODIFIEDUSER_ID", typeof(int)));
        

        DataTable dt1 = new DataTable("submenugroup");
        dt1.Columns.Add(new DataColumn("MENUSUBGROUP_ID", typeof(int)));
        dt1.Columns.Add(new DataColumn("DESCRIPTION", typeof(string)));
        dt1.Columns.Add(new DataColumn("MENUGROUP_ID", typeof(int)));
        dt1.Columns.Add(new DataColumn("FLAG", typeof(int)));
        dt1.Columns.Add(new DataColumn("CREATEDDATE", typeof(DateTime)));
        dt1.Columns.Add(new DataColumn("CREATEDUSER_ID", typeof(int)));
        dt1.Columns.Add(new DataColumn("MODIFIEDDATE", typeof(DateTime)));
        dt1.Columns.Add(new DataColumn("MODIFIEDUSER_ID", typeof(int)));
        ds.Tables.Add(dt);
        ds.Tables.Add(dt1);
        DataRelation datarelation = new DataRelation("menugrp", dt.Columns["MENUGROUP_ID"], dt1.Columns["MENUGROUP_ID"]);
        ds.Relations.Add(datarelation);

        cmbmenusubgroupid.DataSource = ds;
        cmbmenusubgroupid.DisplayMember = "submenugroup.DESCRIPTION";
        cmbmenusubgroupid.ValueMember = "MENUSUBGROUP_ID";
        cmbmenugroupid.DataSource = ds;
        cmbmenugroupid.DisplayMember = "submenugroup.menugrp.DESCRIPTION";
        cmbmenugroupid.ValueMember = "MENUGROUP_ID";

Open in new window

0
 
GRChandrashekarAuthor Commented:
Can you help me check this itself since i am using RAD tools and I can write the code directly

int.Parse(cmbmenusubgroupid.SelectedValue.ToString())

0
 
GRChandrashekarAuthor Commented:
I meant filtering is not a problem with my code it is working fine.

Only issue is on form load where I get an error
error Message: at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
BUT ONCE FORM IS LOADED IT WORKS CORRECTLY

I get same error by adding line if(cmbmenusubgroupid.SelectedIndex!=1)
0
 
puru1981Commented:
can you send me the stack trace of the error
0
 
GRChandrashekarAuthor Commented:
Here it is

Error Message:   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.Int32.Parse(String s)
   at ClubCentric.Master.MasterMenu.Fillcmbmenugroupid() in D:\LLBLGenProjectSourceBackup\ProjectLive\ClubCentric\Master\MasterMenu.cs:line 261
0
 
puru1981Commented:
please add a breakpoint on the line number 261. and see the value need to be converted.
0
 
GRChandrashekarAuthor Commented:
On form load it returns "{ClubCentricHelper.EntityClasses.MenusubgroupEntity}"

After load, when i select first combobox, it returns 3 or what ever is selected. so the problem again is the same i said only on form load for first time
0
 
GRChandrashekarAuthor Commented:
That means cmbmenusubgroupid_SelectedIndexChanged is getting fired while loading cmbmenusubgroupid. but again how to cover come this ?
0
 
puru1981Commented:
just check for the selected index =-1 again in the cmbmenusubgroupid_SelectedIndexChanged it will solve your problem

this is occuring because you are setting the selectedindex to -1 after setting the datasource.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 12
  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now