Retrieving object Session data to fill gridview

Trying to put object data into a grid view from a session variable

Page 1 of 1

9 Replies - 10251 Views - Last Post: 25 February 2010 - 04:14 PM Rate Topic: -----

#1 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Retrieving object Session data to fill gridview

Posted 21 February 2010 - 04:31 PM

Good morning.
Yes, it's monday morning and I've been struggling with this problem since friday :(

I'm trying to add form data to form data, which I've made into an object, then add the object to the session. Then I want to retreive this data so that it can be displayed in a grid view. Code as follows

TertiaryQualification Class snippet that is for each qualification that the user wants to add
  //Class Variables
    private String country;
    private String institution;
    private String firstYear;
    private String lastYear;
    private String qualName;
    private String studentNum;
    private String equivYears;
    private String excluded;
    private String completed;
    private ValidateUserInput userData = new ValidateUserInput();
	
    //Class constructors
    public TertiaryQualification()
	{
		//No argument constructor
		

    }

    public TertiaryQualification(String country, String institute, String firstYr, String lastYr, String qualName,
                    String studID, String equivYrs, String excluded, String completed)
    {
        setCountry(country);
        setInstitution(institute);
        setFirstYear(firstYr);
        setLastYear(lastYr);
        setQualName(qualName);
        setStudentNum(studID);
        setEquivYears(equivYrs);
        setExcluded(excluded);
        setCompleted(completed);
    }

    //Setters and Getters



TertiaryEducation Class code snippet that adds each Tertiary qualification object in an arraylist
public class TertiaryEducation : AppFormData
{
    //Class Variables
    private List<TertiaryQualification> qualifications = new List<TertiaryQualification>();
    private ValidateUserInput userData = new ValidateUserInput();


    //Class constructors
	public TertiaryEducation()
	{
		//No argument constructor
	}

    public TertiaryEducation(TertiaryQualification qual)
    {
        setQualifications(qual);
    }


    //Class Getters and Setter
    private void setQualifications(TertiaryQualification qual)
    {
        qualifications.Add(qual);
    }
    public List<TertiaryQualification> getQualifications()
    {
        return qualifications;
    }



    public void addTertiaryQualification(TertiaryQualification qualification)
    {
        try
        {

            setQualifications(qualification);
        }
        catch (Exception ex)
        {
            setErrorMsg("Could not add this qualification " + ex);
        }
    }



aspx.cs code behind page
protected void btnAddQual_Click(object sender, EventArgs e)
    {
        //txtStudentIDEHT.Text = "click";
        //UpdatePanel2.Update();
        if (addQualification() == true)
        {
            
            List<TertiaryEducation> userList = new List<TertiaryEducation>();
            userList = (List<TertiaryEducation>) Session["TertiaryHistory"]; 
            grvTertiaryHistory.DataSource = userList;
            grvTertiaryHistory.DataBind();
            rblYesNoQuals.SelectedValue = "Yes";
            UpdatePanel4.Update();
            ddlCountryEHT.SelectedValue = "0";
            //txtNameInstituEHT.Visible = true;
            //ddlNameInstituEHT.Visible = false;
            txtNameInstituEHT.Text = "";
            ddlFirstYearEnrolled.SelectedValue = "0";
            ddlLastYearEnrolled.SelectedValue = "0";
            txtQualName.Text = "";
            txtStudentIDEHT.Text = "";
            ddlFTEquivYears.SelectedValue = "0";
            ckbExcluded.Checked = false;
            rblCompleted.SelectedValue = "No";
            
            
            UpdatePanel2.Update();
            
            

        }
        else
        {
            Label1.Text = qualification.getErrorMessage();
        }
    }
 private bool addQualification()
    {
        bool valid = false;
        string excluded;
        string institute;
        if (ckbExcluded.Checked == true)
        {
            excluded = "Yes";
        }
        else
        {
            excluded = "";
        }

        if (ddlNameInstituEHT.Visible == false)
        {
            institute = txtNameInstituEHT.Text;
        }
        else
        {
            institute = ddlNameInstituEHT.SelectedValue;
        }

        qualification = new TertiaryQualification(ddlCountryEHT.SelectedValue, institute, ddlFirstYearEnrolled.SelectedValue,
                            ddlLastYearEnrolled.SelectedValue, txtQualName.Text, txtStudentIDEHT.Text,
                            ddlFTEquivYears.SelectedValue, excluded, rblCompleted.SelectedValue);
        if (qualification.isPageDataValid())
        {
            user.addTertiaryQualification(qualification);
            Session.Add("TertiaryHistory", user);
            valid = true;
        }
        else
        {
            Label1.Text = user.getErrorMessage();
            qualError = true;
        }
        return valid;
    }



Any suggestions would be appreciated
Thanks so much :)

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving object Session data to fill gridview

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retrieving object Session data to fill gridview

Posted 21 February 2010 - 04:37 PM

Since you said add to Session I assume this is a web application (ASP.NET) so I'm moving this to the ASP.NET Forum :)
Was This Post Helpful? 0
  • +
  • -

#3 rudi-rame  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-December 09

Re: Retrieving object Session data to fill gridview

Posted 21 February 2010 - 09:31 PM

I have always used either an in memory DataTable object or a datasource object (that queries a database) as the data source for a Gridview object. May I suggest a different approach?

Instead, use a DataTable object in place of your List<TertiaryQualification> for your qualifications class and each row of the DataTable would be a TertiaryQualification object. Then you could bind the GridViews' datasource to the DataTable (which is what a GridView is typically bound to).

Here is a sample code snippett for creating an in memory DataTable object:
        DataTable myDataTable = new DataTable("NameOfTable");

        DataColumn myDataColumn;

        myDataColumn = new DataColumn("ColumnName1", Type.GetType("System.Int32"));
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn("ColumnName2", Type.GetType("System.Int32"));
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn("ColumnName3", Type.GetType("System.String"));
        myDataTable.Columns.Add(myDataColumn);

        return myDataTable;



Here's a code snippett for adding a row to the DataTable object:
private void AddDataToTable(Int32 firstCol, Int32 secondCol, string thirdCol, DataTable myTable)
    {
        DataRow row;

        row = myTable.NewRow();

        row["ColumnName1"] = firstCol;
        row["ColumnName2"] = secondCol;
        row["ColumnName3"] = thirdCol;

        myTable.Rows.Add(row);
    } 



Hope this points you in the right direction...
Was This Post Helpful? 0
  • +
  • -

#4 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: Retrieving object Session data to fill gridview

Posted 21 February 2010 - 10:01 PM

Thanks so much rudi-rame
I have been struggling with this for so long!! Can I ask, how can I persist the TertiaryEducation data thru the application without using a Session? Does the DataTable persist the information thru out the session?? I like your approach a lot as I will be using the gridview to display the data in several places.
Cheers
Becky
Was This Post Helpful? 0
  • +
  • -

#5 rudi-rame  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-December 09

Re: Retrieving object Session data to fill gridview

Posted 22 February 2010 - 12:53 AM

Hi Becky,

Unfortunately, I am almost certain that the DataTable object information will not automatically persist throughout your session. If you do not want to store it in the session, then you will have to implement an alternative method for persisting the data. If you know that you are going to have a lot of rows in your DataTable objects, then I would recommend that you look into using a relational database management system. Microsoft has its SQL Server Express Editions available for download for free. Another approach is to work with XML data (or a combination of both). I would do a search on the DataTable object and the DataSet object to see what properties/methods are available to you. Good Luck Becky and "May the Source be with you" :)

