GRChandrashekar
asked on
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(Men usubgroupF ields.Menu subgroupId ==
int.Parse(cmbmenusubgroupi d.Selected Value.ToSt ring()));
bucketmengroup.PredicateEx pression.A dd(filterm enusubgrou p);
bucketmengroup.Relations.A dd(
MenugroupEntity.Relations. Menusubgro upEntityUs ingMenugro upId);
bucketmengroup.PredicateEx pression.A dd(Menugro upFields.F lag ==
StandardFlag.recordvalidfl ag);
adaptermengroup.FetchEntit yCollectio n(datasour cemenugrou p, bucketmengroup, 0,
new SortExpression(
MenugroupFields.Descriptio n |
SortOperator.Ascending));
cmbmenugroupid.DataSource = datasourcemenugroup;
cmbmenugroupid.ValueMember = "MENUGROUPID";
cmbmenugroupid.DisplayMemb er = "DECRIPTION";
cmbmenugroupid.SelectedInd ex = -1;
}
catch (Exception ex)
{
GlobalErrorHandler.LogMess age(ex.Sta ckTrace);
}
finally
{
adaptermengroup.CloseConne ction();
}
}
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(Men
int.Parse(cmbmenusubgroupi
bucketmengroup.PredicateEx
bucketmengroup.Relations.A
MenugroupEntity.Relations.
bucketmengroup.PredicateEx
StandardFlag.recordvalidfl
adaptermengroup.FetchEntit
new SortExpression(
MenugroupFields.Descriptio
SortOperator.Ascending));
cmbmenugroupid.DataSource = datasourcemenugroup;
cmbmenugroupid.ValueMember
cmbmenugroupid.DisplayMemb
cmbmenugroupid.SelectedInd
}
catch (Exception ex)
{
GlobalErrorHandler.LogMess
}
finally
{
adaptermengroup.CloseConne
}
}
check http://www.emoreau.com/Entries/Articles/2007/01/Setting-a-masterdetail-relationship-between-two-ComboBox.aspx
ASKER
I need to know in C#
just check the selectedindex of submenugroup for not equals -1 the go for the mengroup binding.
ASKER
Can you give me code example Puru1981
you do not know how to add () and ;
ASKER
First line after Try i added this
int? index = cmbmenusubgroupid.Items.In dexOf(cmbm enusubgrou pid.Select edValue.To String());
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.
int? index = cmbmenusubgroupid.Items.In
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.
you have to check
if( cmbmenusubgroupid.Selected Index != -1)
if( cmbmenusubgroupid.Selected
>>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#.
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#.
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.
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.
ASKER
Now I have altered like this. So how do i get the desired output
var adapter = new DataAccessAdapter();
bucket = menugroupsubgroup.GetRelat ionInfo();
bucket.PredicateExpression .Add(Menus ubgroupFie lds.Flag == StandardFlag.recordvalidfl ag);
ISortExpression sorter =
new SortExpression(Menusubgrou pFields.De scription | SortOperator.Ascending);
adapter.FetchTypedList(men ugroupsubg roup.GetFi eldsInfo() , menugroupsubgroup, bucket, 0,
sorter,false);
cmbmenusubgroupid.DataSour ce = menugroupsubgroup;
cmbmenusubgroupid.ValueMem ber = "MENUSUBGROUPID";
cmbmenusubgroupid.DisplayM ember = "MENUSUBGROUPDESCRIPTION";
cmbmenusubgroupid.Selected Index = -1;
cmbmenugroupid.BindingCont ext = new BindingContext();
cmbmenugroupid.DataSource = menugroupsubgroup;
cmbmenugroupid.ValueMember = "MENUGROUPID";
cmbmenugroupid.DisplayMemb er = "MENUGROUPDESCRIPTION";
cmbmenugroupid.SelectedInd ex = -1;
var adapter = new DataAccessAdapter();
bucket = menugroupsubgroup.GetRelat
bucket.PredicateExpression
ISortExpression sorter =
new SortExpression(Menusubgrou
adapter.FetchTypedList(men
sorter,false);
cmbmenusubgroupid.DataSour
cmbmenusubgroupid.ValueMem
cmbmenusubgroupid.DisplayM
cmbmenusubgroupid.Selected
cmbmenugroupid.BindingCont
cmbmenugroupid.DataSource = menugroupsubgroup;
cmbmenugroupid.ValueMember
cmbmenugroupid.DisplayMemb
cmbmenugroupid.SelectedInd
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.
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();
}
}
ASKER
Get this error
at System.Number.StringToNumb er(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.StringToNumb
int.Parse(cmbmenusubgroupi d.Selected Value.ToSt ring())
can you confirm that is cmbmenusubgroupid.Selected Value of type integer
can you confirm that is cmbmenusubgroupid.Selected
ASKER
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
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
ASKER
and with regard to your question
int.Parse(cmbmenusubgroupi d.Selected Value.ToSt ring())
yes it is integer
int.Parse(cmbmenusubgroupi
yes it is integer
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";
ASKER
Can you help me check this itself since i am using RAD tools and I can write the code directly
int.Parse(cmbmenusubgroupi d.Selected Value.ToSt ring())
int.Parse(cmbmenusubgroupi
ASKER
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.StringToNumb er(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(S tring 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.Selec tedIndex!= 1)
Only issue is on form load where I get an error
error Message: at System.Number.StringToNumb
at System.Number.ParseInt32(S
at System.Int32.Parse(String s)
BUT ONCE FORM IS LOADED IT WORKS CORRECTLY
I get same error by adding line if(cmbmenusubgroupid.Selec
can you send me the stack trace of the error
ASKER
Here it is
Error Message: at System.Number.StringToNumb er(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(S tring s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at ClubCentric.Master.MasterM enu.Fillcm bmenugroup id() in D:\LLBLGenProjectSourceBac kup\Projec tLive\Club Centric\Ma ster\Maste rMenu.cs:l ine 261
Error Message: at System.Number.StringToNumb
at System.Number.ParseInt32(S
at System.Int32.Parse(String s)
at ClubCentric.Master.MasterM
please add a breakpoint on the line number 261. and see the value need to be converted.
ASKER
On form load it returns "{ClubCentricHelper.Entity Classes.Me nusubgroup Entity}"
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
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
ASKER
That means cmbmenusubgroupid_Selected IndexChang ed is getting fired while loading cmbmenusubgroupid. but again how to cover come this ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.