Serializing drop-down list data sources in Session State

L

Leszek Taratuta

Hello,

I have several drop-down lists on my ASP.NET page. I need to keep data
sources of these lists in Session State.
What would be the most effective method to serialize this kind of data
structures?

Thanks for any hints,
Leszek Taratuta
 
L

Leszek Taratuta

Thanks, but this what I have already tried. The problem is the ListItem
class is not serializable.

I have created my own ListItem-like class (serializable) and copied
drop-down list data sources (using my ListItem class) to ArrayLists. Then I
put the array lists into a hash table with keys as drop-down lists' ids.
This solution does not work. When I try to deserialize such a structure,
..NET gives me an exception that the type version has been changed and it is
not possible to deserialize data. I think the type is too complex for
serialization (custom ListItem-like objects within ArrayLists within a
Hashtable).

Any other suggestions,
Leszek
 
K

Kevin Spencer

You didn't say you wanted to serialize Controls, you said you wanted to
serialize the DataSource that you're binding to. A ListItem is a UI Control,
and there's no need (or even a good reason) to serialize it. Just store the
DataTable in Session and bind to it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
K

Kikoz

Live sample:

(If this ddl doesn't depend on specific user, store it in Application or
Cache):

const string SOURCE_NAME = "SourceName";
private DataTable GetThatFreakingSource()
{
if(Application[SOURCE_NAME] == null)
{
DataTable tbl = GetThatTableFromDbOrSomething();
Application[SOURCE_NAME] = tbl;
return tbl;
}
return (DataTable)Application[SOURCE_NAME];
}
private void BindThatDdl()
{
ddl.DataSource = GetThatFreakingSource();
ddl.DataBind();
}

Done. In sort, when you box Application[SOURCE_NAME] to DataTable (last line
in the first method) that's where serilization takes place. Automatically.

Enjoy :)
 
L

Leszek Taratuta

Thanks. I forgot the ListItems are controls.
The situation is that my data sources are not DataTables. I have ArrayLists
filled with ListItems.
The problem is I cannot serialize this kind of ArrayLists to session state
kept in state server.

Thanks,
Leszek
 
K

Kikoz

I think you complicate things for no obvious reason :) I may be totally
wrong, it all depends on particular situation, but this is what it seems
like. Much easier to store data in serializable object like DataTable and
then keep it on server, even if that would require to put your data in such
table first.
 
L

Leszek Taratuta

Well, I know it is much easier to store data in serializable object like
DataTable but I work in a team, and what I receive from the data layer is
ArrayList containing ListItems.
For now the only resonable solution is to convert the ArrayLists to
DataTables. It is not efficient though. I thought it would be any other more
appropriate way to store this kind of data. Any kind of serialization from
ArrayLists to strings or so, and then deserialization.

Thanks,
Leszek
 
K

Kevin Spencer

Well, Leszek, it seems that the wrong person is asking this question of the
newsgroup. Whoever is sending you ArrayLists of ListItems is the one who
needs the help!

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
L

Leszek Taratuta

Thank you. I am going to talk with the guy who creates the data layer.

BTW
What could be the best alternative to get drop-down list data sources from
the data layer? DataTables are pretty heavy. Any other possibilities.

Thanks,

Leszek
 
K

Kevin Spencer

Hi Leszek,

First of all, as was mentioned earlier, ListItems are UI elements. A
well-designed Data Layer should NEVER work with UI elements. Why, even your
business layer shouldn't be working with UI elements. Only data structures
should come from the Data Layer. That would be raw primitve data, such as
integers, dates, and strings, DataSets, DataTables, and DataReaders. Now,
there is some controversy over returning DataReaders, so the next smallest
data structure (other than primitives) would be a DataTable, and it really
has a relatively small footprint. DataReaders are the smallest, but have
some issues, such as necessitating a constant Connection while in use, as
well as being forward-only, read-only, and not being serializable.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
L

Leszek Taratuta

Thanks a lot. It seems DataTable is the best solution.

In the meantime the database guy decided to fill ArrayLists not with
ListItems but with some user defined structures similar to ListItems:

public sealed class ItemData
{
public string name;
public int val;

public ItemData(string name, int val)
{
this.name = name;
this.val = val;
}
}

Now I need to populate drop-down lists manually using code like this:

// list - an ArrayList containing ItemDatas
// ddl - the drop-down list control
for ( int i = 0; i < list.Count; i++ )
{
ddl.Items.Add( new ListItem( ((ItemData) list).Name, ((ItemData)
list).Id.ToString() ) );
}

What do you think about such a solution? Is it any better than passing
DataTable? For sure it is more cumbersome in terms of populating the lists,
but the database guy wants the smallest possible footprint and ItemData
items are light (they are structures). But on the other hand they are
elements of ArrayList that boxes the sturcture elements (that are value
types) to have reference types.

