How: Checkboxlist binding from database

Posts   
 
    
weezer
User
Posts: 42
Joined: 24-Apr-2012
# Posted on: 22-May-2012 09:48:55   

The Checkbox information is from The Grade_Level table column name Grade_Level

When the user select for instance select all students by year checkbox basically it will try to save to SelectAll column in the table which stored as a bit value.

And it also stored the value to the table called Program_instance_year_select.

My question is: - How should I save the value and rebind the value selected in both Program_instance table and Program_Instance_Year_Select

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-May-2012 18:49:19   

I'm totally confused by your question. Don't know how Grade_Level is related to Program_instance table and Program_Instance_Year_Select. And which table has the SelectAll column.

weezer
User
Posts: 42
Joined: 24-Apr-2012
# Posted on: 24-May-2012 16:31:41   

I have 3 tables which contain the information

First table called Grade table which contain Grade_Level_Pk (int, not null), Grade_Level_ID (int, null), Grade_Level (varchar(50) not null), Grade_Level_Code(varchar(20) null,

The first thing, i have to bind the value from Grade_Level in Grade table to the check box list. (this one its not a problem)

I have another table called ProgramInstance which contain:

ProgramInstanceId(Pk, int, not null) SchoolId(int, null) ProgramId(Fk, int null) InstanceName (nvarchar 255, null) InstanceStart (DateTime, null) InstanceEnd (DateTime, null) SchoolAdmin(int, null) InstanceNotes (nvarchar(max) null) SelectAll (bit, null) ClosureReasonID(int, null)

When i add a program instance, i have checkboxlist which contain the information from Grade_level column from Grade_Level table. I also have a checkbox called select all. Here is the part i having a problem.

First, if the user select select all checkbox, it will select all the value in the checkboxlist and when it click save button the select all value checkbox to the selectAll column in ProgramInstance (bit, null) and it also save to the following table called Program_Instance_Year_Select table which contain information:

RecordId(Pk, int, not null) ProgramInstance(Fk, int not null) Grade_Level_Fk(int, not null) Grade_Level_Select(bit, null)

Grade_Level_Select column will record the selected checkboxlist when you try to add a program instance.

Thank you so much

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-May-2012 19:26:52   

Now I understand your business case, but I don't understand the initial question. So could you please elaborate more on your question.

weezer
User
Posts: 42
Joined: 24-Apr-2012
# Posted on: 25-May-2012 03:51:13   

The user is trying to add A program Instance and fill out all the required fields in the Program Instance Form then click select All and click Save button.

The information need to record to save to table called ProgramInstance and the select all checkbox is recorded as a bit.

When the user click save button from Program Instance Form, it also save the information to the second table Program_Instance_Year_Select with the following information

RecordId(Pk, int, not null) ProgramInstance(Fk, int not null) --> grab information from Program Instance Table Grade_Level_Fk(int, not null) --> grab information from ProgramInstancetable (Checkbox list) Grade_Level_Select(bit, null) --> grab information from ProgramInstance (Checkbox list)

Could you please advise how i need to implement it?. Thank you

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-May-2012 08:01:25   

Ok. So Program_Instance_Year_Select is kind of an intermediate table that store the info about the Program and all the existing Grade_Level, including whether the user select it or not (bit).

Lets suppose you are doing a classic Asp.Net 2.0 app and Adapter templateSet. Then start with the basic, that is, doing a FormView that binds to a LLBLGenProDataSource2. For more info about that please read Databinding with ASP.Net. When doing this, use the LivePersistence=false at the LLBLGenProDataSource (you will see why later), so you will have to implement the datasource event handlers. At this point you should be able to insert a ProgramInstance entity.

Now you have to insert also the ProgramInstance 1:n ProgramInstanceYearSelect collection. The first step is to show all GradeLevel entities in the GUI in a way of checkboxlist. You can also bind the checkboxlist control to another LLBLGenProDataSource, such datasource must have the GradeLevelEntityFactory as the EntityFactoryTypeName. Now when you click save, the code inside the yourProgramInstanceDatasource_PerformWork should be executed. Inside that method you can access the underlying collection of the gradeLevelDataSource, so you know what grades the user selected, with that information you construct the ProgramInstanceYearSelect collection that you will attach to the main ProgramInstance entity.

I hope that would not sounds too overwhelming. In fact it's easy but you have to get used to the LLBLGenProDataSource and its event handlers. In short you should:

  1. Create a FormView (programFormView) in conjunction with a LLBLGenProDataSource (programDS) that allows you to save a ProgramInstance .

  2. Create another LLBLGenProDataSource (gradeLevelsDS) that serves the data to your checkboxlist control (gradesChks).

  3. When you click save, the programDS.PerformWork event handler will fire, in there you can access the gradeLevelsDS collection to examine the selected grades. Or you can access the gradesChks to see that as well.

  4. Extract the involved entity ProgramInstanceentity to be saved (theProgram).

  5. Based on (3) you should be able to fill theProgram.ProgramInstanceYearSelcect collection.

  6. Save theProgram entity recursively.

If you need we help you with a little example please provide the following info (http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7722): - What LLBLGen version are you using? - Are you using Adapter or SelfServicing? - What kind of GUI are you coding (ASP.NET, ASP.Net MVC, WinForms, etc)? - What do you have so far?

David Elizondo | LLBLGen Support Team
weezer
User
Posts: 42
Joined: 24-Apr-2012
# Posted on: 25-May-2012 08:10:00   

Hi Daelmo,

  1. I am currently using LLBLGen version 3.0,
  2. I am using Adapter
  3. application that i am developing is build using ASP.net framework 4
  4. I am able to save to ProgramInstance table but i am unable to save to Program_Instance_Year_Select table.

If you could help me with some example that would be awesome.

Thank you so much.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-May-2012 10:24:12   

Ok. Attached is an example. It's very basic:

Tables Program ProgramId (PK, int, autoinc) Name SelectAllCourses (bit)

Course CourseId (PK, int, autoinc) Name

ProgramCourse ProgramId (PK, FK to Program.ProgramId) CourseId (PK, FK to Course.CourseId) IsSelected (bit)

GUI All is almost automatic with LLBLGenProDataSource2. I realized it's not necessary to use LivePersistence=false, so the behind code is very small. The only relevant part is:

/// <summary>
/// Trap the insertion just before it's executed. This way we can add things to the save action.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void _programDS_EntityInserting(object sender, DataSourceActionEventArgs e)
{           
    // see what the user selected
    CheckBoxList coursesChks = (CheckBoxList) _programFormView.FindControl("_coursesChks");
            
    // convert the user selection into ProgramCourse's and add them to the inserting program
    ProgramEntity program = ((ProgramEntity)e.InvolvedEntity);          
    foreach (ListItem chk in coursesChks.Items)
    {
        var toAdd = new ProgramCouseEntity();
        toAdd.CourseId = Convert.ToInt32(chk.Value);
        toAdd.IsSelected = chk.Selected;
        program.ProgramCouses.Add(toAdd);
    }           
}

Running it 1. Run the DB script 2. Open solution, fix references if necessary. Fix the connection string if necessary. 3. Run it. The example just add Programs, so there is no ItemTemplate nor EditTemplate into FormView, but it's easy to add those.

Attachments
Filename File size Added on Approval
Careers.zip 198,891 25-May-2012 10:24.33 Approved
David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 25-May-2012 10:59:02   

Additionally, this forum (which is open source and uses llblgen pro) has binding to checkboxes:

https://bitbucket.org/Solutionsdesign/hnd/src/4975377cfd3c/GUI/Admin/ManageForumRights.aspx

code behind: https://bitbucket.org/Solutionsdesign/hnd/src/4975377cfd3c/GUI/Admin/ManageForumRights.aspx.cs

It's written with asp.net webforms 2.0, so no fancy datasource controls, but it illustrates a point. Nowadays you can get away with less code than this, so I'd first use David's example simple_smile

Frans Bouma | Lead developer LLBLGen Pro
weezer
User
Posts: 42
Joined: 24-Apr-2012
# Posted on: 27-May-2012 12:39:11   

Hi Daelmo, Otis

Thank you so much.

Cheers