|
|
Hi,
I'm trying to save save a user preference via a cookie. This should be simple, right? Not!
To set the cookie, I use the following code:
Response.Cookies("TestCookie").Value = "test"
Response.Cookies("TestCookie").Expires = DateTime.Now.AddYears(30)
I then have another page - in the same project and domain - where I try to retrieve the cookie:
response.write("Cookie value is: " & Response.Cookies("TestCookie").Value)
This returns a null value for the cookie.
What am I doing wrong?
Thanks,
Kevin
|
|
|
|
|
First you should check wether the browse supports cookies and has cookies enabled
If Request.Browser.Cookies Then
Then you should check wether the cookie is available, or if you want to create a new cookie... create a cookie
' Create a new cookie<br />
HttpCookie hcMyCookie = new HttpCookie("TextCookie", "test")<br />
' set expiration date<br />
hcMyCookie.Expires = DateTime.Now.AddMonths(1)<br />
Now you have your cookie, but don't forget to send it back to the visitor...
Response.Cookies.Add(hcMyCookie)
And then close the 'If' section
End If
|
|
|
|
|
Eduard,
Thanks. Your example - with a few modifications - works for the session, but not when the user returns to the page.
If Request.Browser.Cookies Then
' Create a new cookie
Dim hcMyCookie As New HttpCookie("TestCookie", "test")
' set expiration date
hcMyCookie.Expires = DateTime.Now.AddMonths(1)
Response.Cookies.Add(hcMyCookie)
Else
Response.Write(" Cookies not supported ")
End If
Response.Write(Response.Cookies("TestCookie").Value)
If I set the cookie and then comment out all the above code except for the last line, the cookie goes away. It seems that the cookie only lasts for the session. How do I get the cookie to last longer than that?
Thanks
|
|
|
|
|
I figured it out...
The following code retrieves the value of the cookie:
Dim cookieCols As New HttpCookieCollection
cookieCols = Request.Cookies
Response.Write(cookieCols(0).Value)
|
|
|
|
|
Tip: since cookiecols is receiving a value... no need to create a new instance before it receives a value (remove NEW)
Dim cookiecols as httpcookiecollection = Request.Cookies
Response.Write(cookieCols(0).Value)
unless you plan on doing something else with cookiecols no need to create that variable either
Response.Write(Request.Cookies(0).Value)
Just trying to help
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous
'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
|
|
|
|
|
I have a problem with my code that I need help on. I think I know what the problem is just not how to fix it.
Here it is: Background:
I am programming a barecode scanner that accesses network files. I am coding a way to save the files locally when the next work is down. I successfully save the files locally and put a link on the mainmenu that signals to the user that there are local files that need to be uploaded to the network. When the user clicks on the link the program trys to write to the network files.
Here is the problem. When I have network access and click on the link it says that the local file is in use. But if I minimize program window and then bring it up again, it works. Is this refreshing the form or something. I tried a refresh (I think) and it did not work.
Does anyone know what I can do?
|
|
|
|
|
Cory Kimble wrote: When I have network access and click on the link it says that the local file is in use.
What file?? How is it written?? What does the code you use to do this look like?? Are the files being closed properly?? Are you Disposing stream objects after you're done with them??
Cory Kimble wrote: But if I minimize program window and then bring it up again, it works.
Minimizing a form forces a garbage collection which may explain this. It sounds like you're not dispoing of some resource properly after you're done with it.
|
|
|
|
|
Disposing stream objects after I am done with them? I do a .close .dispose is not available. How do I dispose of it? I am using .net compact framework.
|
|
|
|
|
Cory Kimble wrote: Disposing stream objects after I am done with them? I do a .close .dispose is not available.
Whoops! My bad. I've been hanging around with the Graphics classes too much today.
There is no Dispose on the streams. You just Flush (if needed) and Close them. But, from the sounds of your problem, it appears that a stream isn't getting closed.
What files are these?? Images?? What does the code look like that's reading/writing them??
|
|
|
|
|
Hi Dave,
AFAIK a Flush() is implied by a Close(), so it makes no sense to explicitly Flush()
just before a Close(). The only time Flush() is useful is when you don't intend
to Close() but instead just want to make sure everything up till then is actually
written to the file before you continue.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I try to write to file on network
Try
swship = New StreamWriter(shipFile, True)
swship.WriteLine(frmShip.cmbobxLocation.Text + "," + frmShip.txtbxScanID.Text + "," + frmShip.cmbobxBuyer.Text + "," + dt.ToString("d"))
swship.Close()
Catch ex As Exception
writeLocal()
End Try
WriteLocal:
Try
Dim swshipCatch As StreamWriter = New StreamWriter(localShipFile, True)
swshipCatch.WriteLine(frmShip.cmbobxLocation.Text + "," + frmShip.txtbxScanID.Text + "," + frmShip.cmbobxBuyer.Text + "," + "," + dt.ToString("d"))
swshipCatch.Flush()
swshipCatch.Close()
Catch ex As Exception
MessageBox.Show("writeLocal failed")
End Try
The above all works Fine.
The Below is when I attempt to access the localShipFile
Try
'SHIP
frmsr = New StreamReader("program files\steidingerfoodsce\localship.txt")
frmsw = New StreamWriter("\\lit3\device\ship.txt", True)
Do While frmsr.Peek() >= 0
frmsw.WriteLine(frmsr.ReadLine())
Loop
frmsr.Close()
frmsw.Close()
file.ReWriteSHIPFile() This is where it fails!!!
MessageBox.Show("SHip done")
Catch ex As Exception
MessageBox.Show(ex.ToString)
MessageBox.Show("Network connection not established")
End Try ReWriteSHIPFIle()
Public Sub ReWriteSHIPFile()
dim swship as streamwriter = New StreamWriter(localShipFile, False) ----It fails when this line executes.
swship.Write("")
swship.Close()
End Sub
modified on Monday, March 24, 2008 4:20 PM
|
|
|
|
|
Cory Kimble wrote: frmsr = New StreamReader("program files\steidingerfoodsce\localship.txt")
You're really using a hard-coded path that assumes the current directory is C:\ ?? Change this to use a fully qualified path built from known sources, such as Application.StartupPath , the System.Io.Path class, and other methods of determining the true path to well-known folders.
|
|
|
|
|
Hi Cory,
if it still does not work, just show us the code with all the opens, writes,
flushes, closes, disposes, what have you.
BTW: please use PRE tags (not CODE tags) for multi-line code snippets; they
preserve formatting such as indentation, much easier to read.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I try to write to file on network
Try
swship = New StreamWriter(shipFile, True)
swship.WriteLine(frmShip.cmbobxLocation.Text + "," + frmShip.txtbxScanID.Text + "," + frmShip.cmbobxBuyer.Text + "," + dt.ToString("d"))
swship.Close()
Catch ex As Exception
writeLocal()
End Try
WriteLocal:
Try
Dim swshipCatch As StreamWriter = New StreamWriter(localShipFile, True)
swshipCatch.WriteLine(frmShip.cmbobxLocation.Text + "," + frmShip.txtbxScanID.Text + "," + frmShip.cmbobxBuyer.Text + "," + "," + dt.ToString("d"))
swshipCatch.Flush()
swshipCatch.Close()
Catch ex As Exception
MessageBox.Show("writeLocal failed")
End Try
The above all works Fine.
The Below is when I attempt to access the localShipFile
Try
'SHIP
frmsr = New StreamReader("program files\steidingerfoodsce\localship.txt")
frmsw = New StreamWriter("\\lit3\device\ship.txt", True)
Do While frmsr.Peek() >= 0
frmsw.WriteLine(frmsr.ReadLine())
Loop
frmsr.Close()
frmsw.Close()
file.ReWriteSHIPFile() This is where it fails!!!
MessageBox.Show("SHip done")
Catch ex As Exception
MessageBox.Show(ex.ToString)
MessageBox.Show("Network connection not established")
End Try
ReWriteSHIPFIle()
Public Sub ReWriteSHIPFile()
dim swship as streamwriter = New StreamWriter(localShipFile, False) ----It fails when this line executes.
swship.Write("")
swship.Close()
End Sub
|
|
|
|
|
Cory Kimble wrote: Try 'SHIP frmsr = New StreamReader("program files\steidingerfoodsce\localship.txt")
You're really using a hard-coded path that assumes the current directory is C:\?? Change this to use a fully qualified path built from known sources, such as Application.StartupPath, the System.Io.Path class, and other methods of determining the true path to well-known folders.
|
|
|
|
|
Does Application.StartPath work in .net compact framework?
|
|
|
|
|
Cory Kimble wrote: Does Application.StartPath work in .net compact framework
Nope, not supported. You can find out what is and isn't supported in the .NET Framework documentation. Everything with a small phone-looking icon next to it is supported by the Compact framework. Click[^]
|
|
|
|
|
Hi Cory,
Your code is not very clear; here are my assumptions:
1. the files refered to in the lines frmsr = New StreamReader("program files\steidingerfoodsce\localship.txt")
and dim swship as streamwriter = New StreamWriter(localShipFile, False)
are the same
2. sub ReWriteSHIPFile intends to delete the original file.
Under those assumptions I see two potential problems:
1. you do close the streams frmsr (and frmsw) but you fail to dispose of them, although the
documentation says one should dispose of streams. (In fact one should dispose of any class instance
that has a public dispose method). Failing to do so is likely to have negative effects, in this
case I expect the underlying file system does not get cleaned up until the Finalize method is
executed (either by calling dispose explicitly, or by forcing a garbage collection, which happens
to occur when you minimize your form).
2. other processes may have the same file temporarily open (without you being aware); that happens
for fresh files when you have disk indexing enabled, or a gready antivirus program.
in these cases, you should attempt say 5 times in a loop, with 1 second intervals to solve almost
all of it.
Further remarks:
1.
you should avoid similar lines of code, they will lead to mistakes in the end (when you change one
and forget to change the other). Example: swship.WriteLine(frmShip.cmbobxLocation.Text + "," + frmShip.txtbxScanID.Text + "," + frmShip.cmbobxBuyer.Text + "," + dt.ToString("d"))
Solution: calculate the text line once, and (re)use it where ever you need it.
2.
you should avoid magic constants in your code; here "program files\steidingerfoodsce\localship.txt"
should be defined once, at the start of your program, in the initialization code, not in the middle of some
method. And I suspect you already have the variable localShipFile to point to it, so use it consistently.
3.
not sure you should store application data files under "program files". Some Windows versions, and/or some
installations won't let your app write anything there.
4.
I don't like the catch blocks that catch an exception and then ignore it, i.e. only the change-in-flow is used,
not the actual exception. On the other hand, MessageBox.Show(ex.ToString) is excellent; most
programmers use Message or so and miss most of the exception text...
5.
the sequence Flush() followed by Close() is overkill; AFAIK every Close() implies a Flush(), flush is useful
only if you do not intend to close right away but want to make sure the data so far got out to disk anyway.
6.
If the aim of ReWriteSHIPFile() is to delete a possibly existing file, there are better ways to do that.
And remember trying 5 times with 1 sec intervals (as Windows Explorer does, but no single API function offers).
Regards,
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Thank you so much for your time and comments.
|
|
|
|
|
I have searched the command for launching EXCEL.EXE with an existed .xls file without any success.
Can someone help me?
Thank in-advanced
|
|
|
|
|
Launch the ".xls" file itself. If Excel is installed, it'll launch and open the file, just like you double-clicked the file yourself.
|
|
|
|
|
Hi DAve,
I am sorry that I did not post the question clearly, what I meant was a valid command code in VB.net source codes in order to execute EXCEL.exe to open an existed .xls file (I do have EXCEL.exe available in my computer)
Anyhelp?
Thanks
|
|
|
|
|
You can find the documentation on the Process class here[^], along with examples on it's use.
|
|
|
|
|
Hi Dave,
Thank for the link, I am able to do what I posted
Thank for help
|
|
|
|
|