Showing posts with label saves. Show all posts
Showing posts with label saves. Show all posts

Saturday, March 31, 2012

Strange Behavior

Hi All,
I have a webapp page that gathers a bunch of parameters and then saves the parameters into a session variable (as an object). The code then redirects via Response.Redirect() to a new page. The new page then renders a chart based on the values stored in the session variable. On the resulting page I have a button that redirects back to the original page with the form. I'm doing this as follows: Response.Redirect("Frm_KPI.aspx?redo=1");
In the Page_Load() method in "Frm_KPI.aspx.cs" I have the following code:

protectedvoid Page_Load(object sender, System.EventArgs e)

{

//

// Initialize the database connection

//

InitDB();

// Check the query string to see if user wants to reload the form.

if (Quartz_QueryString("redo") =="1")

{

//

// User wants to reload the form from the Session parameter object

//

m_Params = (CKPIParameters)Session["KPI_PARAMS"];

InitJobTypeList(true);

InitJobOwnerList(true);

InitTimingParameters(true);

InitInterval(true);

InitTimeBase(true);

InitNormalizeBy(true);

InitJobPriorityList(m_Params.JobType,true);

InitJobAnalysisMode(m_Params.JobType,true);

InitJobAnalysisHistory(m_Params.JobType,true);

}

else

{

if (!IsPostBack)

{

m_Params.JobType ="SEM";

InitJobTypeList(false);

InitJobOwnerList(false);

InitTimingParameters(false);

InitInterval(false);

InitTimeBase(false);

InitNormalizeBy(false);

InitJobPriorityList(m_Params.JobType,false);

InitJobAnalysisMode(m_Params.JobType,false);

InitJobAnalysisHistory(m_Params.JobType,false);

m_hplStartDate.NavigateUrl ="javascript:;";

m_hplEndDate.NavigateUrl ="javascript:;";

this.m_editStartDate.Text ="01/01/2005";

this.m_editEndDate.Text ="12/31/2005";

//

// Now disable all of the controls except for the job type selector

//

EnableControls(false);

ShowControls(false);

}

}

}

privatestring Quartz_QueryString(string strName)

{

string strReturn ="";

if (Request.QueryString[strName] !=null && Request.QueryString[strName].Length > 0)

{

strReturn = Request.QueryString[strName].ToString();

}

return (strReturn);

}



The problem is that when the new page is redisplayed, all the values are set correctly, but when I attempt to resubmit the page, the line
if (Quartz_QueryString("redo") =="1")

resolves to true even though I'm doing a post via my submit button. Consequently any changes I make to the variables don't stick when the form is ultimately submitted. Any thoughts??
Regards,
Greg.

Possible solution:
I changed the QueryString check to the following:

if (Context.Items["RedoQuery"] !=null && Context.Items["RedoQuery"].ToString().Length > 0)
{
// Reload form from session values
}

This has the desired effect. After the initial Page_Load() the context disappears and the above code block is not called repeatedly.

Wednesday, March 28, 2012

Strange character [Â] appearing in file.

Hi All,

I have an ASP.NET application which opens a text file and then saves it again.
This saved file is then passed into another software suite (not ASP.NET) for processing.
This secondary suite expects the lines in the file to be a certain length (legacy).

All text is transferred between the files correctly, except for where a uk pound sign [£] appears in the original file.

This character is NOT transferredusing the default StreamReader/Writer.

I havetriedchanging the encoding of both the StreamReader and the StreamWriter butcannot find one which does the job as expected, changing the reader toASCII and leaving the default writer is the closest but not withoutissues.
Where thiscondition occurs, the resulting saved file, in the place where theoriginal pound sign was displayed shows 2 (!) characters ?£.

This extra ? [alt + 0194] character is not seen by Notepad, not visiblewhen debugging, but is there, as it affects the fixed length recordprocessing mentioned above, and can be seen in other text editors suchas Wordpad, DocPad etc.

Please help, this is causing some despair!

Thanks and Regards,
Ric

The following code sample demonstrates my point...
[A file c:\testInput.txt is also required with a line in it that contains a £ sign]
eg. 111111111111111£1111111111
[A file c:\testOutput.txt is created to demonstrate the point.]

Sub Main()

'file access vars
Dim fsRead As FileStream
Dim fsWrite As FileStream
Dim sr As StreamReader
Dim sw As StreamWriter

