Monday, October 15, 2007

Access items on the parent form from the child form

Suppose you have a session variable in your main form which you want to check in a child form to find out if the button text should be "logon" or "logoff".

In the child form, declare a variable MyParentForm:

public MainForm MyParentForm;

In the main form display the child form and pass a pointer to the main form:

ChildForm logonForm = new ChildForm();
logonForm.MyParentForm = this;
logonForm.Show();

In the child form you can now access the session variable:

if(MyParentForm.MySessionContext == null)
{
DoSomething();
}

Saturday, October 13, 2007

SRS Switch versus Iif

How do you like the Iif statement equivalent of the Switch statement in the previous post? :-)

=IIF(Fields!Stops.Value = 0, "",
(IIF(Fields!Stops.Value = 1, Fields!ScheduleJourneyCityPairs.Value.Substring(4, 3),
(IIF(Fields!Stops.Value = 2, Fields!ScheduleJourneyCityPairs.Value.Substring(4, 3) & " " &
Fields!ScheduleJourneyCityPairs.Value.Substring(12, 3),
Fields!ScheduleJourneyCityPairs.Value.Substring(4, 3) & " " &
Fields!ScheduleJourneyCityPairs.Value.Substring(12, 3) & " " &
Fields!ScheduleJourneyCityPairs.Value.Substring(20, 3))))))

SRS expression evaluation issue

Consider a column that contains a journey of at least one city pair and may look as follows JFK/BOS BOS/SLC SLC/LAX. Another column contains the number of stops (in this case 2).

The following expression will correctly report "BOS SLC". However in case the column contains JFK/BOS BOS/SLC or even only a non-stop JFK/BOS (stop values 1 and 0), the report will show "#error".

=Switch (
Fields!Stops.Value = 0, "",
Fields!Stops.Value = 1, Fields!ScheduleJourneyCityPairs.Value.Substring(4, 3),
Fields!Stops.Value = 2, Fields!ScheduleJourneyCityPairs.Value.Substring(4, 3) & " " &
Fields!ScheduleJourneyCityPairs.Value.Substring(12, 3),
Fields!Stops.Value = 3, Fields!ScheduleJourneyCityPairs.Value.Substring(4, 3) & " " &
Fields!ScheduleJourneyCityPairs.Value.Substring(12, 3) & " " &
Fields!ScheduleJourneyCityPairs.Value.Substring(20, 3)
)


This is due to the fact that SRS evaluates the complete expression, and errors on the fact that the column value is too short for offset 20. We can solve this by changing the last SELECT statement as follows:

SELECT
ScheduleJourney_RoutingID,
CASE WHEN LEN(ScheduleJourneyCityPairs) < 24
THEN ScheduleJourneyCityPairs + SPACE(24-LEN(ScheduleJourneyCityPairs))
ELSE ScheduleJourneyCityPairs
END AS ScheduleJourneyCityPairs,
JourneyDepartureStation,
....


Thursday, September 13, 2007

How to convert a byte array to string and vice versa

Here are a few sample commands

System.Text.Encoding encoding = System.Text.Encoding.ASCII;

// convert a string to a byte array
Byte[] data = encoding.GetBytes( "a text string" );
// shorter form
Byte[] data = System.Text.Encoding.GetBytes( "a text string" );

// convert a byte array to a string
string mydata = encoding.GetString( data );
// shorter form
string mydata = System.Text.Encoding.GetString( data );