Solved

Error loading parent combo box based on child combobox

Posted on 2010-08-18
24
764 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
  • 12
  • 9
  • 3
24 Comments
 
LVL 69

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
 

Author Comment

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

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 69

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now