rudi-rame
Was This Post Helpful? 0
  • +
  • -

#6 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: Retrieving object Session data to fill gridview

Posted 22 February 2010 - 04:08 PM

Thanks again for your help rudi-rame,
I wonder if I can add the datatable to the session

Session.Add("TertiaryQualifications", myDataTable);



Would that be an acceptable way to deal with persisting the data??

Unfortunately, the source is not with me on this problem, why has it deserted me??? :splat:
Becky
Was This Post Helpful? 0
  • +
  • -

#7 rudi-rame  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-December 09

Re: Retrieving object Session data to fill gridview

Posted 22 February 2010 - 11:29 PM

View Postbecstribe, on 22 February 2010 - 02:08 PM, said:

Thanks again for your help rudi-rame,
I wonder if I can add the datatable to the session

Session.Add("TertiaryQualifications", myDataTable);



Would that be an acceptable way to deal with persisting the data??

Unfortunately, the source is not with me on this problem, why has it deserted me??? :splat:
Becky


If you don't notice any issues with the performance and scalability of your app., then I think you should be fine storing it in your session. If you do notice performance issues with your application storing it in your session then that will be the determining factor as to whether or not that is an acceptable way to deal with persisting the data.

Happy Coding :bigsmile:
!Rudi
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Retrieving object Session data to fill gridview

Posted 23 February 2010 - 12:15 AM

I would avoid storing something of that nature in the Session Object, it will really degrade your performance. For something of this nature I would really look into storing it in cache instead, must less of a performance hit than storing large items in the Session. Just my 2 :)
Was This Post Helpful? 0
  • +
  • -

#9 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: Retrieving object Session data to fill gridview

Posted 23 February 2010 - 04:16 PM

Thanks for your help with this guys, much appreciated.
:tup:
Was This Post Helpful? 0
  • +
  • -

#10 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: Retrieving object Session data to fill gridview

Posted 25 February 2010 - 04:14 PM

Ok, I've finally worked out my problem!!
This is how I got my session data into the grid view.

This is what I originally had
List<TertiaryEducation> userList = new List<TertiaryEducation>();
            userList = (List<TertiaryEducation>) Session["TertiaryHistory"]; 
            grvTertiaryHistory.DataSource = userList;
            grvTertiaryHistory.DataBind();



This is what I replaced it with
TertiaryEducation displayQuals = new TertiaryEducation();
            displayQuals = (TertiaryEducation)Session["TertiaryHistory"];
            List<TertiaryQualification> userList = new List<TertiaryQualification>();
            userList = displayQuals.getQualifications();
            grvTertiaryQuals.DataSource = userList;
            grvTertiaryQuals.DataBind();
            UpdatePanel4.Update();



I knew it would be something simple. I was struggling with the type casting. I'm going to write a tutorial on this as I could not find one anywhere.
Hope this helps :bigsmile:

Cheers
Becky
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1