'The file to read from
Dim inputFile As String = "c:\testInput.txt"
Dim outputFile As String = "c:\testOutput.txt"

'Create the FileStream : INPUT
Try
fsRead = New FileStream(inputFile, FileMode.Open, FileAccess.Read,FileShare.Read)
Catch ex As Exception
End Try

'Create the StreamReader
Try
sr = New StreamReader(fsRead, System.Text.Encoding.ASCII)
Catch ex As Exception
End Try

'Create the FileStream : OUTPUT
Try
fsWrite = New FileStream(outputFile, FileMode.OpenOrCreate,FileAccess.Write, FileShare.ReadWrite)
Catch ex As Exception
End Try

'Create the StreamWriter
Try
sw = New StreamWriter(fsWrite)
Catch ex As Exception
End Try

Dim s As String
Dim replaced As String
While sr.Peek <> -1

'read a line form the input file
s = sr.ReadLine()

'substitute the misread ? characters into £ signs
replaced = Replace(s, "?", "£")

'move to the end of the file
sw.BaseStream.Seek(0, SeekOrigin.End)

'and write the string to the output file
sw.Write(replaced & vbCrLf)

End While

'Clear up...
Try
sw.Flush()
sw.Close()
fsWrite.Close()

sr.Close()
fsRead.Close()

Catch ex As Exception
End Try

'and dispose
sw = Nothing
sr = Nothing
fsRead = Nothing
fsWrite = Nothing

End Sub

Solution:

When reading/writing plain text files using file streams on a Windows box you should specify an Encoding.

The Encoding can be created using the following code:
[1252 is the Western Windows codepage]

Dim encAs System.Text.Encoding
enc = System.Text.Encoding.GetEncoding(1252)

You can then specify this encoding to be used when creating the StreamReader/StreamWriter.

All characters (even the uk pound sign £) will then be correctly read/written by the streams.

Hope this helps,
Ric

Strange Characters in Asp.Net Text Box

Hello,

I have a form which saves some data to an SQL 2005 database
If the text written in the text box contains characters such as "??áàéí" when I load the data again I get a few strange characters: "?§?£??? ???"

What is going wrong here?

How can I solve this?

Thanks,
Miguel

What encoding did you set for the pages? Set the responseEncoding to "utf-8"

Check this link on how to set it in the web.config filehttp://msdn2.microsoft.com/en-us/library/hy4kkhe0(vs.80).aspx

Thanks

Strange Characters in Asp.Net Text Box

Hello,
I have a form which saves some data to an SQL 2005 database
If the text written in the text box contains characters such as
"=E7=E3=E1=E0=E9=ED" when I load the data again I get a few strange charact=
ers:
"=C3=A7=C3=A3=C3=A1=C3 =C3=A9=C3"
What is going wrong here?
How can I solve this?
Thanks,
MiguelHello shapper,
This seems like a flaw in Unicode encoding mode in your URL If the character
s
you have types are unicode, then make sure that you save the file in proper
Unicode mode. If you are using VS2005, then you will find a command 'Advance
d
Save Options' which does this.
Thanks
Cyril Gupta
-- You can do anything with a little bit of 'magination.
-- I've been programming so long, my brain is now software.

> Hello,
> I have a form which saves some data to an SQL 2005 database
> If the text written in the text box contains characters such as
> "" when I load the data again I get a few strange characters:
> "??á é"
> What is going wrong here?
> How can I solve this?
> Thanks,
> Miguel

Strange Characters in Asp.Net Text Box

Hello,

I have a form which saves some data to an SQL 2005 database
If the text written in the text box contains characters such as
"" when I load the data again I get a few strange characters:
"??á é"

What is going wrong here?

How can I solve this?

Thanks,
MiguelHello shapper,

This seems like a flaw in Unicode encoding mode in your URL If the characters
you have types are unicode, then make sure that you save the file in proper
Unicode mode. If you are using VS2005, then you will find a command 'Advanced
Save Options' which does this.

Thanks
Cyril Gupta

-- You can do anything with a little bit of 'magination.

-- I've been programming so long, my brain is now software.

Quote:

Originally Posted by

Hello,
>
I have a form which saves some data to an SQL 2005 database
If the text written in the text box contains characters such as
"" when I load the data again I get a few strange characters:
"??á é"
What is going wrong here?
>
How can I solve this?
>
Thanks,
Miguel