Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am uploading an excel spreadsheet to a gridview using a datatable and storing the excel data into the datatable. however, due to pagination i am unable to get the name of every row under my gridview column when i use
(GridView1.Rows(i).Cells(x).Text.ToString
. i am doing an arraylist.add property on each row under column 1 however only the first page shows up but not the rest. Furthermore, when i add the datatable to a global variable class object which is also a datatable it is unable to read it in another function. I am not sure as to why this is occuring.
VB
Public Class GlobalVariable
    Public Shared Dttbl As New DataTable
End Class

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Call loadExcel()
        Call callGlobalVariable()
    End If
End Sub

 Public Function loadExcel()
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim taskTable As New DataTable

Dim filePath = Path.Combine(GlobalVariable.savedPath, GlobalVariable.excelFileName)

'Code to Use an Oledb Connection to get data from the Excel File
If File.Exists(filePath) Then
    MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties='Excel 12.0 Xml;HDR=YES;';")
    MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
    MyCommand.Fill(taskTable)
    'Persist the table in the Session object.
    'Below is what i am doing to copy the task table globally so i can use it in a function call. HOWEVER IT DOESN't Show in another function call. not sure why. 
    GlobalVariable.Dttbl = taskTable.Copy
    Session("TaskTable") = taskTable

    'Bind data to the GridView control.
    BindData()
    MyConnection.Close()
Return Nothing
End Function

another function i am creating like this and i am trying to call the globalvariable

VB
Public Function callGlobalVariable()
'The below shows up empty i am not sure why. But it is able to read above in the loadExcel() function. 
GlobalVariable.Dttbl.Rows(0).ItemArray(0).ToString()
Return Nothing
End Function


What I have tried:

I have tried the following code above but its giving me issues not sure what to do now.
Posted
Updated 8-Mar-17 4:46am
v9
Comments
Tomas Takac 8-Mar-17 2:59am    
You don't show the code related to what you are speaking about. You are supposed to post your code in the "What I have tried" field. Please use the Improve question button to do that.
JT1992 8-Mar-17 9:08am    
sorry about that. i am trying to create a variable which is global and using the things in the what have i tried field. thank you!

1 solution

If you think you need to create a "global variable" (which doesn't exist in .NET) you really need to rethink you're design. A global variable like you're describing doesn't get you anywhere and will not solve your problem.

HTTP is stateless. That means the next request the comes in doesn't remember anything at all from the previous request. Your Excel data doesn't exist. Web servers have amnesia. They don't remember what you were just talking about in the last request.

So, you need two parts. The first is you have to store the uploaded Excel workbook somewhere and have it uniquely identifiable so it can be found on the next request from some data in that request. This id value needs to be sent back to the client in whatever method is appropriate for your situation.

The next part is your code to handle the grid request for a page. This request must pass in the ID of the Excel workbook to use, the size of the page of data it needs (number of rows) and which page to return. This code would then have to go get the data from the Excel sheet based on this data and return the appropriate page of data. Remember, NOTHING from the previous request is remembered/loaded.
 
Share this answer
 
Comments
JT1992 8-Mar-17 11:50am    
yeah i realized that the moment i created a global variable class that its worthless to do that. thanks dave for the eye opener. i just realized how "memento" like the server really is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900