Displaying the values from the session array - c#

i am new to asp.net and c# . i am using a session array to store list of values in to the session. but when i try to display the values using response.write method, it does not give the exact values in the session instead it says "System.Collections.ArrayList". the code i used to add values in the session is shown below.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Cart"] == null)
{
Session["Cart"] = new ArrayList();
}
else
{
ArrayList cart = (ArrayList)Session["Cart"];
}
}
protected void addtocart_Click(object sender, EventArgs e)
{
int dealid = Convert.ToInt32(Request.QueryString.Get("deal"));
ArrayList cart1 = new ArrayList();
cart1 = (ArrayList)Session["Cart"];
cart1.Add(dealid);
Session["Cart"] = cart1;
Response.Write(Session["Cart"].ToString());
}
}
can anyone help me to display the values that are stored inside the session. please..

You can't response.write(Session["Cart"].Tostring()) because Session["Cart"] is ArrayList. You have to loopthrough that arraylist before response.write like below:
protected void addtocart_Click(object sender, EventArgs e)
{
int dealid = Convert.ToInt32(Request.QueryString.Get("deal"));
ArrayList cart1 = new ArrayList();
cart1 = (ArrayList)Session["Cart"];
cart1.Add(dealid);
Session["Cart"] = cart1;
ArrayList cart2 = new ArrayList();
cart2 = (ArrayList)Session["Cart"];
foreach(var item in cart2 )
{
Response.WriteLine(item);
}
}

You need to make a loop for show all value in the session.
private ArrayList cart
{
get
{
if (Session["Cart"] == null)
{
Session["Cart"] = new ArrayList();
}
return (ArrayList)Session["Cart"];
}
set
{
Session["Cart"] = value;
}
}
protected void addtocart_Click(object sender, EventArgs e)
{
int dealid = Convert.ToInt32(Request.QueryString.Get("deal"));
cart.Add(dealid);
foreach (var item in cart)
{
Response.Write(item.ToString());
}
}

Because you placed ArrayList into Session. And then it returned it to you when you call Session["Cart"]. If you want to display all items from that list, then you need to iterate them like this:
protected void addtocart_Click(object sender, EventArgs e)
{
int dealid = Convert.ToInt32(Request.QueryString.Get("deal"));
ArrayList cart1 = new ArrayList();
cart1 = (ArrayList)Session["Cart"];
cart1.Add(dealid);
Session["Cart"] = cart1;
// iterate items in session
ArrayList cart = Session["Cart"] as ArrayList;
foreach(var item in cart)
{
Response.Write(item.ToString());
}
}
One more thing, that using
List<int>
is more useful and better that ArrayList, because you don't need to cast objects to needed type.

There is no need to assign cart1 back to session in Session["Cart"] = cart1;. To print dealid, you need to write something like Response.Write(Session["Cart"][0].ToString());

Iterate over arraylist and build the necessary string.
StringBuilder sb = new StringBuilder();
foreach(var obj in cart1)
{
sb.Append(obj.ToString())
}
Response.Write(sb.ToString());

There is a debug functionality inside Visual Studio. Set breakpoints in your code. Run your project and step through your code using F10 or F11. Click on a value and see it's content. More information? See this

For display arraylist item from the array you use below code:
foreach (var item in (ArrayList)Session["Cart"])
{
Response.Write(item);
}

Related

How to filter the data bound to GridView control by the QueryString?

