Extracting values from arraylist

A

Amit

Hi everybody,I have 2 dropdowns and on selected index change the second
dropdown is populated accordingly and so on.i am displaying the
selected value in a label for the user so that he comes to know the
hierarchy level.
i will show you this with a example so that u can understand it well
and help me out to come with a solution.the scenario is as below:
Label: a>>a1
Dropdown1:a,b,c,d
Dropdown2:suppose user clicks on (a) item in the first dropdown
then corresponding records to that item will be displayed in the 2nd
dropdown lets say a1,a2,a3 will be displayed and same will be displayed
in the label for example if user clicks on a1 in 2nd dropdown then
label text would be a>>a1 but if there are no records under a1 and
after that if user clicks on a2 then in the present situation the label
is displayed as a>>a1>>a2 which is wrong,this should have been a>>a2 .
This is happening because i am fetching the records from the database
on selected index change and appending it to the label text. I need
help and advice from all of you as early as possible. this can happen
upto n level so plz give me the solution with the code if you think
that this can be done in this or that way...



waitng for your earliest reply........
 
S

Steven Nagy

Well as you have identified, the problem is that you are APPENDING the
selected string.
You need to rewrite the whole label each time.
So lets say your label is called: lblResult, and your drop downs are
called ddl1 and ddl2 respectively.

So when ddl2 selected index changes, you want to rewrite the whole
label, something like this:

lblResult.Text = ddl1.SelectedText + " >> " + ddl2.SelectedText;

Is this what you had in mind?

SN
 
A

Amit

Steven said:
Well as you have identified, the problem is that you are APPENDING the
selected string.
You need to rewrite the whole label each time.
So lets say your label is called: lblResult, and your drop downs are
called ddl1 and ddl2 respectively.

So when ddl2 selected index changes, you want to rewrite the whole
label, something like this:

lblResult.Text = ddl1.SelectedText + " >> " + ddl2.SelectedText;

Is this what you had in mind?

SN
Dear Steven,
what you have written is almost correct the only thing is that only
first time the ddl1.selected text would be displayed in label and from
there on it would be only ddl2.selected text which i want to display in
the label. I will explain you in more detail.its like i have n level
categories in two droopdowns.first time only first dropdown is
displayed and on that selected index change the second one is
populated.from there onwards whenever a user selects a category that
category i am shifting to the first dropdown and corresponding records
to that category which user has selected i am displaying those records
in second dropdown.and i am checking the parentid's in database if any
corresponding records are there to that category in the database and so
on.for example suppose i have a1,a2 categories ubder a category.so the
first time user clicks on ist dropdown he will select a and in the 2nd
dropdown a1 and a2 will be displayed and when he clicks on a1 in the
2nd dropdown then a1 moves up to 1st dropdown and corresponding recoeds
to a1 will be displayed in the second dropdown. the problem is that
after selecting a1 if user clicks on a2 the n label shows:a>>a1>>a2
which is not correct it should have been a>>a2 and this can be upto n
level.This is happening bcoz i m appending the records in label.if you
have a better solution them plz help me by providing me with the code
that this can be done in this way...i tried to call a function in the
label but still it did not work...
your early reply would be a gr8 help.

Thanks
Amit
 
S

Steven Nagy

Hi Amit,

I'm sorry but after your explanation I am still not 100% sure what the
final result should be.

In the selected index change for DDL1 you would have this:
lblResult.Text = ddl1.SelectedText;

In the selected index change for DDL2 you would have this:
lblResult.Text = ddl1.SelectedText + " >> " + ddl2.SelectedText;


This is rewriting the whole label each time, depending on which DDL you
select from.
So with this code, selecting A from DDL1 would show:
A

Then selecting A2 from DDL2 would show:
A >> A2

Then selecting A4 from DDL2 would show:
A >> A4

Is this what you wanted?

Cheers,
Steven
 
A

Amit

Dear Steven,
This is rewriting the whole label each time, depending on which DDL you
select from.
So with this code, selecting A from DDL1 would show:
A

Then selecting A2 from DDL2 would show:
A >> A2

Then selecting A4 from DDL2 would show:
A >> A4

Upto here what you have written is absolutely correct but suppose if
under A4, i have two more categories say A4.1 and A4.2 then by
selecting A4.1 from DDl2, label should display
A>>A4>>A4.1 and if there are no records under A4.1 and then if user
selects A4.2 then label should display A>>A4>>A4.2 .right now its
showing A>>A4>>A4.1>>A4.2 .bcoz i am appending the text in the
label.but if i do not append then i loose previous text.In this way i
have to retain the previous text in the label also This can be upto n
level. I mean A>> A4>>A4.2>>A4.21>>A4.211 and so on. I think now it
will give you a clear picture and i hope i will get a solution from
you..

Thanks
Amit
 
S

Steven Nagy

Ok I think I understand you now.
The second drop down can contain elements that are sub elements of each
other?
Whats your table schema look like for these elements?

Currently I am imagining something like this:

ID
Name
ParentID

.... where anything with a null (or 0) ParentID goes into the first drop
down list, and everything else goes into the second drop down list.

In this example, you could write a method that recursively returns the
right string:

// Note: Code untested
private string GetLabel(int ItemID) {
SqlConnection dbc = GetDBConenction();
string SQL = "SELECT * FROM MyTable WHERE ID=" + ItemID.ToString();
SqlDataAdapter da = new SqlDataAdapter(SQL, dbc);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count>0) {
string Name = dt.Rows(0)("Name");
int ParentID = (int)dt.Rows(0)("ParentID");
if (ParentID==0) {
return Name;
}
else {
return GetLabel(ParentID) + " >> " + Name;
}
}
return "";
}


Hope this helps.

SN
 
A

Amit

Hi steven,
I was off from my work for one week as i was on holidays so could not
see your reply.
now i have used it and its working fine till now.Thanks for your
extended help and i wish and hope that i will get the same help from
your side in future also..

Thanks
Amit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top