Solved

Error loading parent combo box based on child combobox

Posted on 2010-08-18
24
774 Views
Last Modified: 2012-05-10
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
Comment
Question by:GRChandrashekar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 3
24 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33463855
0
 

Author Comment

by:GRChandrashekar
ID: 33463884
I need to know in C#
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33463888
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!

 

Author Comment

by:GRChandrashekar
ID: 33463902
Can you give me code example Puru1981
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33463905
you do not know how to add () and ;
0
 

Author Comment

by:GRChandrashekar
ID: 33464256
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
 
LVL 9

Expert Comment

by:puru1981
ID: 33464978
you have to check
if( cmbmenusubgroupid.SelectedIndex != -1)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33465004
>>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
 
LVL 9

Expert Comment

by:puru1981
ID: 33465910
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
 

Author Comment

by:GRChandrashekar
ID: 33473527
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
 
LVL 9

Expert Comment

by:puru1981
ID: 33473839
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
 

Author Comment

by:GRChandrashekar
ID: 33474487
Get this error
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33476219
int.Parse(cmbmenusubgroupid.SelectedValue.ToString())

can you confirm that is cmbmenusubgroupid.SelectedValue of type integer
0
 

Author Comment

by:GRChandrashekar
ID: 33490410
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
 

Author Comment

by:GRChandrashekar
ID: 33490575
and with regard to your question
int.Parse(cmbmenusubgroupid.SelectedValue.ToString())

yes it is integer
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33490663
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
 

Author Comment

by:GRChandrashekar
ID: 33490690
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
 

Author Comment

by:GRChandrashekar
ID: 33490703
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
 
LVL 9

Expert Comment

by:puru1981
ID: 33490741
can you send me the stack trace of the error
0
 

Author Comment

by:GRChandrashekar
ID: 33490767
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
 
LVL 9

Expert Comment

by:puru1981
ID: 33490857
please add a breakpoint on the line number 261. and see the value need to be converted.
0
 

Author Comment

by:GRChandrashekar
ID: 33490867
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
 

Author Comment

by:GRChandrashekar
ID: 33490898
That means cmbmenusubgroupid_SelectedIndexChanged is getting fired while loading cmbmenusubgroupid. but again how to cover come this ?
0
 
LVL 9

Accepted Solution

by:
puru1981 earned 500 total points
ID: 33490952
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question