How do you Programmatically store values in a WebPart?

I need to have a per user DataTable Programmatically stored in a web-part.
Rahsaan-PringleAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Rahsaan-PringleConnect With a Mentor Author Commented:
It's good to know I'm not entirely crazy. I did try that. It says you can't convert DTs to strings.
52E43607-FDE0-4507-9993-B3DAB520.png
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Session? ViewState?

If you don't want to use the server's memory you can  use a Sql Table where user is part of the table itself.   It allows you to then
select * from table where user = @user

0
 
Bryan ButlerCommented:
From here:
http://msdn.microsoft.com/en-us/library/e0s9t4ck.aspx

I see this:
Creating a Data-Bound Web Parts Control
http://msdn.microsoft.com/en-us/library/ms227438.aspx

And here a code sample from a link on that page:
http://msdn.microsoft.com/en-us/library/ms227548.aspx
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Rahsaan-PringleAuthor Commented:
I originally planned to store the data in the database, because it's tabular in nature (two columns, x rows). I have been directed to avoid using the database. As far as I know, cookies aren't suitable because it's tabular, and Session isn't good because the data needs to persist between sessions. Data-Bounding isn't suitable because I have been told not to use the database.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
If data needs to be persisted between sessions than database is definitely the way to go.  Cookies should be able to store that data depending on the number of rows though.  

0
 
Rahsaan-PringleAuthor Commented:
I agree about the database. How do you stick a table in a cookie?
0
 
Rahsaan-PringleAuthor Commented:
I agree about the database. How do you stick a DataTable in a cookie?
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
same way would the session.  Just use create a cookie object.

http://msdn.microsoft.com/en-us/library/78c837bd.aspx#Y239


HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);


myCookie["Datatable"] = dt;
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Would do it the hard way then.

Make a delimited string:

String tmp ="";

for each row in dt.Rows
  tmp += row.Columns[0] + "|" + row.Columns[1] + "|" . . .  "~";

//write to cookie

then on load do spilts:

int i;
for each string tmpRow in  myCookie["dt"].ToString().Split("!")
{
   i = 0;
   DataRow row = dt.NewRow();
   for each string col in tmpRow.Split("|")
   {
     row[ i ] = col;
     i++;
   }
     dt.Rows.Add(row);
}

     
0
 
Bryan ButlerCommented:
Just remember the max cookie size is 4k.
0
 
Rahsaan-PringleAuthor Commented:
I had to try something else.
0
 
Rahsaan-PringleAuthor Commented:
zzz
0
All Courses

From novice to tech pro — start learning today.