Answered by:
datagridview don't update after file.txt is updated via FTP

Question
-
User-350763668 posted
Hi, i have a simple file .txt in the server that populates a global list(of myobject).
In the page_init event the gridview is binded to list with:
txt_load() ' this sub reads line by line the .txt file, clear mygloballist and repopulate it with content of txtfile. gridview1.datasource=mygloballist gridview1.databind()
the first time that i publish the application everything works right (the gridview shows the list correctly.)
If i update the .txt file via ftp and i refresh page, (or i close the browser and i go in the page) the gridview still shows me the old content of file!
Even if i DELETE the .txt file (via ftp) and i go on the page, i NOT get any error (file not found for example) and the gridview always show me the old content! Why???? I need the gridview show me the new content of .txt file after updating via ftp, and i need a error if file don't exist. Thanks.
Wednesday, October 14, 2020 10:48 PM
Answers
-
User-1330468790 posted
Hi Uruse1980,
Could you please share more details so that I could try to reproduce the problem? (Or Foo/bar stuff as a replacement for secure data)
- Version of ASP.NET framework, VS and browser.
- What does the method "txt_load()" do? How it load data from the file? Any return value and type?
- What is the variable "mygloballist"? Is it a datatable?
- Have you tried to prevent caching of your web form pages? (Add below lines in Page_Load function)
Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetAllowResponseInBrowserHistory(false);
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 15, 2020 6:41 AM -
User753101303 posted
Maybe a caching issue? I would show the current date/time on the page to see if the value is updated when refreshing the page.
If ok and it still runs when this file is deleted, the only reason I can see is that this file is not needed. I would show perhaps the file size or last modified date on the page to see if it seems consistent with changes done through FTP.
BTW it is uploaded on the web server and you have a single web server?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 16, 2020 12:17 PM
All replies
-
User-1330468790 posted
Hi Uruse1980,
You should use the Page_Load event to bind the data to controls. Init and Load events have different behaviors in webforms life cycle.
- Page load event fires whenever the page is loaded for the first time, as well as when the page is posted back.
- However, the init event wont fire on postbacks
So if you really need some early initialization of a control then you can use init event.
Hope this helps.
Best regards,
Sean
Thursday, October 15, 2020 2:32 AM -
User-350763668 posted
Thanks Sean, i have tried, but same result in page_load event
Thursday, October 15, 2020 5:40 AM -
User-1330468790 posted
Hi Uruse1980,
Could you please share more details so that I could try to reproduce the problem? (Or Foo/bar stuff as a replacement for secure data)
- Version of ASP.NET framework, VS and browser.
- What does the method "txt_load()" do? How it load data from the file? Any return value and type?
- What is the variable "mygloballist"? Is it a datatable?
- Have you tried to prevent caching of your web form pages? (Add below lines in Page_Load function)
Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetAllowResponseInBrowserHistory(false);
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 15, 2020 6:41 AM -
User-350763668 posted
Private Sub componi_Load(sender As Object, e As EventArgs) Handles Me.Load Dim nome As String = G_utente_loggato.nome
lblutente.Text = "Ciao " & nome & "!" carica_alimenti("01") GridView1.DataSource = G_listaalimenti GridView1.DataBind() Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.Cache.SetAllowResponseInBrowserHistory(False) End SubPublic Sub carica_alimenti(filiale As String) G_listaalimenti.Clear() Dim vettore() As String Dim rigaletta As String = "" Dim lettorefile As New StreamReader(Server.MapPath("/pranziveloci01app/Database/" & filiale & "/alimenti.txt")) Do Dim alim As New Alimento rigaletta = lettorefile.ReadLine If IsNothing(rigaletta) Then Exit Do vettore = Split(rigaletta, "*") alim.Nome = vettore(0) alim.prezzox100g = vettore(1) alim.caloriex100g = vettore(2) alim.carboidratix100g = vettore(3) alim.proteinex100g = vettore(4) alim.grassix100g = vettore(5) alim.conversione_peso = vettore(6) alim.Disponibile = vettore(7) alim.Peso_standard = vettore(8) alim.IVA = vettore(9) alim.Pesocrudo = alim.Peso_standard alim.calcola_valori() G_listaalimenti.Add(alim) Loop lettorefile.Close() End Sub
Thanks Sean, i have tried yor second solution, but with same result. Datagridview display All data, but if i update alimenti.txt via FTP the data displayed DONT CHANGE, even, if i DELETE the file from server i take no error ad the data displayed don't change.
G_listaalimenti is a public BindingList(Of Alimento), alimento is a simple object with various Properties. Here is the code:
Public Class Alimento Public Property Nome As String Public Property Pesocrudo As Double '*********** Public Property Pesocotto As Double '*********** Public Property Carboidrati As Integer '*********** Public Property Proteine As Integer '*********** Public Property Grassi As Integer '*********** Public Property Calorie As Integer '*********** Public Property Prezzo As Double '*********** Public Property prezzox100g As Double Public Property caloriex100g As Double Public Property carboidratix100g As Double Public Property proteinex100g As Double Public Property grassix100g As Double Public Property conversione_peso As Double Public Property Disponibile As Boolean = True Public Property Peso_standard As Integer Public Property IVA As String Public Sub calcola_valori() 'tutto calcolato in base a pesocrudo If Me.Nome = "TOTALE PIATTO:" Then Exit Sub If Me.Nome = "TOTALE ORDINE:" Then Exit Sub Me.Pesocotto = Math.Round(Me.Pesocrudo * Me.conversione_peso, 0, MidpointRounding.AwayFromZero) Me.prezzox100g = Math.Round(Me.prezzox100g, 2, MidpointRounding.AwayFromZero) Me.Prezzo = Math.Round(Me.prezzox100g * Me.Pesocrudo / 100, 2, MidpointRounding.AwayFromZero) Me.Calorie = Int(Me.caloriex100g * Me.Pesocrudo / 100) Me.Carboidrati = Int(Me.carboidratix100g * Me.Pesocrudo / 100) Me.Proteine = Int(Me.proteinex100g * Me.Pesocrudo / 100) Me.Grassi = Int(Me.grassix100g * Me.Pesocrudo / 100) End Sub end class
here is the declaration of G_listaalimenti:
Public G_listaalimenti As New BindingList(Of Alimento)
Here are some lines of the file "alimenti.txt" :
Anguria*5*65*100*20*45*1*True*50*01*
Farro*2*338*70*15*2*2,5*True*100*01*
Penne*2*356*83*11*0*2*True*100*01*
Pesto*1,3*491*87*67*475*1*True*20*01*
Petto di Pollo grigliato*3,5*97*0*22*1*0,9*True*100*01*
Piselli*1,5*54*75*57*4*0,87*True*30*01*
Riso*3,2*364*793*67*1*2,5*True*80*01*
Riso Basmati*2*364*79*7*1*10*True*80*01*
Trivelle Integrali*2*324*66*13*2*2*True*500I am using VISUAL STUDIO 2015 with VB.NET
I have partially solved the problem with:
Session.timeout = 1
in this way, strangely, if I change the page and immediately return to it, I have the updated data despite the fact that the minute has not passed. But shortening the session to one minute creates other problems that we can imagine .... honestly I would prefer a better way than this. Some idea?
Thursday, October 15, 2020 7:02 PM -
User-1330468790 posted
Hi Uruse1980,
I notice that you are using componi_Load. Is this similar to Page_Load? Why is it showing a different name?
I have tested the codes you provided and successfully update the page with the data from the .txt file.
Currently I have no idea but have to suspect if you have update the file.txt successfully via FTP.
You could refer to the codes below and try the demo from your side to see if it works as from my side.
ReadDataFromFile.aspx
<form id="form1" runat="server"> <h3><asp:Label ID="lblutente" runat="server"></asp:Label></h3> <div> <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="true"> </asp:GridView> </div> <asp:Button ID="postback" runat="server" Text="Post back" /> </form>
Code behind:
Public G_listaalimenti As New BindingList(Of Alimento) Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init ' If you put codes in Page_Load in Page_Init, then the post back button won't update the data. End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim nome As String = "Read Data From Text File" lblutente.Text = "Ciao " & nome & "!" carica_alimenti("01") GridView1.DataSource = G_listaalimenti GridView1.DataBind() Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.Cache.SetAllowResponseInBrowserHistory(False) End Sub Public Sub carica_alimenti(filiale As String) G_listaalimenti.Clear() Dim vettore() As String Dim rigaletta As String = "" Dim lettorefile As New StreamReader(Server.MapPath("/Uploads/" & filiale & "/alimenti.txt")) Do Dim alim As New Alimento rigaletta = lettorefile.ReadLine If IsNothing(rigaletta) Then Exit Do vettore = Split(rigaletta, "*") alim.Nome = vettore(0) alim.prezzox100g = vettore(1) alim.caloriex100g = vettore(2) alim.carboidratix100g = vettore(3) alim.proteinex100g = vettore(4) alim.grassix100g = vettore(5) alim.conversione_peso = vettore(6) alim.Disponibile = vettore(7) alim.Peso_standard = vettore(8) alim.IVA = vettore(9) alim.Pesocrudo = alim.Peso_standard alim.calcola_valori() G_listaalimenti.Add(alim) Loop lettorefile.Close() End Sub
alimento.vb
Public Class Alimento Public Property Nome As String Public Property Pesocrudo As Double '*********** Public Property Pesocotto As Double '*********** Public Property Carboidrati As Integer '*********** Public Property Proteine As Integer '*********** Public Property Grassi As Integer '*********** Public Property Calorie As Integer '*********** Public Property Prezzo As Double '*********** Public Property prezzox100g As Double Public Property caloriex100g As Double Public Property carboidratix100g As Double Public Property proteinex100g As Double Public Property grassix100g As Double Public Property conversione_peso As Double Public Property Disponibile As Boolean = True Public Property Peso_standard As Integer Public Property IVA As String Public Sub calcola_valori() 'tutto calcolato in base a pesocrudo If Me.Nome = "TOTALE PIATTO:" Then Exit Sub If Me.Nome = "TOTALE ORDINE:" Then Exit Sub Me.Pesocotto = Math.Round(Me.Pesocrudo * Me.conversione_peso, 0, MidpointRounding.AwayFromZero) Me.prezzox100g = Math.Round(Me.prezzox100g, 2, MidpointRounding.AwayFromZero) Me.Prezzo = Math.Round(Me.prezzox100g * Me.Pesocrudo / 100, 2, MidpointRounding.AwayFromZero) Me.Calorie = Int(Me.caloriex100g * Me.Pesocrudo / 100) Me.Carboidrati = Int(Me.carboidratix100g * Me.Pesocrudo / 100) Me.Proteine = Int(Me.proteinex100g * Me.Pesocrudo / 100) Me.Grassi = Int(Me.grassix100g * Me.Pesocrudo / 100) End Sub End Class
Demo:
Hope helps.
Best regards,
Sean
Friday, October 16, 2020 7:01 AM -
User-350763668 posted
Hi Sean, thanks a lot for your time.
"Componi" is the name of my page (www.fooexample/componi.aspx)
When i double click in page form, visual studio 2015 automatically create for me the "componi_load" sub, and NOT "page_load", when i go at INIT event, visual studio create a sub called "componi_init" ecc.......
I have no problem with FTP upload, the file REALLY is updated.
Have you tried to navigate to another page and come back instead of postback?
I noticed that you edit the file locally, if you install the code in real remote webserver and you upgrade file via FTP, i think
probably you have my same problem! Thanks againFriday, October 16, 2020 11:08 AM -
User753101303 posted
Maybe a caching issue? I would show the current date/time on the page to see if the value is updated when refreshing the page.
If ok and it still runs when this file is deleted, the only reason I can see is that this file is not needed. I would show perhaps the file size or last modified date on the page to see if it seems consistent with changes done through FTP.
BTW it is uploaded on the web server and you have a single web server?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 16, 2020 12:17 PM