are you trying to return all of the information from a specific column or filter based on a column
you can just do a select
ds.Tables[0].Select(column
Main Topics
Browse All TopicsExperts,
I need immediate help. I have to create a class in asp.net 3.5. This class will take a dataset as input and have method in it that will populate dropdown list. The dataset has 5 columns. Based on column number passed as parameter to this method, the dropdown list will be populated with data in that column.
Please help me with code ASAP.
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
this might help depending on the datasource
http://blogs.msdn.com/adon
DataTable filteredCCenter = (from d in ((DataSet)_MyDataset).????
DSToReturn.Tables.Add(filt
return DSToReturn;
I have to use linq query. what wil be the syntax in ???? - such that distinct values from dataset column name "business" is extracted to the datatable. Please provide the linq query code.
Actually the column name is passed to the class method as parameter like this :
project mypj = new project((DataSet)(Session[
DataSet DS4DDL1 = mypj.DDLPopulate("Business
So what should be the linq query :
public DataSet DDLPopulate(String columnName)
{
DataSet DSToReturn = new DataSet();
DataTable filteredCCenter = (from d in ((DataSet)_MyDataset).????
DSToReturn.Tables.Add(filt
return DSToReturn;
}
Ok so I got the linq query done and its working except one small problem, I am hardcoding the columnName. How do I pass the parameter colunName to the linq query instead of hardcoding "Business"???
DataTable filteredCostCenter = (from d in ((DataSet)_MyDataset).Tabl
where (d.Field<string>("Business")
select d).Distinct().CopyToDataTa
yes, which mean that if business column has 3 distinct data/values - Bus1, bus2, bus3, then all those values will show up in DDL. How can I add in CS code behind in ProjDDL1.DataValueField = "Business";
number that increases 1, 2, 3, dynamically with the number of business values?
ClientPJ mypj = new ClientPJ((DataSet)(Session
DataSet DS4DDL1 = mypj.DDLPopulate("Business
ProjDDL1.DataSource = DS4DDL1;
ProjDDL1.DataTextField = "Business";
ProjDDL1.DataValueField = "Business";
ProjDDL1.DataBind();
ProjDDL1.Items.Insert(0, "--");
Yes, initially it gave me error but I added the where clause and its working now, I am getting unique values from column
DataTable filteredCostCenter = (from d in ((DataSet)_MyDataset).Tabl
where (d.Field<string>(columnName)
orderby d[columnName] ascending
group d by d[columnName]
into g
select g.First()).CopyToDataTable
I have to add another method in this class to search for matching text selected in any of the DDL or text box in another field in same dataset. Have to use ling query:
public DataSet searchPJ(String ProjectName)
{
DataSet DSToReturn = new DataSet();
//linq query to filter Project column based on search string selected in DDL or textbox
return DSToReturn;
}
do you need distinct values again? if not
DataTable results = from d in ((DataSet)_MyDataset).Tabl
orderby d.Field<string>("Project") ascending
where d["Project"].ToString().Eq
select d).CopyToDataTable();
if you dont care about case
DataTable results = (from d in ((DataSet)_MyDataset).Tabl
orderby d.Field<string>("Project") ascending
where d["Project"].ToString().Eq
select d).CopyToDataTable();
onselect change event for dropdownlist, the selected text is passed to search method. But nothing is happening. - something wrong with the string that I am passing?
DataSet DS4DDL1 = mypj.searchPJ("ProjectDDL1
protected void ProjDDL1_SelectedIndexChan
{
String ProjectDDL1 = ProjDDL1.SelectedItem.Text
ClientPJ mypj = new ClientPJ((DataSet)(Session
DataSet DS4DDL1 = mypj.searchPJ("ProjectDDL1
ProjList.Items.Clear();
foreach (DataRow dr in DS4DDL1.Tables[0].Rows)
{
if (dr[0].ToString().Trim().L
ProjList.Items.Add(new ListItem(string.Format("{0
}
}
I am using this one and its giving syntax error when I tried replacing equlas to contains.
DataTable results = (from d in ((DataSet)_MyDataset).Tabl
orderby d.Field<string>("Project") ascending
where d["Project"].ToString().Co
select d).CopyToDataTable();
If the serach does not return any result I am getting a compilation error. How can I check for empty dataset?
DataTable results = (from d in ((DataSet)_MyDataset).Tabl
orderby d.Field<string>("Name") ascending
where d["Name"].ToString().Conta
select d).CopyToDataTable();
just check if the dataset==null or the dataset.table["TableName"]
2 ways to do the ignore case
DataTable results = (from d in ((DataSet)_MyDataset).Tabl
orderby d.Field<string>("Name") ascending
where d["Name"].ToString().ToLow
select d).CopyToDataTable();
or
DataTable results = (from d in ((DataSet)_MyDataset).Tabl
orderby d.Field<string>("Name") ascending
where d["Name"].ToString().Index
select d).CopyToDataTable();
3rd if you know the type you can just type it in the method like
public DataSet searchPJ(DataSet ds,String ProjectName)
or
public DataSet searchPJ(DataTable dt,String ProjectName)
if not
public DataSet searchPJ(object source,String ProjectName)
and then you would have to check its type and then do something with it
I tried it this way and the datasource that I am passing is null, even though it not null: How can I pass datasource to this method?
public DataSet searchPJ(DataSet listb, String ProjectName)
{
DataSet DSToReturn = new DataSet();
//logic to filter based on the column NAme
DataTable results = (from d in ((DataSet)listb).Tables["R
orderby d.Field<string>("Name") ascending
where d["Name"].ToString().Conta
select d).CopyToDataTable();
DSToReturn.Tables.Add(resu
return DSToReturn;
}
Method--
protected void ProjDDL1_SelectedIndexChan
{
String ProjectDDL1 = ProjDDL1.SelectedItem.Text
ClientPJ mypj = new ClientPJ((DataSet)(Session
//DataSet DS4DDL1 = mypj.searchPJ("chemistry")
ListBox listBox = new ListBox();
DataSet DS4DDL1 = mypj.searchPJ((DataSet)(DD
DataSet DS4DDL1 = mypj.searchPJ((DataSet)(Pr
ProjList.Items.Clear();
foreach (DataRow dr in DS4DDL1.Tables[0].Rows)
{
if (dr[0].ToString().Trim().L
ProjList.Items.Add(new ListItem(string.Format("{0
}
}
Method should do this I don't know what the code wil be- Please help
protected void ProjDDL1_SelectedIndexChan
{
String SearchStr = ProjDDL1.SelectedItem.Text
// Code to get the listbox ProjList datasource?
ClientPJ mypj = new ClientPJ((DataSet)(Session
DataSet DS4DDL1 = mypj.searchPJ((DataSet)(Pr
ProjList.Items.Clear();
// Code to check if not Null then update the listbox or databind the list box with the search results?
foreach (DataRow dr in DS4DDL1.Tables[0].Rows)
{
if (dr[0].ToString().Trim().L
ProjList.Items.Add(new ListItem(string.Format("{0
}
}
yes, only on page load, but after that when 1st dropdown selects the listbox will filter when the 2nd DDL will select the 1st DDL's filtered listbox datasource will be passed. there are 5 DDLs and there is no sequence on DDL selection, i.e. user can select from 4th or 2nd and then select from 3 or 5th, the listbox item will remember the previous filter. So that's why I am trying to pass the listbox datasource to the method for searching.
Sadly, that is not accepted, becaz it wil slow the performance, and this prosal of mine was turned down.
So coming back to the same old problem, I am passing list items instead of source now.
DataSet DS4DDL1 = mypj.searchPJ(ProjList.Ite
But in method getting stucked on linq query to search listcollection litems, any idea how to search list colltection? :
public DataSet searchPJ(ListItemCollectio
{
DataSet DSToReturn = new DataSet();
DataTable results = (from d in (projList.????
orderby d.Field<string>("Name") ascending
where d["Name"].ToString().Conta
select d).CopyToDataTable();
select d).CopyToDataTable();
DSToReturn.Tables.Add(resu
return DSToReturn;
}
When I am bind listbox in pagload
ProjList.DataSource = DS4DDL1;
ProjList.DataTextField = "Name";
ProjList.DataValueField = "Value";
ProjList.DataBind();
When I am rebinding it again with seacrh results -
ProjList.DataSource = DS4DDL1;
ProjList.DataTextField = "Name";
ProjList.DataValueField = "Value";
ProjList.DataBind(); <---- giving error - does not contain property with name "Name"
What is missing here?
Does that mean in function search, something needs to be added to include the name and value properties in Listbox collection being returned?
istItemCollection returnItems= new ListItemCollection();
foreach (ListItem item in projList)
{
if (item.Text.IndexOf(Project
{
returnItems.Add(item);
}
}
return returnItems;
I think that will be fair to open a new question. Please reply to the new thread because you have help me all thru this question and are familiar with this it.
Before I open a new questu=ion, need some help in understanding what is diffrence between
ProjList.DataTextField = "Text";
ProjList.DataTextField = "Name";
Name is a field in dataset that binds the list box in pageload. Does this mean that when it rebinded after search listbox does not have Name and Valueids in it?
Just trying to see if we don't have to go thru datatable route:
if listitem has text and value, then is it possible to assign value to the rebinded listbox with original value coming from session variable dataset? if that is possible, then I only need this value id. While text wil display the filtered matching item in listbox, values of these items will be the value id passed in original dataset. the selected value in listbox will be needed on page submit. Is it possible to reatin the value at least in listbox?
try this for the method i dont have much time left for today
public DataTable searchPJ(ListItemCollectio
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Value");
foreach (ListItem item in projList)
{
if (item.Text.IndexOf(Project
{
DataRow dr = dt.NewRow();
dr["Name"] = item.Text;
dr["Value"] = item.Value;
dt.Rows.Add(dr);
}
}
return dt;
}
I have posted a question at
http://www.experts-exchang
I will keep this question open for a while, just incase something needs to change. But overall it was a very helpful solution. Thx.
Ran into a serious problem today with the listbox search. there are 2 dropdown out of 5 that will not search for matching text in listbox but in the dataset behind it, when matching text is found it will populate listbox with dataset. this filtered dataset wil be ssearched by other DDL as well. Should I open another question, becaz this solution will not work :(. Need help ASAP.
Please help, I am in seroius time crunch now, I thought the problem was solve, now coming back to zero again. http://www.experts-exchang
Is this possible to get valueid from this matching list items and then search data from original dataset? For example in the code :
public DataSet searchPJ(ListItemCollectio
{
DataSet DSToReturn = new DataSet();
//ListItem item = projList.FindByText("Proje
ListItemCollection returnItems = new ListItemCollection();
foreach (ListItem item in projList)
{
if (item.Text.IndexOf(Project
{
returnItems.Add(item); <-----------how can I get valueIDs from listbox with the matching selection instead of text?
// then linq query to search these valueids in dataset _mydataset, extract field name Client_owner and return text value to listbox from dataset field "Name".
dataset structure is :
//Name Client_owner Business_owner
}
}
return returnItems;
}
Please help asap. Thx.
I have posted a new question, please help me if you can
http://www.experts-exchang
Business Accounts
Answer for Membership
by: suran78Posted on 2009-10-12 at 15:15:00ID: 25555549
here is the public method, how do I add linq code in it that will retrieve data based on column name
#region Public Methods
public DataSet DDLPopulate(String columnName)
{
DataSet DSToReturn = new DataSet();
//logic to filter based on the column NAme
return DSToReturn;
}