So my question is which option is better DataTable or the custom-defined
ItemData elements contained in an ArrayList?

Thanks,
Leszek
 
K

Kevin Spencer

I would go with a DataTable.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

Leszek Taratuta said:
Thanks a lot. It seems DataTable is the best solution.

In the meantime the database guy decided to fill ArrayLists not with
ListItems but with some user defined structures similar to ListItems:

public sealed class ItemData
{
public string name;
public int val;

public ItemData(string name, int val)
{
this.name = name;
this.val = val;
}
}

Now I need to populate drop-down lists manually using code like this:

// list - an ArrayList containing ItemDatas
// ddl - the drop-down list control
for ( int i = 0; i < list.Count; i++ )
{
ddl.Items.Add( new ListItem( ((ItemData) list).Name, ((ItemData)
list).Id.ToString() ) );
}

What do you think about such a solution? Is it any better than passing
DataTable? For sure it is more cumbersome in terms of populating the lists,
but the database guy wants the smallest possible footprint and ItemData
items are light (they are structures). But on the other hand they are
elements of ArrayList that boxes the sturcture elements (that are value
types) to have reference types.

So my question is which option is better DataTable or the custom-defined
ItemData elements contained in an ArrayList?

Thanks,
Leszek

Kevin Spencer said:
Hi Leszek,

First of all, as was mentioned earlier, ListItems are UI elements. A
well-designed Data Layer should NEVER work with UI elements. Why, even your
business layer shouldn't be working with UI elements. Only data structures
should come from the Data Layer. That would be raw primitve data, such as
integers, dates, and strings, DataSets, DataTables, and DataReaders. Now,
there is some controversy over returning DataReaders, so the next smallest
data structure (other than primitives) would be a DataTable, and it really
has a relatively small footprint. DataReaders are the smallest, but have
some issues, such as necessitating a constant Connection while in use, as
well as being forward-only, read-only, and not being serializable.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

object
like data
is
a need
to
 
L

Leszek Taratuta

Thanks a lot. I will use DataTables.

Leszek

Kevin Spencer said:
I would go with a DataTable.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

Leszek Taratuta said:
Thanks a lot. It seems DataTable is the best solution.

In the meantime the database guy decided to fill ArrayLists not with
ListItems but with some user defined structures similar to ListItems:

public sealed class ItemData
{
public string name;
public int val;

public ItemData(string name, int val)
{
this.name = name;
this.val = val;
}
}

Now I need to populate drop-down lists manually using code like this:

// list - an ArrayList containing ItemDatas
// ddl - the drop-down list control
for ( int i = 0; i < list.Count; i++ )
{
ddl.Items.Add( new ListItem( ((ItemData) list).Name, ((ItemData)
list).Id.ToString() ) );
}

What do you think about such a solution? Is it any better than passing
DataTable? For sure it is more cumbersome in terms of populating the lists,
but the database guy wants the smallest possible footprint and ItemData
items are light (they are structures). But on the other hand they are
elements of ArrayList that boxes the sturcture elements (that are value
types) to have reference types.

So my question is which option is better DataTable or the custom-defined
ItemData elements contained in an ArrayList?

Thanks,
Leszek

Kevin Spencer said:
Hi Leszek,

First of all, as was mentioned earlier, ListItems are UI elements. A
well-designed Data Layer should NEVER work with UI elements. Why, even your
business layer shouldn't be working with UI elements. Only data structures
should come from the Data Layer. That would be raw primitve data, such as
integers, dates, and strings, DataSets, DataTables, and DataReaders. Now,
there is some controversy over returning DataReaders, so the next smallest
data structure (other than primitives) would be a DataTable, and it really
has a relatively small footprint. DataReaders are the smallest, but have
some issues, such as necessitating a constant Connection while in use, as
well as being forward-only, read-only, and not being serializable.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

Thank you. I am going to talk with the guy who creates the data layer.

BTW
What could be the best alternative to get drop-down list data
sources
from
the data layer? DataTables are pretty heavy. Any other possibilities.

Thanks,

Leszek

Well, Leszek, it seems that the wrong person is asking this
question
of
the
newsgroup. Whoever is sending you ArrayLists of ListItems is the
one
who
needs the help!

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

Well, I know it is much easier to store data in serializable object
like
DataTable but I work in a team, and what I receive from the data layer
is
ArrayList containing ListItems.
For now the only resonable solution is to convert the ArrayLists to
DataTables. It is not efficient though. I thought it would be any
other
more
appropriate way to store this kind of data. Any kind of serialization
from
ArrayLists to strings or so, and then deserialization.

Thanks,
Leszek

I think you complicate things for no obvious reason :) I may be
totally
wrong, it all depends on particular situation, but this is
what
it is it.
Just is
the
such
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top