Solved

Error loading parent combo box based on child combobox

Posted on 2010-08-18
24
758 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
Comment Utility
0
 

Author Comment

by:GRChandrashekar
Comment Utility
I need to know in C#
0
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
just check the selectedindex of submenugroup for not equals -1 the go for the mengroup binding.
0
 

Author Comment

by:GRChandrashekar
Comment Utility
Can you give me code example Puru1981
0
 
LVL 69

Expert Comment

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

Author Comment

by:GRChandrashekar
Comment Utility
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
Comment Utility
you have to check
if( cmbmenusubgroupid.SelectedIndex != -1)
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Get this error
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:puru1981
Comment Utility
int.Parse(cmbmenusubgroupid.SelectedValue.ToString())

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

Author Comment

by:GRChandrashekar
Comment Utility
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
Comment Utility
and with regard to your question
int.Parse(cmbmenusubgroupid.SelectedValue.ToString())

yes it is integer
0
 
LVL 9

Expert Comment

by:puru1981
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
can you send me the stack trace of the error
0
 

Author Comment

by:GRChandrashekar
Comment Utility
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
Comment Utility
please add a breakpoint on the line number 261. and see the value need to be converted.
0
 

Author Comment

by:GRChandrashekar
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows phone Development 11 27
c# string 6 39
Using MS Code on my Mac 6 44
bulid json format 3 16
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

19 Experts available now in Live!

Get 1:1 Help Now