I am a new ASP.NET Web Forms developer and I am struggling now in with the best way of filtering the data by the value of QueryString before
binding it to the GridView control. I am binding the GridView to the GetData() method, and I would like to filter the data in the code-behind based
on the value of the QueryString if there is a QueryString. So should I do the checking of the QueryString in the BindGrid() method or in the
Page_Load() method? And how should I do it?
For your information, the GridView has a pagination capability as shown in the code-behind below.
Here's the C# code for the GetData():
public IEnumerable<Item> getData(Item itemObj)
{
List<Item> itemList = new List<Item>();
using (ATMSEntities context = new ATMSEntities())
{
itemList = (from item in context.Item
select new Item()
{
ItemId = item.ItemId,
Name = item.Name,
}).ToList();
if (itemObj.ItemId != 0)
{
itemList = itemList.Where(item => item.ItemId == itemObj.ItemId).ToList();
}
}
}
return itemList;
}
And here's the code-behind for the aspx page that has the GridView control:
protected void Page_Load(object sender, EventArgs e)
{
if(Request.QueryString["ItemId"] != null) //the filtration is not working here.
{
bindGrid();
}
}
private void bindGrid()
{
Item itemObj = new Item();
var result = itemObj.getData(itemObj).ToList();
gvItems.DataSource = itemObj.getData(itemObj);
gvItems.DataBind();
}
protected void gvItems_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvItems.PageIndex = e.NewPageIndex;
bindGrid();
}
Thanks in advance for your help.
You are not using the QueryString value to filter the list items. What you should be doing is this
private void bindGrid()
{
Item itemObj = new Item();
if(Request.QueryString["ItemId"] != null)
{
itemObj.ItemId = Convert.ToInt32(Request.QueryString["ItemId"]);
}
var result = itemObj.getData(itemObj).ToList();
gvItems.DataSource = itemObj.getData(itemObj);
gvItems.DataBind();
}

How to bind cascading Dropdown list using methods for countries and states in asp.net without using java script or Ajax

I need 2 Dropdown list's in which the second drop down list is populated based on the firstone without using any javascript or ajax and in how many ways can we achieve this? i tried using methods but i couldn't bind it.
here is what i tried
public List <string> indiacities()
{
List<string> l2 = new List<string>();
l2.Add("hyderabad");
l2.Add("calcutta");
l2.Add("chennai");
return l2;
}
public List<string> usacities()
{
List<string> l1 = new List<string>();
l1.Add("newyork");
l1.Add("new jersy");
l1.Add("texas");
return l1;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if(DropDownList1.SelectedValue=="0")
{
DropDownList2.DataSource=usacities();
DropDownList2.DataBind();
}
else if(DropDownList1.SelectedValue=="1")
{
DropDownList2.DataSource=indiacities();
DropDownList2.DataBind();
}
else{}
}
Here is an example, just to give you a general rough idea on how to assign a list as a datasource using your code.
List<string> UsaCities = new List<string>();
List<string> IndiaCities = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
UsaCities.Add("newyork");
UsaCities.Add("new jersy");
UsaCities.Add("texas");
IndiaCities.Add("hyderabad");
IndiaCities.Add("calcutta");
IndiaCities.Add("chennai");
DropDownList1.DataSource = UsaCities;
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
{
DropDownList2.DataSource = UsaCities;
DropDownList2.DataBind();
}
else if (DropDownList1.SelectedIndex == 1)
{
DropDownList2.DataSource = IndiaCities;
DropDownList2.DataBind();
}
}

Update Listbox without restarting program?

I'm building an WinForms application which is going to be an adressbook. I'm stuck with a problem though. When I open the program and press on my load contacts button, it loads all that's written in the txt file. But if I create a new contact and press load again, the new contact doesn't show up. Is there any way to fix this?
Also, when I try to create new methods for example a Delete() method. It says "Items collection cannot be modified when the DataSource property is set." Any ideas why is crashes?
List<string> Load()
{
StreamReader read = new StreamReader(path);
string row = "";
while ((row = read.ReadLine()) != null)
{
adressbook.Add(row);
}
read.Close();
return adressbook; //Adressbook is my List<string> adressbook = new List<string> uptop.
}
private void button2_Click(object sender, EventArgs e)
{
List<string> list = Load();
listBox1.DataSource = list;
}
You have to set to null the DataSource before clearing and binding:
private void button2_Click(object sender, EventArgs e)
{
if(listBox1.DataSource != null)
{
listBox1.DataSource = null;
listBox1.Items.Clear();
}
List<string> list = Load();
listBox1.DataSource = list;
}
In your Load you must first clear the list
List<string> Load()
{
if (adressbook.Count != 0)
{
adressbook.Clear();
}
StreamReader read = new StreamReader(path);
string row = "";
while ((row = read.ReadLine()) != null)
{
adressbook.Add(row);
}
read.Close();
return adressbook; //Adressbook is my List<string> adressbook = new List<string> uptop.
}

