Dynamic drop down creation in asp.net - c#

I have a table with 3 columns
EmployeeId EmployeeName ReportingTo
1 A
2 B 1
3 C 1
4 D 1
5 E 2
6 F 3
7 I 3
8 J 4
9 K 8
Employee Ids are unique. ReportingTo is id of person to whom employee is reporting. ReportingTo is null means A is Boss.
In Asp.net, I want to create a page with one dropdownlist by default. In that dropdownlist EmployeeId whose reportingto is null will be loaded. when I select that dropdown with EmployeeId 1 then next dropdownlist will get created & fill with employees who are reporting to EmployeeId 1.
I have created this whole page, but for that i am generating dropdownlist manually in aspx page.
Anyone can guide me how to create this scenario with dynamic dropdownlist, so I can generate any number of dropdowns & fill it dynamically.

You can add a placeholder control on your web page and then create the new dropdown and add it to the placeholder like so:
private void AddDropDown(){
var dl = new DropDownList();
dl.ID="empDDL";
dl.DataSource = list;
dl.DataBind();
myPlaceholer.Controls.Add(dl);
}
Hope this helps.

You use ASP.Net cascade dropdownlist. This example
http://www.aspsnippets.com/Articles/Creating-Cascading-DropDownLists-in-ASP.Net.aspx

Related

How to display column data as row header in Crystal Reports

product Table
I have 2 tables, product table with productID and productName :
Product id productName
-----------------------
1 BUR
2 bariis
3 moos
and branch table with branch Id and branchName:
branchid branchName
----------------------
B1 Sb1
B2 Cb2
I want to display a report in Crystal Reports like this
branch BUR bariis moos
--------------------------
Sb1 3 4 4
Cb2 2 1 5
Look at the image for clear understanding
Simply use the option to Insert, CrossTab...

Auto populating textbox with incremented column value

I have a dropdownlist of ID=ddlDept which shows the department from a column named DId and table named Department. Under that selected department there are students with StudentID from the column named StudentID and table named registration which i have done by inner join operation. Now I want to auto populate a textbox in a aspx webform with the incremented StudentID by selecting from ddlDept.
Before selecting ddlDept Suppose i had ddlDept of 02 and the last studentID in that "02" department is 0705044.
Now if I select "02" from ddlDept then the textbox for StudentID must populate with the value of 0705045
Also if the Student ID is 0405089 then by selecting the ddlDept it must populate with 0405089.
That means increment will be of the last 2 digits only.
I am very stuck.Suggest with sample code.Thnx in advance

Datatable - Using linq I want to update a row with the count

I have a C# datatable with:
Id ParentId Name
======== =========== =====
1 A
2 1 b
3 1 c
4 2 d
6 5 e
I want to add a column or just the result that will identify any ParentIds that do not have an Id. Basically in the above table, I want to find Id=6 since ParentId 5 is invalid.
I want to identify anywhere a parentId is not listed as an Id
I thought this could be accomplish with Linq.
This will return all rows where the ParentId does not appear in any row as Id
rows.Where(r1 => !rows.Any(r2 => r1["ParentId"] == r2["Id"]))

how to use EF to select using DISTINCT and FOREIGN KEY together

