I have an arraylist and i define it as public variable
Public StockArray As New ArrayList
inside button1_click body
i append new elements in that Stockarray
by
dim stockobj as new stock // stock is a class
StockArray.Add(stockobj)
when i print any element of Stockarray within button1 body it works fine
but when i call the stockArray in a another button body the StockArray is empty !
any ideas will be Extremly helpful
9 1659
I have an arraylist and i define it as public variable
Public StockArray As New ArrayList
inside button1_click body
i append new elements in that Stockarray
by
dim stockobj as new stock // stock is a class
StockArray.Add(stockobj)
when i print any element of Stockarray within button1 body it works fine
but when i call the stockArray in a another button body the StockArray is empty !
any ideas will be Extremly helpful
Are you sure you placed StockArray in the correct place? Works for me: - Public Class Form1
-
Public StockArray As New ArrayList
-
Public iCounter As Integer = 0
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
iCounter = iCounter + 1
-
Dim s As New Stock
-
s.ID = "A-" & iCounter
-
StockArray.Add(s)
-
ListStock()
-
End Sub
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
iCounter = iCounter + 1
-
Dim s As New Stock
-
s.ID = "B-" & iCounter
-
StockArray.Add(s)
-
ListStock()
-
End Sub
-
Private Sub ListStock()
-
Dim s As Stock, msg As String = ""
-
For Each s In StockArray
-
msg = msg & s.ID & ", "
-
Next
-
msg = Strings.Left(msg, Len(msg) - 2)
-
MsgBox("ArrayList contains " & msg)
-
End Sub
-
End Class
-
Public Class Stock
-
Public ID As String
-
End Class
I have an arraylist and i define it as public variable
Public StockArray As New ArrayList
inside button1_click body
i append new elements in that Stockarray
by
dim stockobj as new stock // stock is a class
StockArray.Add(stockobj)
when i print any element of Stockarray within button1 body it works fine
but when i call the stockArray in a another button body the StockArray is empty !
any ideas will be Extremly helpful
Hi,
Is it a web application you are working on? If it is so, I think the problem is caused by the postback. When you do a postback from the first button click, - Public StockArray As New ArrayList
will be called again and it will create a new empty StockArray object. You can use the following code to instanciate the StockArray only in the initial page load. - if(!IsPostback)
-
Public StockArray As New ArrayList
Hope this will help.
thanks alot SammyB but this is exactly my problem the buttons ommit each other changes
and YES channaJ i'm using a web application i'm interested in your sollution
but shall I declare StockArray only once in the page_load ? because when I did the buttons could not see StockArray declaration!
and it force me to replace -
-
If (Not IsPostBack) Then
-
Public StockArray as new ArrayList
-
End IF
-
with this -
-
If (Not IsPostBack) Then
-
Dim StockArray as new ArrayList
-
End IF
-
and when I declare StockArray inside the body of Page_Load and inside the body of page Class the Array became empty :)
I'm So sorry but I'm new to asp.net with VB
> I'm So sorry but I'm new to asp.net with VB
You are better than I, I've never created a web form, so Chan will have to help you there.
But, notice where I defined StockArray. It is inside the Form class, but outside of all of the Sub's. Thus, there is just one of them and all of the Sub's can access it. You should have a similar structure in your code.
Hi,
Is it a web application you are working on? If it is so, I think the problem is caused by the postback. When you do a postback from the first button click, - Public StockArray As New ArrayList
will be called again and it will create a new empty StockArray object. You can use the following code to instanciate the StockArray only in the initial page load. - if(!IsPostback)
-
Public StockArray As New ArrayList
Hope this will help.
***********************
Hello channaJ,
I did exactly what you siad but declaring StockArray as an ArrayList inside the page_load makes StockArray undeclared to other subs inside the page calss
as you siad when not post back the array contents will be empty ,i checked that by printing the array count (StockArray.Count) and the result is zero :(
what should i do i need the array to save pervious valuse of stocks and use them when Undoing last changes.
thanks in advance
Hello every body
can any one help me with this issue i waste lots of time on it but still could not find the right sullotion any ideas :) !
regards
I have an arraylist and i define it as public variable
Public StockArray As New ArrayList
inside button1_click body
i append new elements in that Stockarray
by
dim stockobj as new stock // stock is a class
StockArray.Add(stockobj)
when i print any element of Stockarray within button1 body it works fine
but when i call the stockArray in a another button body the StockArray is empty !
any ideas will be Extremly helpful
Hi there!
When creating a web application you have to remember that every time the user clicks a button (or causes a postback event) its as if they are coming to the website for the first time.
In your case, you're creating the stockArray ArrayList, but you are only filling it within the method that handles your button click event. Once that information is sent back to the user, all the objects are destroyed.
There is never a persistent connection to the user and the application.
This means that when the user does something else that causes a postback a new stockArray (ArrayList) is created.
In order to use information collected from a previous postback to the website you need to store your arrayList in a persistent data store....like Session.
(please note that my code is in VB.NET and is just a guideline...it probably wont work) -
-
Private stockArray As ArrayList
-
-
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
-
-
'Since this is executed first every time we fill our stockArray
-
'variable with what ever is in the Session from previous visits
-
'this way we can use it in the subs that handle the events
-
-
stockArray = new ArrayList
-
-
Try
-
stockArray=CType(Session("arrayListInStore"),ArrayList)
-
-
If stockArray Is Nothing
-
'This is the case when the user is first visiting the website or the
-
'Session has expired and there is no arrayList stored in session
-
-
stockArray = new ArrayList
-
-
'here fill your stock array with default values if you'd like
-
End If
-
Catch ex As Exception
-
-
End Try
-
-
End Sub
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
'Here you can fill the stockArray Array list but make sure that you
-
'Store this in session so that you can use this data later
-
-
'I always double check to make sure the object has been intialized
-
If stockArray Is Nothing Then
-
stockArray = new ArrayList
-
End IF
-
-
stockArray.Add(....)
-
Session("arrayListInStore")=stockArray
-
-
End Sub
-
-
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
'Since you've already filled the stockArray variable in the page_load method
-
'with the information gathered during the Button1 postback (which was
-
'stored in session)...
-
'You will be able to use the information in the stockArray variable in
-
'this method
-
-
lbl_output.Text = stockArray.Length.ToString()
-
-
End Sub
-
Does this make sense?
(Oh yeah, you don't need to make the variable Public unless its being used by another object. I'd recommend making it Private if its not being used by another object)
Cheers
-Frinny
pew! Finally i found it instead of declaring StockArray as public or dim declare it as shared
thanks Chan and Sammy for your time and effort :)
here is the code -
-
-
Shared StockArray As New ArrayList
-
-
'***********************************************************'
-
Public Sub Update_Group(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
-
'Loop through each DataGridItem, and determine which CheckBox controls
-
'have been selected.
-
-
-
Dim GridItem As DataGridItem
-
For Each GridItem In DataGrid1.Items
-
-
Dim myCheckbox As CheckBox = CType(GridItem.Cells(0).Controls(1), CheckBox)
-
Dim TextBox1 As TextBox = CType(GridItem.Cells(4).Controls(1), TextBox)
-
Dim TextBox2 As TextBox = CType(GridItem.Cells(5).Controls(1), TextBox)
-
-
If myCheckbox.Checked = True Then
-
-
Dim Stockobj As New stock
-
Stockobj.FunCode = DataGrid1.DataKeys(GridItem.ItemIndex).ToString()
-
Stockobj.FunPrice = Convert.ToDouble(TextBox1.Text)
-
Stockobj.FunVolume = Convert.ToInt64(TextBox2.Text)
-
StockArray.Add(Stockobj)
-
' Response.Write(Stockobj.FunCode)
-
' rowCount += 1
-
DoUpdateStock(DataGrid1.DataKeys(GridItem.ItemIndex).ToString(), Convert.ToDouble(TextBox1.Text), Convert.ToInt64(TextBox2.Text))
-
End If
-
Next
-
updateDate.Text = "Last Update was on " & Now()
-
-
-
BindDataGrid()
-
End Sub
-
-
-
-
Public Sub Undo_Update(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
-
-
Dim i As Integer = 0
-
-
While (i < StockArray.Count - 1)
-
-
DoUpdateStock(StockArray(i).FunCode, StockArray(i).FunPrice, StockArray(i).FunVolume)
-
i += 1
-
End While
-
-
updateDate.Visible = False
-
-
BindDataGrid()
-
-
End Sub
-
-
Hi there!
When creating a web application you have to remember that every time the user clicks a button (or causes a postback event) its as if they are coming to the website for the first time.
In your case, you're creating the stockArray ArrayList, but you are only filling it within the method that handles your button click event. Once that information is sent back to the user, all the objects are destroyed.
There is never a persistent connection to the user and the application.
This means that when the user does something else that causes a postback a new stockArray (ArrayList) is created.
In order to use information collected from a previous postback to the website you need to store your arrayList in a persistent data store....like Session.
(please note that my code is in VB.NET and is just a guideline...it probably wont work) -
-
Private stockArray As ArrayList
-
-
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
-
-
'Since this is executed first every time we fill our stockArray
-
'variable with what ever is in the Session from previous visits
-
'this way we can use it in the subs that handle the events
-
-
stockArray = new ArrayList
-
-
Try
-
stockArray=CType(Session("arrayListInStore"),ArrayList)
-
-
If stockArray Is Nothing
-
'This is the case when the user is first visiting the website or the
-
'Session has expired and there is no arrayList stored in session
-
-
stockArray = new ArrayList
-
-
'here fill your stock array with default values if you'd like
-
End If
-
Catch ex As Exception
-
-
End Try
-
-
End Sub
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
'Here you can fill the stockArray Array list but make sure that you
-
'Store this in session so that you can use this data later
-
-
'I always double check to make sure the object has been intialized
-
If stockArray Is Nothing Then
-
stockArray = new ArrayList
-
End IF
-
-
stockArray.Add(....)
-
Session("arrayListInStore")=stockArray
-
-
End Sub
-
-
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
'Since you've already filled the stockArray variable in the page_load method
-
'with the information gathered during the Button1 postback (which was
-
'stored in session)...
-
'You will be able to use the information in the stockArray variable in
-
'this method
-
-
lbl_output.Text = stockArray.Length.ToString()
-
-
End Sub
-
Does this make sense?
(Oh yeah, you don't need to make the variable Public unless its being used by another object. I'd recommend making it Private if its not being used by another object)
Cheers
-Frinny
--------------------------
Hi Firnny
you know i have read lots of articles talking about session I'll try your code coze the code i have solves some problems not all of them
thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Chris Plowman |
last post by:
Hi all,
I was wondering if anyone can help me with a really annoying problem I have
been having. I made a derived datagrid class that will select the row when
a user clicks anywhere on a cell...
|
by: xmail123 |
last post by:
In a web service I am using a SqlDataReader to retrieve the
information to populate an ArrayList.
public ArrayList ReadHistory(int ItemPK)
{
SDrdr = SDcmd.ExecuteReader();
ArrayList AL = new...
|
by: Sharon |
last post by:
Hi all.
I have an ArrayList and
sometimes while enumerating through, i get an
exception because another thread has added to the ArrayList.
To solve this problem, i lock the enumeration,
passing...
|
by: Stephen |
last post by:
I have the following code working in order to create an array list and
populate a datagrid however everytime i click my button the first item in the
array and the first row in the datagrid are...
|
by: Marcus Kwok |
last post by:
I am having a weird problem with my DataGrid that is bound to an
ArrayList. My situation is as follows:
I have two DataGrids on a modal form. The top grid populates an
ArrayList from a file,...
|
by: Jarod |
last post by:
Hey
I change backColor of linkButton in my Page_Load:
lbDoSth.BackColor = Color.Red;
But I have multiView on this page. On one of the views I have detailsView.
When I add a new row a set...
|
by: Arjen |
last post by:
Hi,
I have a public class 'settings' with a public static void
'loadsettings'.
Inside 'loadsettings' I go to public static 'loadpagesettings'. Inside
this function I have a problem.
1. I...
|
by: garyusenet |
last post by:
I have had no replies to my previous post so perhaps I didn't write it
good enough.
Please excuse new thread but i wanted to break from the last thread
hopefully this thread will be better.
...
|
by: thanigaimani83 |
last post by:
Hi guys.
i can explain for my doubt with example.see.my table have multiple type of employee details like as Mangemment,Programmer,Designers,Accountants,ContractEmployees.Each type of department...
|
by: Johan Öhrn |
last post by:
Hi,
I'm working with an asp.net 2.0 application.
The application I'm working with come with it's own assemblies which calls
out to code in my assembly.
It may call out to my code during a...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |