Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I am working on my first web application using ASP.NET. The requirement is as follows:

1. Read Certain rows from DB and dynamically display on the page.
2. Update the values in the web page and update the DB on a button click event.

The controls are simple text boxes and are created dynamically depending on different user actions. Basically, the same page has different number of text boxes based on which user is accessing the website.

I have completed the dynamic population part but not able to think of a way to map the contents of the controls back to the corresponding DB entries.
I am a beginner to this, so any help will be greatly appreciated.

Sample data in DB table UserInfo:

1,1,Name,Peter
1,2,Gender,Male
1,3,UID,100
2,4,FirstName,Mark
2,5,LastName,Harrold
2,6,Occupation,Engineer
2,7,Country,USA

If Peter logs in, the page renders only 3 label-textbox pairs to display Name,Gender,UID
If Mark Harrold logs in, the page renders 4 label-textbox pairs to display FirstName,LastName,Occupation,Country.
At any given point, I have the ID of the user whose data is being updated (1 for Peter, 2 for Mark Harrold) but I am not able to figure out a way to map the label-textbox pairs to the second ID (1-3, 4-7).

Thanks,
Hersh
Posted
Updated 21-Jul-12 15:18pm
v2

 
Share this answer
 
Comments
Hersh Iyer 21-Jul-12 22:29pm    
The link you suggested describes a static scenario..the controls in my case are laid out dynamically..I do not know before hand as to how many controls are being created. It depends on the user id.
woutercx 21-Jul-12 22:47pm    
It seems like you are dealing with two different "object types", so you should store them in different tables. Then you could create 2 different entity types for them
Hersh Iyer 21-Jul-12 22:58pm    
What objects are we talking about? All are coming from the same table. I just query based on the user ID which is the first entry in the table. I cannot create different tables for each user.
What I would do in your case is create a new column "UserType" or something. Otherwise, it is very hard to determine which columns you need to update.
Then I would do (as in the example in VB):

VB
Dim ctx As New EmployeeEntities

'You said "I have the ID of the user whose data is being updated"
Dim IHaveTheUserId As Integer = 1

Dim oEmp As Emp = ctx.Emp.Where(Function(p) p.ID = IHaveTheUserId ).FirstOrDefault
If oEmp Is Nothing Then
  Me.lblMsg.Text = "Employee not found to update"
  Exit Sub
End If

If usertype = 1 Then
  oEmp.Name = Me.txtName.Text
  oEmp.Gender = Me.txtGender.Text
  oEmp.UID = Me.txtUID.Text
Else
  oEmp.FirstName = Me.txtFirstName.Text
  oEmp.LastName = Me.txtLastName.Text
  oEmp.Occupation = Me.txtOccupation.Text
  oEmp.Country = Me.txtCountry.Text
End if

ctx.SaveChanges()
 
Share this answer
 
v2
Comments
Hersh Iyer 22-Jul-12 12:25pm    
Thanks for the explanation. But this will still not help me because I do not know if those are the only fields that are present for every employee. There are many employees and each one has different number of fields. So, I do not know if its the name, gender, etc., It's dynamically laid out.
If I would find a solution like that, I would throw it away and start again...
Anyway, in this case maybe you could make the fieldnames the same as the columns in the table, and then you could ask the value of the control name, and you would update that in the database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900