This may be very basic, but here we go, as I can´t solve that for now.
I´m using Entity Frameword for Oracle (Managed Driver) and would like to do get all following:
I have 2 database tables as follows:
TABLE_USER
ID INTEGER
NAME STRING
AGE INTEGER
Content:
0 JOHN 39
1 MARY 40
2 ALBERT 41
3 ROBERT 42
4 SARAH 43
5 PETER 44
TABLE_EVENTS
ID INTEGER
EVENT_NAME STRING
USER_ID INTEGER (FOREIGN KEY TO TABLE_USER.ID)
Content:
0 CREATE 1
1 CREATE 2
2 CREATE 5
3 DELETE 3
4 DELETE 0
5 CREATE 1
6 DELETE 3
7 CREATE 0
8 UPDATE 4
9 UPDATE 5
10 DELETE 1
I need to get all DISTINCT values from TABLE_EVENTS where the user age in TABLE_USER is of a certain condition (like AGE <= 40).
That example would give me the following list:
CREATE
DELETE
My code shall look like:
IQueryable<TABLE_USER> tableUser = dbContext.TABLE_USER;
IQueryable<TABLE_EVENTS> tableEvents = dbContext.TABLE_EVENTS;
///
/// Build the query
///
tableUserQuery = tableUser.Where(record => record => USER_ID == ??tableUser.ID?? &&
??tableUser.AGE < 30?? ).GroupJoin(???);
///
/// Execute que query
///
var dbList = query.ToList();
I was looking at GroupJoin and other stuff, but I can´t even figure out how to built this query, so all I´ve posted is a skeleton...
Appreciate any kind of help.
typically if 2 tables are linked by foreign key, EF generates the navigation property to reach the other entity and you can use this query.
List<string> events = dbContext.TABLE_EVENTS.Where(te=>te.TABLE_USER.AGE < 30).Select(te=>te.EVENT_NAME).Distinct().ToList();
If you don't see the foreign entity linked, then the query is
List<string> events = dbContext.TABLE_EVENTS.Join(dbContext.TABLE_USERS, te=>te.USER_ID, tu=>tu.ID, (te,tu)=> te).Where(te=>te.TABLE_USER.AGE < 30).Select(te=>te.EVENT_NAME).Distinct().ToList();
the above query basically does an inner join on the user id, applies the filter and gets the distinct values.

Unable to retrieve Distinct record from database?

I have a Database table like below,
ID Author Book
1 Kate Smuggler
2 Harper Smuggler
3 Slater Smuggler
4 Kate Katy
5 Smitha Katy
i want to retrieve all Book names(Distinct) based on the author name and display them on list view, i wrote the code below to Retrieve distinct values
int UserID = Convert.ToInt32(Session["User"]);
var Li = (from s in Data.TableName where s.Author == UserID select s.Book).Distinct().OrderBy(ID=>ID);
ListViewChatList.DataSourceID = "";
ListViewChatList.DataSource = Li;
ListViewChatList.DataBind();
but i get the following error 'System.String' does not contain a property with the name 'ID'.
How to solve this ?
This part:
select s.Book
is selecting just the book (title) part of the record. You can't get back from that to the ID... there can easily be multiple IDs for the same title, as per your sample data.
It's not clear what you'd want that query to return anyway, given that you're getting distinct book titles - ignoring the author, would you want IDs 1 and 4? 2 and 5? 3 and 5? Both represent the same set of distinct titles (Smuggler and Katy).
You could potentially group by name, and then do whatever you want with the IDs for the books with the shared name - but you really need to consider what your distinctness criterion means for the results.
Here's a query which will give you the lowest ID for each name:
var query = from s in Data.TableName
where s.Author == UserID
group s.ID by s.Book into g
select g.OrderBy(id => id).First();
It's not clear whether that's what you really need though.
var Li = from s in Data.TableName
where s.Author == User
orderby s.ID
select s.Book;
You're only selecting the Book column (a string), and then trying to order by ID, which you no longer have access to, and which certainly is not a property of the Book string.
Assume this is your data:
ID Author Book
1 Kate Smuggler
2 Kate Smuggler2
3 Kate Smuggler
4 Frey Katy
5 Smitha Katy
If you do the following, you'll get the first three books listed above:
(from s in Data.TableName where s.Author == "Kate" select s.Book);
Then you do a distinct, which reduces the books to the two unique ones (Smuggler and Smuggler2):
(...).Distinct();
Then you try to order by the ID, but you didn't originally select it so you don't have access to it. And you've taken a Distinct on the results, so you've lost some of the detail.
Since the two "Smuggler" books have been reduced to one, but their IDs were 1 and 3 (in my example), what should ordering them do? Should the single "Smuggler" book appear before "Smuggler2" or after?

Categories