Reading and storing data to a list though session - c#

When i store an item, i can store it and read it. But when i try to store another item, the first one disappear.
I'm using a masterpage for my default site dont know if it affects it.
my code is very simple and looks like this.
List<kurvliste> kurv = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv.Add(new kurvliste(1,1,1, "Produktnavn"));
Session["kurv"] = kurv;
}
In my masterpage it looks like this
List<kurvliste> kurv = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
Repeater1.DataSource = kurv;
Repeater1.DataBind();
}

Because you have a bug - you declare the same name outside and inside the Page_load, so on the click the outside is used - so you actually use two different lists.
I write your code adding 1 & 2 to understand the bug.
List<kurvliste> kurv_1 = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv_2 = (List<kurvliste>)Session["kurv"];
if (kurv_2 == null)
{
kurv_2 = new List<kurvliste>();
Session["kurv"] = kurv_2; // Store the new list in the session object!
}
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv_1.Add(new kurvliste(1,1,1, "Produktnavn"));
Session["kurv"] = kurv_1;
}
How to solve it
But if i remove the outside, i get a null reference just by leaving it
to be List kurv.
No you need to remove the inside of Page_Load declaration that hides the outside one
protected void Page_Load(object sender, EventArgs e)
{
// on this line, just remove the declaration
// List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
// do it like
kurv = Session["kurv"] as List<kurvliste>;
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
}
Alternative
I suggest to move the list on the session to a property like that
// using this const you avoid bugs in mispelling the correct key.
const string ckurvlisteNameConst = "kurvliste_cnst";
public List<kurvliste> kurv
{
get
{
// If not on the Session then add it
if (Session[ckurvlisteNameConst] == null)
Session[ckurvlisteNameConst] = new List<kurvliste>();
// this code is not exist on release, but I check to be sure that I did not
// overwrite this Session with a different object.
Debug.Assert(Session[ckurvlisteNameConst] is List<kurvliste>);
return (List<kurvliste>)Session[ckurvlisteNameConst];
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv.Add(new kurvliste(1,1,1, "Produktnavn"));
}
a similar answer : How to store list of object into ViewState

Related

How to load previous ViewState when go back on previous page

I want to reload all my user changes when he want to go back on previous page and I tried to use LoadViewState() and SaveViewState() methods like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(Session["savedViewState"] != null)
{
Object saved = (Object)Session["savedViewState"];
LoadViewState(saved);
}
}
else
{
Session["savedViewState"] = SaveViewState();
}
}
but it did't work. The session savedViewState is null.
But I still do not know if these methods can solve the problem of reloading my all view state.
you need to switch the code between the if else conditions
protected void Page_Load(object sender, EventArgs e)
{
if(Session["savedViewState"] != null && SaveViewState()!=null)
{
Object saved = (Object)Session["savedViewState"];
LoadViewState(saved);
}
else
{
Session["savedViewState"] = SaveViewState();
}
}

Session Variable error bach booking

I set up a session variable on the Booking form.aspx as so:
protected void confirmImageButton_Click(object sender, ImageClickEventArgs e)
{
Session["confirmBooking"] = "confirm";
Session["beachBach"] = beachBachRadioButtonList.SelectedItem.Text;
}
and I transfer to my other page as so:
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
I'm trying to add 1 to the beachBach session variable whenever user press the confirm button....however, when i start to debug it, instead of adding 1, it add 2 to the label.
Can someone please help me out.. thanks
try this
protected void Page_Load(object sender, EventArgs e) {
{
if(!IsPostBack)
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
Well what happens after we come to this page ?? Do we have any event that causes postback in this page??
If there is than definitely it will add 1 again to your session .
Try using !IsPostBack property .

c# trying to access something from another method

i am new to this so hope u guys bear with me . i am trying to insert into database the URL directory of the filePathImage upon btnDone.
Partial Codes:
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
EnsureDirectoriesExist();
String filepathImage = (#"Images/Story/" +txtTitle.Text + "/" + e.FileName);
AjaxFileUpload1.SaveAs(Server.MapPath(filepathImage));
}
protected void btnDone_Click(object sender, EventArgs e)
{
act.ActivityName = dropListActivity.SelectedItem.Text;
act.Title = txtTitle.Text;
act.FileURL = filepathImage;
daoStory.Insert(act);
daoStory.Save();
}
i got a problem with filePathImage in act.FileURL = AjaxFileUpload1.filePathImage; Any advise or solutions will be grateful
try below, when upload complete you can put your path in to session and take that session path when you needed.
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
// your Code
Session["filepathImage"] = filepathImage ; // put the path in session variable
}
protected void btnDone_Click(object sender, EventArgs e)
{
if(Session["filepathImage"]!=null)
{
string filepathImage = Session["filepathImage"] as string;
// your code ...
}
}

SubmitChanges() not work when i change datagridview

nothing happen and nothing change when I use datagridview and SubmitChanges()
private void Form1_Load(object sender, EventArgs e)
{
this.book_infoDataGridView.DataSource = bookstore.book_info;
BindingSource bds=new BindingSource();
bds.DataSource = this.bookstore.book_info;
this.book_infoBindingSource = bds;
this.book_infobindingNavigator.BindingSource = bds;
}
private void save_Click(object sender, EventArgs e)
{
this.book_infoBindingSource.EndEdit();
bookstore.SubmitChanges();
}
I believe you're trying to .SubmitChanges() from your data in the DataGridView
private void save_Click(object sender, EventArgs e)
{
using (var bookstoreContext = new yourContext())
{
var b = (BookClass)bds.Current;
var book = bookstoreContext.Products.Single(c => c.BookId == b.BookId); //or .First()
book.BookName = b.BookName;
book.BookInfo = b.BookInfo;
bookstoreContext.SubmitChanges();
}
}

Textbox in apsx.cs file won't update a changed text

I have a web application consisting of an aspx-file.
On page load two textboxes are filled with data (a "username" and a "password"). This works.
On a button click it should save the textboxes' text. But for some reason the text of the textboxes isn't updated if I have changed it manually meanwhile (by typing in some letters with my keyboard).
Why is that? And how can I tell my program to regard my changes?
My code is:
protected void Page_Load(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
protected void Button_Speichern_Click(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
cm.setCredentials(TextBox_Benutzername.Text, TextBox_Passwort.Text);
}
EDIT:
It works with this improvement:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
For further information, see answers below. Thanks everyone!
Try checking for a postback -
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
Your Page_Load code will currently run after every button click (or postback), and overwrite the values you have manually added.
Try this,
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
You are assiging the value to the textboxes on every page load instead of firt page load.
Change the Page_Load method to :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
I think the problem is that you are creating a new CredentialsManager each and every time that the page is loaded (I assume that a new CredentialsManager has an empty Username and Password fields). You should only do that on new page loads, and not when the page is refreshed because of a button click. That is determined with the Page.IsPostBack property, so you moght need to do:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}

Categories