ViewState

VeiwState in one way to save data for the user. When a postback is made to the server, the fields on a form can be repopulated using ViewState. By default, ViewState is enabled automatically but can be disabled. The values from ViewState cannot be carried to other web pages throughout the website.

Reading a value from ViewState example:
This example saves the value “apple” to the Control.ViewState property. When the postback occurs (posting the page back to the server), the value of the property is retrieved.

String strFruit
If (Page.IsPostBack)
{
// get the property value and display it
strFruit = (string)ViewState[“FruitType”];
Response.Write(strFruit);
}
Else
{
// save the poperty value
ViewState[“FruitType”] = “apple”;
}

Command Line Arguments Will Not Be Passed

I was working on an application and added a command line argument under the Start Options on the Debug tab under the program’s properties.

When I start the program, I am getting the message “The current project settings specify that the project will be debugged with specific security permissions. In this mode, command line arguments will not be passed to the executable. Do you want to continue debugging anyway? Yes NO.”

To get past the message, I unchecked the Enable ClickOnce Security Settings checkbox on the Security tab.

Replacing 0x0D With 0×20 Using MemoryStream

One of my projects involves parsing a text file that is generated from a mainframe. Occasionally the mainframe program injects a carriage return (0x0D) in the middle of the text line. This injection creates issues with other applications that are using the text file.

The previous programmer had created a method to remove the 0x0D with a space (0×20). This method works fine but takes around 30 minutes to process the file. The text file can be anywhere from 4 to 5 Megs. The method was using the FileStream class.

I felt this was taking to long and there had to be a better way. After doing some research, I came up with a new method that uses the MemoryStream class. By changing from the FileStream to the MemoryStream class, the process now takes approximately 5 seconds to complete.

Going from 30 minutes to 5 seconds was a major performance difference and has reduced the time for the text file cleansing.

Here is the modified method using the MemoryStream class:

private void ReplaceByte(string strInputFile)
{
byte[] filebytes = File.ReadAllBytes(strInputFile);
bool modified = false;


for (int i = 0; i < filebytes.Length; i++)
{
try
{
if (filebytes[i] == 0x0D)
{
filebytes[i] = 0x20;
modified = true;
}
}
catch (Exception ex)
{
lblStatus.Text = "Error: " + ex;
}
}

if (modified)
{
File.WriteAllBytes(strInputFile, filebytes);
}
}

And here is the original method using the FileStream class:

private void replaceStream(string strInPutFile)
{
FileStream fileStream = new FileStream(strInPutFile, FileMode.Open, FileAccess.ReadWrite);

byte filebyte;

while (fileStream.Position < fileStream.Length)
{
try
{
filebyte = (byte)fileStream.ReadByte();
if (filebyte == 0x0D)
{
filebyte = 0×20;
fileStream.Position = fileStream.Position – 1;
fileStream.WriteByte(filebyte);
}
}
catch (Exception ex)
{
lblStatus.Text = “error ” + ex;
}

}
fileStream.Close();

}

Removing Duplicates in a String Array

One of my projects was to parse text data and extract data that met certain requirements. Some of the text data contained duplicate data and the duplicates had to be removed before inserting into the database.

Here is the method I used to remove the duplicate data.


public static string[] GetDistinctArray(string[] stringArray)
{
ArrayList newList = new ArrayList();

foreach (string str in stringArray)
{
if (!newList.Contains(str))
{
newList.Add(str);
}
}

return (string[])newList.ToArray(typeof(string));
}