session variables in an ASP.NET

hi guy i am trying to place my session in to a drop down, any help would be great.
at the moment it puts the data in to a label, i wish to put it into a dropdown with it adding a new string every time i click button without getting rid of the last
default page
protected void Button1_Click1(object sender, EventArgs e)
{
Session["Fruitname"] = TbxName.Text; // my session i have made
}
output page
protected void Page_Load(object sender, EventArgs e)
{
var fruitname = Session["Fruitname"] as String; // my session ive made
fruit.Text = fruitname; // session used in lable
}
Have Tried
var myFruits = Session["Fruitname"] as List<string>;
myFruits.Add(listbox1.Text);
but i get error when i try to run the program
Broken glass thanks for your help, it is still not doing what i need but its getting there.
var fruitname = Session["Fruitname"] as String; // my session ive made
fruit.Text = string.Join(",", fruitname); // session used in lable
this is what is working. i need a dropdown to display all the strings put into TbxName.Text; to output into fruit
Just use a List<string> instead of a string then.
var myFruits = Session["Fruitname"] as List<string>;
myFruits.Add(TbxName.Text);
Has been fixed using code found else where
button page code bellow
protected void Button1_Click1(object sender, EventArgs e)
{
// Session["Fruitname"] = TbxName.Text; // my session i have made
MyFruit = Session["Fruitname"] as List<string>;
//Create new, if null
if (MyFruit == null)
MyFruit = new List<string>();
MyFruit.Add(TbxName.Text);
Session["Fruitname"] = MyFruit;
{
public List<string> MyFruit { get; set; }
}
page where display
protected void Page_Load(object sender, EventArgs e)
{
MyFruit = Session["Fruitname"] as List<string>;
//Create new, if null
if (MyFruit == null)
MyFruit = new List<string>();
ListBox1.DataSource = MyFruit;
ListBox1.DataBind();
}
public List<string> MyFruit { get; set; }
}

Dynamically add links to sharepoint webpart

Hi Stackoverflow community!
I'm working on a Sharepoint 2013 Server with Visual Studio 2012 and Windows Server 2012. So, I have to build a Webpart which should add a link via textbox to the GUI. Furthermore, it should be possible to add another link as well. After adding a new link, the whole collection of links should be displayed in a list. The problem is now: after adding a link, the site reloads. As a consequence the array, which stores the links, does only contain the latest link. All previous links are gone/deleted.
Here's my approach on this:
protected void Page_Load(object sender, EventArgs e) {
if (Page.IsPostBack) {
Events = new List<String>();
}
}
protected void btnAddLink_click(object sender, EventArgs e) {
AddToList();
List<String> links = Events;
foreach (String s in links) {
HyperLink link = new HyperLink();
link.NavigateUrl = s;
link.Text = s;
lnkPanel.Controls.Add(link);
}
foreach (String l in links) {
tbDescription.Text += l + "\n";
}
}
public List<String> Events {
get { return (List<String>)ViewState["HyperLinkList"]; }
set { ViewState["HyperLinkList"] = value; }
}
public void AddToList() {
List<String> events = Events; // Get it out of the viewstate
String l = tbLinks.Text; // tb = textbox (user input)
HyperLink link = new HyperLink();
link.NavigateUrl = tbLinks.Text;
link.Text = tbLinks.Text;
if (!events.Contains(link.NavigateUrl.ToString())) {
events.Add(l);
}
Events = events; // Add the updated list back into the viewstate
}
I hope someone can help me with my (maybe nooby) problem.
Ahhh you need this:
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
Events = new List<String>();
}
}
Each time the page is loaded, your wiping the contents of the list in viewstate. You need to add the ! to make sure it's not a postback.

Categories